Skip to content

Commit

Permalink
Fix!(spark): create temporary table needs provider (#2322)
Browse files Browse the repository at this point in the history
* fix(spark) add tests for temp tables

* fix!(spark) Create Temporary Table needs provider
  • Loading branch information
dmoore247 authored Sep 26, 2023
1 parent 180cd8e commit dc7a6c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions sqlglot/dialects/spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,17 @@ def datediff_sql(self, expression: exp.DateDiff) -> str:
return self.func("DATEDIFF", unit, start, end)

return self.func("DATEDIFF", end, start)

def create_sql(self, expression: exp.Create) -> str:
kind = self.sql(expression, "kind").upper()
properties = expression.args.get("properties")
temporary = any(
isinstance(prop, exp.TemporaryProperty)
for prop in (properties.expressions if properties else [])
)
if kind == "TABLE" and temporary:
provider = exp.FileFormatProperty(this=exp.Literal.string("parquet"))
expression = expression.copy()
expression.args["properties"].append("expressions", provider)

return super().create_sql(expression)
1 change: 1 addition & 0 deletions tests/dialects/test_spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestSpark(Validator):
dialect = "spark"

def test_ddl(self):
self.validate_identity("CREATE TEMPORARY VIEW test AS SELECT 1")
self.validate_identity("CREATE TABLE foo (col VARCHAR(50))")
self.validate_identity("CREATE TABLE foo (col STRUCT<struct_col_a: VARCHAR((50))>)")
self.validate_identity("CREATE TABLE foo (col STRING) CLUSTERED BY (col) INTO 10 BUCKETS")
Expand Down
2 changes: 1 addition & 1 deletion tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def test_ddl(self):
self.validate_all(
"CREATE TABLE #mytemp (a INTEGER, b CHAR(2), c TIME(4), d FLOAT(24))",
write={
"spark": "CREATE TEMPORARY TABLE mytemp (a INT, b CHAR(2), c TIMESTAMP, d FLOAT)",
"spark": "CREATE TEMPORARY TABLE mytemp (a INT, b CHAR(2), c TIMESTAMP, d FLOAT) USING PARQUET",
"tsql": "CREATE TABLE #mytemp (a INTEGER, b CHAR(2), c TIME(4), d FLOAT(24))",
},
)
Expand Down

0 comments on commit dc7a6c2

Please sign in to comment.