Skip to content

Commit 73186a8

Browse files
feat(optimizer)!: annotate type for Snowflake REGEXP_COUNT function (#5963)
1 parent d425ba2 commit 73186a8

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

sqlglot/dialects/snowflake.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ class Snowflake(Dialect):
590590
exp.ParseUrl,
591591
exp.ParseIp,
592592
},
593+
exp.DataType.Type.DECIMAL: {
594+
exp.RegexpCount,
595+
},
593596
}
594597

595598
ANNOTATORS = {
@@ -611,6 +614,9 @@ class Snowflake(Dialect):
611614
},
612615
exp.ConcatWs: lambda self, e: self._annotate_by_args(e, "expressions"),
613616
exp.Reverse: _annotate_reverse,
617+
exp.RegexpCount: lambda self, e: self._annotate_with_type(
618+
e, exp.DataType.build("NUMBER", dialect="snowflake")
619+
),
614620
}
615621

616622
TIME_MAPPING = {

sqlglot/expressions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7293,6 +7293,15 @@ class RegexpSplit(Func):
72937293
arg_types = {"this": True, "expression": True, "limit": False}
72947294

72957295

7296+
class RegexpCount(Func):
7297+
arg_types = {
7298+
"this": True,
7299+
"expression": True,
7300+
"position": False,
7301+
"parameters": False,
7302+
}
7303+
7304+
72967305
class Repeat(Func):
72977306
arg_types = {"this": True, "times": True}
72987307

tests/dialects/test_snowflake.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,10 @@ def test_regexp_substr(self, logger):
24212421
"REGEXP_EXTRACT_ALL(subject, pattern)",
24222422
)
24232423

2424+
self.validate_identity("SELECT REGEXP_COUNT('hello world', 'l')")
2425+
self.validate_identity("SELECT REGEXP_COUNT('hello world', 'l', 1)")
2426+
self.validate_identity("SELECT REGEXP_COUNT('hello world', 'l', 1, 'i')")
2427+
24242428
@mock.patch("sqlglot.generator.logger")
24252429
def test_regexp_replace(self, logger):
24262430
self.validate_all(

tests/fixtures/optimizer/annotate_functions.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,18 @@ BOOLEAN;
18831883
REGEXP_LIKE('foo', NULL, 'baz');
18841884
BOOLEAN;
18851885

1886+
# dialect: snowflake
1887+
REGEXP_COUNT('hello world', 'l');
1888+
DECIMAL(38, 0);
1889+
1890+
# dialect: snowflake
1891+
REGEXP_COUNT('hello world', 'l', 1);
1892+
DECIMAL(38, 0);
1893+
1894+
# dialect: snowflake
1895+
REGEXP_COUNT('hello world', 'l', 1, 'i');
1896+
DECIMAL(38, 0);
1897+
18861898
# dialect: snowflake
18871899
REGEXP_REPLACE('hello world', 'world', 'universe');
18881900
VARCHAR;

0 commit comments

Comments
 (0)