Skip to content

Commit

Permalink
Handle ADD COLUMN with DEFAULT for spark dialect
Browse files Browse the repository at this point in the history
See WRONG_COLUMN_DEFAULTS_FOR_DELTA_ALTER_TABLE_ADD_COLUMN_NOT_SUPPORTED and WRONG_COLUMN_DEFAULTS_FOR_DELTA_FEATURE_NOT_ENABLED error classes in Databricks for details.
  • Loading branch information
Gennadiy Anisimov committed Jan 4, 2024
1 parent bd72fb4 commit dc80704
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ spark,(SELECT DISTINCT TOP @([0-9]+)rows @a),(SELECT DISTINCT @a LIMIT @rows)
spark,"WITH @cte AS (SELECT @s1 '@literal' @s2)","WITH @cte AS (SELECT @s1 CAST('@literal' as STRING) @s2)"
spark,UPDATE STATISTICS @a;,
spark,"SELECT @columns FROM (@a) @x,(@b) @y;","SELECT @columns FROM (@a) @x cross join (@b) @y;"
spark,"ALTER TABLE @table ADD COLUMN @([\w_-]+)column @type DEFAULT @default;","ALTER TABLE @table ADD COLUMN @column @type; \nALTER TABLE @table SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported'); \nALTER TABLE @table ALTER COLUMN @column SET DEFAULT @default;"
sqlite extended,"IIF(@condition, @whentrue, @whenfalse)","CASE WHEN @condition THEN @whentrue ELSE @whenfalse END"
sqlite extended,"ROUND(@a,@b)","ROUND(CAST(@a AS REAL),@b)"
sqlite extended,+ '@a',|| '@a'
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-translate-spark.R
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,8 @@ test_that("translate sql server -> spark temp table field ref", {
sql <- translate("SELECT #tmp.name FROM #tmp;", targetDialect = "spark", tempEmulationSchema = "ts")
expect_equal_ignore_spaces(sql, sprintf("SELECT %stmp.name FROM ts.%stmp;", getTempTablePrefix(), getTempTablePrefix()))
})

test_that("translate sql server -> spark add column with default", {
sql <- translate("ALTER TABLE mytable ADD COLUMN mycol int DEFAULT 0;", targetDialect = "spark")
expect_equal_ignore_spaces(sql, "ALTER TABLE mytable ADD COLUMN mycol int; \n ALTER TABLE mytable SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported'); \n ALTER TABLE mytable ALTER COLUMN mycol SET DEFAULT 0;")
})

0 comments on commit dc80704

Please sign in to comment.