Skip to content
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
31 changes: 31 additions & 0 deletions udfs/migration/snowflake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ SELECT bqutil.sf.factorial(0)
## UDFs

* [array_equal](#array_equal)
* [bitmap_bit_position](#bitmap_bit_position)
* [bitmap_bucket_number](#bitmap_bucket_number)
* [factorial](#factorial)
* [flatten](#flatten)
* [json_ilike](#json_ilike)
* [object_agg](#object_agg)


## Documentation
Expand All @@ -38,6 +42,33 @@ SELECT bqutil.sf.array_equal([JSON '1', JSON '2'], [JSON '1', JSON '2']) as eq1,
eq1|eq2|eq3
---|---|---
true|true|false

### [bitmap_bit_position(value INT64)](bitmap_bit_position.sqlx)
Emulates the `bitmap_bit_position` function present in Snowflake. [Snowflake docs](https://docs.snowflake.com/en/sql-reference/functions/bitmap_bit_position)

```sql
SELECT bqutil.sf.bitmap_bit_position(1) as pos1,
bqutil.sf.bitmap_bit_position(32768) as pos2,
bqutil.sf.bitmap_bit_position(32769) as pos3;
```

pos1|pos2|pos3
---|---|---
0|32767|0

### [bitmap_bucket_number(value INT64)](bitmap_bucket_number.sqlx)
Emulates the `bitmap_bucket_number` function present in Snowflake. [Snowflake docs](https://docs.snowflake.com/en/sql-reference/functions/bitmap_bucket_number)

```sql
SELECT bqutil.sf.bitmap_bucket_number(1) as bucket1,
bqutil.sf.bitmap_bucket_number(32768) as bucket2,
bqutil.sf.bitmap_bucket_number(32769) as bucket3;
```

bucket1|bucket2|bucket3
---|---|---
1|1|2

### [factorial(integer_expr INT64)](factorial.sqlx)
Computes the factorial of its input. The input argument must be an integer expression in the range of `0` to `27`. Due to data type differences, the maximum input value in BigQuery is smaller than in Snowflake. [Snowflake docs](https://docs.snowflake.com/en/sql-reference/functions/factorial.html)
```sql
Expand Down
24 changes: 24 additions & 0 deletions udfs/migration/snowflake/bitmap_bit_position.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
config { hasOutput: true }
/*
* Copyright 2026 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()}(value INT64)
RETURNS INT64
OPTIONS (
description = "Emulates the 'bitmap_bit_position' function present in Snowflake."
) AS (
if(value > 0, (value - 1) & 32767, 0)
);
24 changes: 24 additions & 0 deletions udfs/migration/snowflake/bitmap_bucket_number.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
config { hasOutput: true }
/*
* Copyright 2026 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()}(value INT64)
RETURNS INT64
OPTIONS (
description = "Emulates the 'bitmap_bucket_number' function present in Snowflake."
) AS (
if(value > 0, ((value - 1) >> 15) + 1, 0)
);
61 changes: 60 additions & 1 deletion udfs/migration/snowflake/test_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,64 @@ generate_udf_test("array_equal", [
}
]);

generate_udf_test("bitmap_bucket_number", [
{
inputs: [`CAST(1 AS INT64)`],
expected_output: `CAST(1 AS INT64)`
},
{
inputs: [`CAST(1000000 AS INT64)`],
expected_output: `CAST(31 AS INT64)`
},
{
inputs: [`CAST(32767 AS INT64)`],
expected_output: `CAST(1 AS INT64)`
},
{
inputs: [`CAST(32768 AS INT64)`],
expected_output: `CAST(1 AS INT64)`
},
{
inputs: [`CAST(32769 AS INT64)`],
expected_output: `CAST(2 AS INT64)`
},
{
inputs: [`CAST(400000 AS INT64)`],
expected_output: `CAST(13 AS INT64)`
},
{
inputs: [`CAST(0 AS INT64)`],
expected_output: `CAST(0 AS INT64)`
}
]);


generate_udf_test("bitmap_bit_position", [
{
inputs: [`CAST(1 AS INT64)`],
expected_output: `CAST(0 AS INT64)`
},
{
inputs: [`CAST(1000000 AS INT64)`],
expected_output: `CAST(16959 AS INT64)`
},
{
inputs: [`CAST(32767 AS INT64)`],
expected_output: `CAST(32766 AS INT64)`
},
{
inputs: [`CAST(32768 AS INT64)`],
expected_output: `CAST(32767 AS INT64)`
},
{
inputs: [`CAST(32769 AS INT64)`],
expected_output: `CAST(0 AS INT64)`
},
{
inputs: [`CAST(400000 AS INT64)`],
expected_output: `CAST(6783 AS INT64)`
},
{
inputs: [`CAST(0 AS INT64)`],
expected_output: `CAST(0 AS INT64)`
}
]);
Loading