Skip to content

Commit 3cd9529

Browse files
authored
[Spark] Add CREATE TABLE LIKE compatibility with user-provided table properties (#3138)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [x] Spark - [ ] Standalone - [ ] Flink - [ ] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> User provided properties when performing CREATE LIKE commands were being ignored and only the properties from source table were being added. This PR adds/overwrites any applicable properties with the user provided ones. ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> Unit tests were created replicating the customer issue for CREATE LIKE commands both originating in Delta tables and other formats. ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> No
1 parent ff5b36f commit 3cd9529

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

spark/src/main/scala/org/apache/spark/sql/delta/DeltaAnalysis.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class DeltaAnalysis(session: SparkSession)
160160
tableType = tblType,
161161
storage = newStorage,
162162
schema = sourceMetadata.schema,
163-
properties = config,
163+
properties = config ++ ctl.properties,
164164
partitionColumnNames = sourceMetadata.partitionColumns,
165165
provider = Some("delta"),
166166
comment = Option(sourceMetadata.description)
@@ -171,7 +171,7 @@ class DeltaAnalysis(session: SparkSession)
171171
tableType = tblType,
172172
storage = newStorage,
173173
schema = src.schema,
174-
properties = src.properties,
174+
properties = src.properties ++ ctl.properties,
175175
partitionColumnNames = src.partitionColumnNames,
176176
provider = Some("delta"),
177177
comment = src.comment

spark/src/test/scala/org/apache/spark/sql/delta/DeltaCreateTableLikeSuite.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,40 @@ class DeltaCreateTableLikeSuite extends QueryTest
397397
checkTableCopyDelta(srcTbl, targetTbl)
398398
}
399399
}
400+
401+
test("CREATE TABLE LIKE where user explicitly provides table properties") {
402+
val srcTbl = "srcTbl"
403+
val targetTbl = "targetTbl"
404+
val expectedTbl = "expectedTbl"
405+
withTable(srcTbl, targetTbl, expectedTbl) {
406+
createTable(srcTbl, addTblProperties = false)
407+
createTable(expectedTbl, addTblProperties = false)
408+
spark.sql(s"ALTER TABLE $srcTbl" +
409+
" SET TBLPROPERTIES(this.is.my.key = 14, 'this.is.my.key2' = false," +
410+
"'delta.appendOnly' = 'false')")
411+
spark.sql(s"ALTER TABLE $expectedTbl" +
412+
" SET TBLPROPERTIES(this.is.my.key = 14, 'this.is.my.key2' = false, " +
413+
"'this.is.my.key3' = true, 'delta.appendOnly' = 'true')")
414+
spark.sql(s"CREATE TABLE $targetTbl LIKE $srcTbl TBLPROPERTIES('this.is.my.key3' = true, " +
415+
s"'delta.appendOnly' = 'true')")
416+
checkTableCopyDelta(expectedTbl, targetTbl)
417+
}
418+
}
419+
420+
test("CREATE TABLE LIKE where sourceTable is a parquet table and " +
421+
"user explicitly provides table properties"
422+
) {
423+
val srcTbl = "srcTbl"
424+
val targetTbl = "targetTbl"
425+
withTable(srcTbl, targetTbl) {
426+
createTable(srcTbl, format = "parquet")
427+
spark.sql(s"CREATE TABLE $targetTbl LIKE $srcTbl USING DELTA " +
428+
"TBLPROPERTIES('this.is.my.key3' = true)")
429+
spark.sql(s"ALTER TABLE $srcTbl SET TBLPROPERTIES('this.is.my.key3' = true)")
430+
val msg = intercept[TestFailedException] {
431+
checkTableCopy(srcTbl, targetTbl, checkDesc = false)
432+
}.getMessage
433+
assert(msg.contains("provider does not match"))
434+
}
435+
}
400436
}

0 commit comments

Comments
 (0)