Skip to content

Commit fd48100

Browse files
authored
Fix(presto)!: generate "SUBSTR" instead of "SUBSTRING" for Substr expr (#7583)
1 parent 834240f commit fd48100

5 files changed

Lines changed: 12 additions & 11 deletions

File tree

sqlglot/generators/presto.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ class PrestoGenerator(generator.Generator):
389389
exp.SHA1Digest: rename_func("SHA1"),
390390
exp.SHA2: sha256_sql,
391391
exp.SHA2Digest: sha2_digest_sql,
392+
exp.Substring: rename_func("SUBSTR"),
392393
}
393394

394395
RESERVED_KEYWORDS = {

tests/dialects/test_dialect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ def test_time(self):
951951
write={
952952
"duckdb": "SUBSTRING(CAST(x AS TEXT), 1, 10)",
953953
"hive": "SUBSTRING(CAST(x AS STRING), 1, 10)",
954-
"presto": "SUBSTRING(CAST(x AS VARCHAR), 1, 10)",
954+
"presto": "SUBSTR(CAST(x AS VARCHAR), 1, 10)",
955955
"doris": "SUBSTRING(CAST(x AS STRING), 1, 10)",
956956
},
957957
)
@@ -2403,7 +2403,7 @@ def test_operators(self):
24032403
self.validate_all(
24042404
"STR_POSITION(haystack, needle, position)",
24052405
write={
2406-
"athena": "IF(STRPOS(SUBSTRING(haystack, position), needle) = 0, 0, STRPOS(SUBSTRING(haystack, position), needle) + position - 1)",
2406+
"athena": "IF(STRPOS(SUBSTR(haystack, position), needle) = 0, 0, STRPOS(SUBSTR(haystack, position), needle) + position - 1)",
24072407
"bigquery": "INSTR(haystack, needle, position)",
24082408
"clickhouse": "POSITION(haystack, needle, position)",
24092409
"databricks": "LOCATE(needle, haystack, position)",
@@ -2415,7 +2415,7 @@ def test_operators(self):
24152415
"mysql": "LOCATE(needle, haystack, position)",
24162416
"oracle": "INSTR(haystack, needle, position)",
24172417
"postgres": "CASE WHEN POSITION(needle IN SUBSTRING(haystack FROM position)) = 0 THEN 0 ELSE POSITION(needle IN SUBSTRING(haystack FROM position)) + position - 1 END",
2418-
"presto": "IF(STRPOS(SUBSTRING(haystack, position), needle) = 0, 0, STRPOS(SUBSTRING(haystack, position), needle) + position - 1)",
2418+
"presto": "IF(STRPOS(SUBSTR(haystack, position), needle) = 0, 0, STRPOS(SUBSTR(haystack, position), needle) + position - 1)",
24192419
"redshift": "CASE WHEN POSITION(needle IN SUBSTRING(haystack FROM position)) = 0 THEN 0 ELSE POSITION(needle IN SUBSTRING(haystack FROM position)) + position - 1 END",
24202420
"risingwave": "CASE WHEN POSITION(needle IN SUBSTRING(haystack FROM position)) = 0 THEN 0 ELSE POSITION(needle IN SUBSTRING(haystack FROM position)) + position - 1 END",
24212421
"snowflake": "CHARINDEX(needle, haystack, position)",
@@ -2424,7 +2424,7 @@ def test_operators(self):
24242424
"sqlite": "IIF(INSTR(SUBSTRING(haystack, position), needle) = 0, 0, INSTR(SUBSTRING(haystack, position), needle) + position - 1)",
24252425
"tableau": "IF FIND(SUBSTRING(haystack, position), needle) = 0 THEN 0 ELSE FIND(SUBSTRING(haystack, position), needle) + position - 1 END",
24262426
"teradata": "INSTR(haystack, needle, position)",
2427-
"trino": "IF(STRPOS(SUBSTRING(haystack, position), needle) = 0, 0, STRPOS(SUBSTRING(haystack, position), needle) + position - 1)",
2427+
"trino": "IF(STRPOS(SUBSTR(haystack, position), needle) = 0, 0, STRPOS(SUBSTR(haystack, position), needle) + position - 1)",
24282428
"tsql": "CHARINDEX(needle, haystack, position)",
24292429
},
24302430
)
@@ -2438,10 +2438,10 @@ def test_operators(self):
24382438
write={
24392439
"bigquery": "INSTR(haystack, needle, position, occurrence)",
24402440
"oracle": "INSTR(haystack, needle, position, occurrence)",
2441-
"presto": "IF(STRPOS(SUBSTRING(haystack, position), needle, occurrence) = 0, 0, STRPOS(SUBSTRING(haystack, position), needle, occurrence) + position - 1)",
2441+
"presto": "IF(STRPOS(SUBSTR(haystack, position), needle, occurrence) = 0, 0, STRPOS(SUBSTR(haystack, position), needle, occurrence) + position - 1)",
24422442
"tableau": "IF FINDNTH(SUBSTRING(haystack, position), needle, occurrence) = 0 THEN 0 ELSE FINDNTH(SUBSTRING(haystack, position), needle, occurrence) + position - 1 END",
24432443
"teradata": "INSTR(haystack, needle, position, occurrence)",
2444-
"trino": "IF(STRPOS(SUBSTRING(haystack, position), needle, occurrence) = 0, 0, STRPOS(SUBSTRING(haystack, position), needle, occurrence) + position - 1)",
2444+
"trino": "IF(STRPOS(SUBSTR(haystack, position), needle, occurrence) = 0, 0, STRPOS(SUBSTR(haystack, position), needle, occurrence) + position - 1)",
24452445
},
24462446
)
24472447
self.validate_all(

tests/dialects/test_hive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def test_hive(self):
701701
"LOCATE('a', x, 3)",
702702
write={
703703
"duckdb": "CASE WHEN STRPOS(SUBSTRING(x, 3), 'a') = 0 THEN 0 ELSE STRPOS(SUBSTRING(x, 3), 'a') + 3 - 1 END",
704-
"presto": "IF(STRPOS(SUBSTRING(x, 3), 'a') = 0, 0, STRPOS(SUBSTRING(x, 3), 'a') + 3 - 1)",
704+
"presto": "IF(STRPOS(SUBSTR(x, 3), 'a') = 0, 0, STRPOS(SUBSTR(x, 3), 'a') + 3 - 1)",
705705
"hive": "LOCATE('a', x, 3)",
706706
"spark": "LOCATE('a', x, 3)",
707707
},

tests/dialects/test_presto.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_time(self):
328328
"DATE_PARSE(SUBSTR(x, 1, 10), '%Y-%m-%d')",
329329
write={
330330
"duckdb": "STRPTIME(SUBSTRING(x, 1, 10), '%Y-%m-%d')",
331-
"presto": "DATE_PARSE(SUBSTRING(x, 1, 10), '%Y-%m-%d')",
331+
"presto": "DATE_PARSE(SUBSTR(x, 1, 10), '%Y-%m-%d')",
332332
"hive": "CAST(SUBSTRING(x, 1, 10) AS TIMESTAMP)",
333333
"spark": "TO_TIMESTAMP(SUBSTRING(x, 1, 10), 'yyyy-MM-dd')",
334334
},
@@ -337,7 +337,7 @@ def test_time(self):
337337
"DATE_PARSE(SUBSTRING(x, 1, 10), '%Y-%m-%d')",
338338
write={
339339
"duckdb": "STRPTIME(SUBSTRING(x, 1, 10), '%Y-%m-%d')",
340-
"presto": "DATE_PARSE(SUBSTRING(x, 1, 10), '%Y-%m-%d')",
340+
"presto": "DATE_PARSE(SUBSTR(x, 1, 10), '%Y-%m-%d')",
341341
"hive": "CAST(SUBSTRING(x, 1, 10) AS TIMESTAMP)",
342342
"spark": "TO_TIMESTAMP(SUBSTRING(x, 1, 10), 'yyyy-MM-dd')",
343343
},
@@ -867,7 +867,7 @@ def test_presto(self):
867867
self.validate_all("(5 * INTERVAL '7' DAY)", read={"": "INTERVAL '5' WEEK"})
868868
self.validate_all("(5 * INTERVAL '7' DAY)", read={"": "INTERVAL '5' WEEKS"})
869869
self.validate_all(
870-
"SELECT SUBSTRING(a, 1, 3), SUBSTRING(a, LENGTH(a) - (3 - 1))",
870+
"SELECT SUBSTR(a, 1, 3), SUBSTR(a, LENGTH(a) - (3 - 1))",
871871
read={
872872
"redshift": "SELECT LEFT(a, 3), RIGHT(a, 3)",
873873
},

tests/dialects/test_spark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def test_spark(self):
875875
"SELECT LEFT(x, 2), RIGHT(x, 2)",
876876
write={
877877
"duckdb": "SELECT LEFT(x, 2), RIGHT(x, 2)",
878-
"presto": "SELECT SUBSTRING(x, 1, 2), SUBSTRING(x, LENGTH(x) - (2 - 1))",
878+
"presto": "SELECT SUBSTR(x, 1, 2), SUBSTR(x, LENGTH(x) - (2 - 1))",
879879
"hive": "SELECT SUBSTRING(x, 1, 2), SUBSTRING(x, LENGTH(x) - (2 - 1))",
880880
"spark": "SELECT LEFT(x, 2), RIGHT(x, 2)",
881881
},

0 commit comments

Comments
 (0)