Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(clickhouse): add missing type mappings for string types #2183

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ class Tokenizer(tokens.Tokenizer):
"INT32": TokenType.INT,
"INT64": TokenType.BIGINT,
"INT8": TokenType.TINYINT,
"LONGBLOB": TokenType.LONGBLOB,
"LONGTEXT": TokenType.LONGTEXT,
georgesittas marked this conversation as resolved.
Show resolved Hide resolved
"LOWCARDINALITY": TokenType.LOWCARDINALITY,
"MAP": TokenType.MAP,
"MEDIUMBLOB": TokenType.MEDIUMBLOB,
"MEDIUMINT": TokenType.INT,
"MEDIUMTEXT": TokenType.MEDIUMTEXT,
"NESTED": TokenType.NESTED,
"TINYBLOB": TokenType.TINYBLOB,
"TINYTEXT": TokenType.TINYTEXT,
"TUPLE": TokenType.STRUCT,
"UINT128": TokenType.UINT128,
"UINT16": TokenType.USMALLINT,
Expand Down
66 changes: 38 additions & 28 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,19 @@ class TestClickhouse(Validator):
dialect = "clickhouse"

def test_clickhouse(self):
self.validate_all(
"DATE_ADD('day', 1, x)",
read={
"clickhouse": "dateAdd(day, 1, x)",
"presto": "DATE_ADD('day', 1, x)",
},
write={
"clickhouse": "DATE_ADD('day', 1, x)",
"presto": "DATE_ADD('day', 1, x)",
"": "DATE_ADD(x, 1, 'day')",
},
)
self.validate_all(
"DATE_DIFF('day', a, b)",
read={
"clickhouse": "dateDiff('day', a, b)",
"presto": "DATE_DIFF('day', a, b)",
},
write={
"clickhouse": "DATE_DIFF('day', a, b)",
"presto": "DATE_DIFF('day', a, b)",
"": "DATEDIFF(b, a, day)",
},
)
string_types = [
"BLOB",
"LONGBLOB",
"LONGTEXT",
"MEDIUMBLOB",
"MEDIUMTEXT",
"TINYBLOB",
"TINYTEXT",
"VARCHAR(255)",
]

for string_type in string_types:
self.validate_identity(f"CAST(x AS {string_type})", "CAST(x AS String)")

expr = parse_one("count(x)")
self.assertEqual(expr.sql(dialect="clickhouse"), "COUNT(x)")
Expand Down Expand Up @@ -72,10 +61,7 @@ def test_clickhouse(self):
self.validate_identity("position(haystack, needle)")
self.validate_identity("position(haystack, needle, position)")
self.validate_identity("CAST(x AS DATETIME)")
self.validate_identity("CAST(x AS VARCHAR(255))", "CAST(x AS String)")
self.validate_identity("CAST(x AS BLOB)", "CAST(x AS String)")
self.validate_identity("CAST(x AS TINYBLOB)", "CAST(x AS String)")
self.validate_identity("CAST(x AS TINYTEXT)", "CAST(x AS String)")

self.validate_identity(
'SELECT CAST(tuple(1 AS "a", 2 AS "b", 3.0 AS "c").2 AS Nullable(String))'
)
Expand All @@ -95,6 +81,30 @@ def test_clickhouse(self):
"CREATE MATERIALIZED VIEW test_view (id UInt8) TO db.table1 AS SELECT * FROM test_data"
)

self.validate_all(
"DATE_ADD('day', 1, x)",
read={
"clickhouse": "dateAdd(day, 1, x)",
"presto": "DATE_ADD('day', 1, x)",
},
write={
"clickhouse": "DATE_ADD('day', 1, x)",
"presto": "DATE_ADD('day', 1, x)",
"": "DATE_ADD(x, 1, 'day')",
},
)
self.validate_all(
"DATE_DIFF('day', a, b)",
read={
"clickhouse": "dateDiff('day', a, b)",
"presto": "DATE_DIFF('day', a, b)",
},
write={
"clickhouse": "DATE_DIFF('day', a, b)",
"presto": "DATE_DIFF('day', a, b)",
"": "DATEDIFF(b, a, day)",
},
)
self.validate_all(
"SELECT xor(1, 0)",
read={
Expand Down