Skip to content

Commit 7ecae1b

Browse files
committed
Add presto unsupported warning, tests
1 parent 754dd00 commit 7ecae1b

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

sqlglot/dialects/bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class BigQuery(Dialect):
360360

361361
# https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#initcap
362362
INITCAP_SUPPORTS_CUSTOM_DELIMITERS = True
363-
INITCAP_DEFAULT_DELIMITER_CHARS = ' \t\n\r\f\v\\[\\](){}/|\<>!?@"^#$&~_,.:;*%+\\-'
363+
INITCAP_DEFAULT_DELIMITER_CHARS = ' \t\n\r\f\v\\[\\](){}/|<>!?@"^#$&~_,.:;*%+\\-'
364364

365365
# https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#case_sensitivity
366366
NORMALIZATION_STRATEGY = NormalizationStrategy.CASE_INSENSITIVE

sqlglot/dialects/presto.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545

4646

4747
def _initcap_sql(self: Presto.Generator, expression: exp.Initcap) -> str:
48+
delimiters = expression.expression
49+
if delimiters and not (
50+
delimiters.is_string and delimiters.this == self.dialect.INITCAP_DEFAULT_DELIMITER_CHARS
51+
):
52+
self.unsupported("INITCAP does not support custom delimiters")
53+
4854
regex = r"(\w)(\w*)"
4955
return f"REGEXP_REPLACE({self.sql(expression, 'this')}, '{regex}', x -> UPPER(x[1]) || LOWER(x[2]))"
5056

tests/dialects/test_dialect.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,6 +4325,11 @@ def duckdb_regex_literal_sql(delimiters: str) -> str:
43254325
escaped_literal = "".join(REGEX_LITERAL_ESCAPES.get(ch, ch) for ch in delimiters)
43264326
return exp.Literal.string(escaped_literal).sql("duckdb")
43274327

4328+
# None delimiters arg doesn't error
4329+
with self.subTest("Testing INITCAP with None delimiters arg"):
4330+
self.assertEqual(exp.Initcap(this=exp.Literal.string("col")).sql(), "INITCAP('col')")
4331+
self.assertEqual(exp.Initcap(this=exp.column("col")).sql(), "INITCAP(col)")
4332+
43284333
# default delimiters not present in roundtrip
43294334
for dialect in delimiter_chars:
43304335
with self.subTest(
@@ -4439,6 +4444,8 @@ def escape_expression_sql(sql: str) -> str:
44394444

44404445
def test_initcap_custom_delimiter_warning(self):
44414446
expression = parse_one("INITCAP(col, '_')", read="bigquery")
4442-
with self.assertLogs(generator_logger, level="WARNING") as cm:
4443-
expression.sql("postgres")
4444-
self.assertIn("INITCAP does not support custom delimiters", cm.output[0])
4447+
for dialect in ("postgres", "presto"):
4448+
with self.subTest(f"INITCAP unsupported custom delimiters warning for {dialect}"):
4449+
with self.assertLogs(generator_logger, level="WARNING") as cm:
4450+
expression.sql(dialect)
4451+
self.assertIn("INITCAP does not support custom delimiters", cm.output[0])

tests/dialects/test_presto.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,3 +1367,11 @@ def test_bit_aggs(self):
13671367
"oracle": "BITWISE_XOR_AGG(x)",
13681368
},
13691369
)
1370+
1371+
def test_initcap(self):
1372+
self.validate_all(
1373+
"INITCAP(col)",
1374+
write={
1375+
"presto": "REGEXP_REPLACE(col, '(\w)(\w*)', x -> UPPER(x[1]) || LOWER(x[2]))",
1376+
},
1377+
)

0 commit comments

Comments
 (0)