Skip to content

Commit

Permalink
Translation Engine UDF for GETBIT on binary type. (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-compilerworks authored Jun 6, 2024
1 parent 7b87a12 commit 5f20e54
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
13 changes: 12 additions & 1 deletion udfs/community/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SELECT bqutil.fn.int(1.684)
* [cw_find_in_list](#cw_find_in_listneedle-string-list-string)
* [cw_from_base](#cw_from_basenumber-string-base-int64)
* [cw_getbit](#cw_getbitbits-int64-index-int64)
* [cw_getbit_binary](#cw_getbit_binarybits-bytes-index-int64)
* [cw_initcap](#cw_initcaps-string)
* [cw_instr4](#cw_instr4source-string-search-string-position-int64-ocurrence-int64)
* [cw_json_array_contains_bool](#cw_json_array_contains_booljson-string-needle-bool)
Expand Down Expand Up @@ -530,7 +531,7 @@ SELECT bqutil.fn.cw_from_base('A', 16);
```

### [cw_getbit(bits INT64, index INT64)](cw_getbit.sqlx)
Get bit on given inex.
Return bit of INT64 input at given index, starting from 0 for the least significant bit.
```sql
SELECT bqutil.fn.cw_getbit(11, 100);
SELECT bqutil.fn.cw_getbit(11, 3);
Expand All @@ -539,6 +540,16 @@ SELECT bqutil.fn.cw_getbit(11, 3);
1
```

### [cw_getbit_binary(bits BYTES, index INT64)](cw_getbit_binary.sqlx)
Return bit of BYTES input at given index, starting from 0 for the least significant bit.
```sql
SELECT bqutil.fn.cw_getbit_binary(b'\x0B', 100);
SELECT bqutil.fn.cw_getbit_binary(b'\x0B', 3);

0
1
```

### [cw_initcap(s STRING)](cw_initcap.sqlx)
Takes an input string and returns input string with first letter capital.
```sql
Expand Down
7 changes: 5 additions & 2 deletions udfs/community/cw_getbit.sqlx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
config { hasOutput: true }
/*
* Copyright 2022 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,9 @@ config { hasOutput: true }
* limitations under the License.
*/

CREATE OR REPLACE FUNCTION ${self()}(bits INT64, index INT64) RETURNS INT64 AS (
CREATE OR REPLACE FUNCTION ${self()}(bits INT64, index INT64)
RETURNS INT64
OPTIONS(description="Return bit of BYTES input at given index, starting from 0 for the least significant bit.")
AS (
(bits & (1 << index)) >> index
);
23 changes: 23 additions & 0 deletions udfs/community/cw_getbit_binary.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
config { hasOutput: true }
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

CREATE OR REPLACE FUNCTION ${self()}(bits BYTES, index INT64)
RETURNS INT64
OPTIONS(description="Return bit of BYTES input at given index, starting from 0 for the least significant bit.")
AS (
BIT_COUNT(RIGHT(bits >> index, 1) & b'\x01')
);
16 changes: 16 additions & 0 deletions udfs/community/test_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,22 @@ generate_udf_test("cw_getbit", [
expected_output: `CAST(1 AS INT64)`
},
]);
generate_udf_test("cw_getbit_binary", [
{
inputs: [
`b'\\x0B'`,
`CAST(100 AS INT64)`
],
expected_output: `CAST(0 AS INT64)`
},
{
inputs: [
`b'\\x0B'`,
`CAST(3 AS INT64)`
],
expected_output: `CAST(1 AS INT64)`
},
]);
generate_udf_test("cw_setbit", [
{
inputs: [
Expand Down

0 comments on commit 5f20e54

Please sign in to comment.