Skip to content

Commit f9871ab

Browse files
feat(optimizer)!: annotate types for Snowflake BIT_LENGTH function
1 parent 42cfc79 commit f9871ab

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

sqlglot/dialects/snowflake.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ class Snowflake(Dialect):
532532
exp.DataType.Type.INT: {
533533
*Dialect.TYPE_TO_EXPRESSIONS[exp.DataType.Type.INT],
534534
exp.Length,
535+
exp.BitLength,
535536
},
536537
exp.DataType.Type.VARCHAR: {
537538
*Dialect.TYPE_TO_EXPRESSIONS[exp.DataType.Type.VARCHAR],

sqlglot/expressions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6875,6 +6875,10 @@ class Length(Func):
68756875
_sql_names = ["LENGTH", "LEN", "CHAR_LENGTH", "CHARACTER_LENGTH"]
68766876

68776877

6878+
class BitLength(Func):
6879+
pass
6880+
6881+
68786882
class Levenshtein(Func):
68796883
arg_types = {
68806884
"this": True,

tests/dialects/test_snowflake.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def test_snowflake(self):
3434
"SELECT BASE64_DECODE_BINARY('SGVsbG8=', 'ABCDEFGHwxyz0123456789+/')"
3535
)
3636

37+
self.validate_identity("SELECT BIT_LENGTH('abc')")
38+
self.validate_identity("SELECT BIT_LENGTH(x'A1B2')")
3739
self.validate_identity("SELECT {*} FROM my_table")
3840
self.validate_identity("SELECT {my_table.*} FROM my_table")
3941
self.validate_identity("SELECT {* ILIKE 'col1%'} FROM my_table")

tests/fixtures/optimizer/annotate_functions.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,18 @@ VARCHAR;
15631563
BASE64_DECODE_BINARY('SGVsbG8=');
15641564
BINARY;
15651565

1566+
# dialect: snowflake
1567+
BIT_LENGTH('abc');
1568+
INT;
1569+
1570+
# dialect: snowflake
1571+
BIT_LENGTH(tbl.str_col);
1572+
INT;
1573+
1574+
# dialect: snowflake
1575+
BIT_LENGTH(tbl.bin_col);
1576+
INT;
1577+
15661578
# dialect: snowflake
15671579
CHARINDEX('world', 'hello world');
15681580
INT;

0 commit comments

Comments
 (0)