Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VW5fl-0003BM-6C for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Oct 2013 14:32:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VW5fk-0006sw-MT for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Oct 2013 14:32:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VW5fk-0006sq-3f for pgsql-bugs@postgreSQL.org; Tue, 15 Oct 2013 14:32:32 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VW5fd-0004WG-Hd for pgsql-bugs@postgreSQL.org; Tue, 15 Oct 2013 14:32:31 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id r9FEWJdA022830; Tue, 15 Oct 2013 10:32:19 -0400 From: Tom Lane To: Gabriel Ciubotaru cc: pgsql-bugs@postgreSQL.org Subject: Re: Bit String expand bug In-reply-to: <525D4A40.9070605@bitdefender.com> References: <525670B3.5060007@bitdefender.com> <3061.1381479269@sss.pgh.pa.us> <525D4A40.9070605@bitdefender.com> Comments: In-reply-to Gabriel Ciubotaru message dated "Tue, 15 Oct 2013 16:59:28 +0300" Date: Tue, 15 Oct 2013 16:32:19 +0200 Message-ID: <6428.1381847539@sss.pgh.pa.us> X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Gabriel Ciubotaru writes: > I have a table, with a column of type bit varying for storing > bit string. This means for me a features list (enabled/disabled). > Now, if i have an entry with first 2 features only (eg B'11') > and i need to select all entries which have the 3rd feature enabled, the > normal syntax for this is : > SELECT * FROM my_table WHERE features & B'100' = B'100' (just > test the 3rd bit to be enabled). > There is a little problem because & cannot operate on different > length bit string, so we must extend one of the operand (in our case, > "features" bit string) and the query will be: > SELECT * FROM my_table WHERE features::bit(3) & B'100' = B'100'. > Now, the problem is when we have a "feature" with less than 3 > bits. If we have the "feature" = B'11' , after extending it to bit(3) , > it will become B'110' (right padding with 0) and the result will be > B'110' & B'100' = B'100' which is not good. > If it was left padded , it will become B'011' and the result > was B'000' which means the "feature" bit is unset. > Now, my question is, the right padding is intended to work like > this ? For working in Big Endian (counting bits from left to right) ? Or > is just wrong coded ? It's intended to work like that, and documented to work like that; see the first Note on page http://www.postgresql.org/docs/9.3/static/datatype-bit.html I believe that this is compliant with the SQL99 standard, which states that casting from bit(n) to bit(more-than-n) is done by padding with zeroes on the right (see 6.22 general rule 11c). There are other places in the spec where they confusingly refer to most significant or least significant bits, without clearly saying which end of the string that means; but "on the right" seems pretty clear to me. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs