Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add bitwise operation for varbit with variable length #107863

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,30 @@ The swap_ordinate_string parameter is a 2-character string naming the ordinates
</span></td><td>Immutable</td></tr>
<tr><td><a name="bit_length"></a><code>bit_length(val: varbit) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Calculates the number of bits used to represent <code>val</code>.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_and"></a><code>bitmask_and(a: <a href="string.html">string</a>, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise AND value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_and"></a><code>bitmask_and(a: <a href="string.html">string</a>, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise AND value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_and"></a><code>bitmask_and(a: varbit, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise AND value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_and"></a><code>bitmask_and(a: varbit, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise AND value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_or"></a><code>bitmask_or(a: <a href="string.html">string</a>, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise OR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_or"></a><code>bitmask_or(a: <a href="string.html">string</a>, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise OR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_or"></a><code>bitmask_or(a: varbit, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise OR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_or"></a><code>bitmask_or(a: varbit, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise OR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_xor"></a><code>bitmask_xor(a: <a href="string.html">string</a>, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise XOR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_xor"></a><code>bitmask_xor(a: <a href="string.html">string</a>, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise XOR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_xor"></a><code>bitmask_xor(a: varbit, b: <a href="string.html">string</a>) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise XOR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="bitmask_xor"></a><code>bitmask_xor(a: varbit, b: varbit) &rarr; varbit</code></td><td><span class="funcdesc"><p>Calculates bitwise XOR value of unsigned bit arrays ‘a’ and ‘b’ that may have different lengths.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="btrim"></a><code>btrim(input: <a href="string.html">string</a>, trim_chars: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Removes any characters included in <code>trim_chars</code> from the beginning or end of <code>input</code> (applies recursively).</p>
<p>For example, <code>btrim('doggie', 'eod')</code> returns <code>ggi</code>.</p>
</span></td><td>Immutable</td></tr>
Expand Down
129 changes: 129 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -4037,3 +4037,132 @@ SELECT crdb_internal.merge_aggregated_stmt_metadata(ARRAY[ '{"aMalformedMetadaOb
{"appNames": [], "db": [], "distSQLCount": 0, "failedCount": 0, "fingerprintID": "", "formattedQuery": "", "fullScanCount": 0, "implicitTxn": false, "query": "", "querySummary": "", "stmtType": "", "totalCount": 0, "vecCount": 0}

subtest end

# Test bitmask_or function for string and varbit data
query T
SELECT bitmask_or('1010010', '0101');
----
1010111

query T
SELECT bitmask_or('1010010'::bit(7), '0101'::bit(4));
----
1010111

query T
SELECT bitmask_or('1010010'::bit(7), '0101');
----
1010111

query T
SELECT bitmask_or('1010010', '0101'::bit(4));
----
1010111

query T
SELECT bitmask_or('1010010'::varbit, '0101'::varbit);
----
1010111

# Test bitmask_and function for string and varbit data
query T
SELECT bitmask_and('1010010', '0101');
----
0000000

query T
SELECT bitmask_and('1010010'::bit(7), '0101'::bit(4));
----
0000000

query T
SELECT bitmask_and('1010010'::bit(7), '0101');
----
0000000

query T
SELECT bitmask_and('1010010', '0101'::bit(4));
----
0000000

query T
SELECT bitmask_and('1010010'::varbit, '0101'::varbit);
----
0000000

# Test bitmask_xor function for string and varbit data
query T
SELECT bitmask_xor('1010010', '0101');
----
1010111

query T
SELECT bitmask_xor('1010010'::bit(7), '0101'::bit(4));
----
1010111

query T
SELECT bitmask_xor('1010010'::bit(7), '0101');
----
1010111

query T
SELECT bitmask_xor('1010010', '0101'::bit(4));
----
1010111

query T
SELECT bitmask_xor('1010010'::varbit, '0101'::varbit);
----
1010111

# Test for invalid inputs.
statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_or('not binary', '111')

statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_or('111', 'not binary')

statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_and('not binary', '111')

statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_and('111', 'not binary')

statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_xor('not binary', '111')

statement error pgcode 22P02 could not parse string as bit array: "n" is not a valid binary digit
SELECT bitmask_xor('111', 'not binary')

# Accept hex as well as binary inputs.
query T
SELECT bitmask_or('xfe8c', '1111');
----
1111111010001111

query T
SELECT bitmask_and('1111', 'xfe8c');
----
0000000000001100

query T
SELECT bitmask_xor('xfe8c', '1111');
----
1111111010000011

# Test on a large (>64 bit) input.
query T
SELECT bitmask_or('xffffffffffffffffff', '010');
----
111111111111111111111111111111111111111111111111111111111111111111111111

query T
SELECT bitmask_and('xffffffffffffffffff', '010');
----
000000000000000000000000000000000000000000000000000000000000000000000010

query T
SELECT bitmask_xor('xffffffffffffffffff', '010');
----
111111111111111111111111111111111111111111111111111111111111111111111101
Loading