-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56054][SQL] Undo handling aliased assignments in MERGE INTO schema evolution and add tests #55239
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
base: master
Are you sure you want to change the base?
[SPARK-56054][SQL] Undo handling aliased assignments in MERGE INTO schema evolution and add tests #55239
Changes from all commits
c68d16d
8bd48ed
87bcfa0
1f51e18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.connector | ||
|
|
||
| import org.apache.spark.sql.{AnalysisException, Row} | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Util | ||
| import org.apache.spark.sql.functions.col | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * DataFrame API-only tests for MERGE INTO schema evolution with aliased assignments. | ||
| * These tests use the `.as()` DataFrame method which has no SQL equivalent, so they only run | ||
| * in the Scala/DataFrame API test suites. | ||
| */ | ||
| trait MergeIntoSchemaEvolutionExtraScalaTests extends MergeIntoSchemaEvolutionSuiteBase { | ||
|
|
||
| private def withNestedTestData(body: => Unit): Unit = { | ||
| withTable(tableNameAsString) { | ||
| withTempView("source") { | ||
| val targetSchema = StructType(Seq( | ||
| StructField("pk", IntegerType, nullable = false), | ||
| StructField("info", StructType(Seq( | ||
| StructField("a", IntegerType) | ||
| ))) | ||
| )) | ||
| createTable(CatalogV2Util.structTypeToV2Columns(targetSchema), Seq.empty) | ||
| val targetDf = spark.createDataFrame(spark.sparkContext.parallelize(Seq( | ||
| Row(1, Row(10)), | ||
| Row(2, Row(20)) | ||
| )), targetSchema) | ||
| targetDf.writeTo(tableNameAsString).append() | ||
|
|
||
| val sourceSchema = StructType(Seq( | ||
| StructField("pk", IntegerType, nullable = false), | ||
| StructField("info", StructType(Seq( | ||
| StructField("a", IntegerType), | ||
| StructField("b", IntegerType) | ||
| ))) | ||
| )) | ||
| val sourceDf = spark.createDataFrame(spark.sparkContext.parallelize(Seq( | ||
| Row(2, Row(30, 50)), | ||
| Row(3, Row(40, 75)) | ||
| )), sourceSchema) | ||
| sourceDf.createOrReplaceTempView("source") | ||
|
|
||
| body | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("schema evolution - top-level aliased struct column is not evolved") { | ||
| withNestedTestData { | ||
| val ex = intercept[AnalysisException] { | ||
| spark.table("source") | ||
| .mergeInto(tableNameAsString, | ||
| col(s"$tableNameAsString.pk") === col("source.pk")) | ||
| .whenMatched().update(Map("info" -> col("source.info").as("info"))) | ||
| .withSchemaEvolution() | ||
| .merge() | ||
| } | ||
| assert(ex.getCondition === "INCOMPATIBLE_DATA_FOR_TABLE.EXTRA_STRUCT_FIELDS") | ||
| } | ||
| } | ||
|
|
||
| // Same as above with a mismatched alias name. | ||
| test("schema evolution - top-level aliased struct column with mismatched name is not evolved") { | ||
| withNestedTestData { | ||
| val ex = intercept[AnalysisException] { | ||
| spark.table("source") | ||
| .mergeInto(tableNameAsString, | ||
| col(s"$tableNameAsString.pk") === col("source.pk")) | ||
| .whenMatched().update(Map( | ||
| "info" -> col("source.info").as("something_else"))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does Delta behave in this case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delta allows schema evolution here, but only because it allows arbitrary expressions on the right hand side of the assignment. |
||
| .withSchemaEvolution() | ||
| .merge() | ||
| } | ||
| assert(ex.getCondition === "INCOMPATIBLE_DATA_FOR_TABLE.EXTRA_STRUCT_FIELDS") | ||
| } | ||
| } | ||
|
|
||
| test("schema evolution - nested field through aliased struct column is not evolved") { | ||
| withNestedTestData { | ||
| val ex = intercept[AnalysisException] { | ||
| spark.table("source") | ||
| .mergeInto(tableNameAsString, | ||
| col(s"$tableNameAsString.pk") === col("source.pk")) | ||
| .whenMatched().update(Map( | ||
| "info.b" -> col("source.info").as("x").getField("b"))) | ||
| .withSchemaEvolution() | ||
| .merge() | ||
| } | ||
| assert(ex.getCondition === "UNRESOLVED_COLUMN.WITH_SUGGESTION") | ||
| } | ||
| } | ||
|
|
||
| test("schema evolution - complex expression value is not considered for evolution") { | ||
| withNestedTestData { | ||
| val ex = intercept[AnalysisException] { | ||
| spark.table("source") | ||
| .mergeInto(tableNameAsString, | ||
| col(s"$tableNameAsString.pk") === col("source.pk")) | ||
| .whenMatched().update(Map( | ||
| "info.b" -> (col("source.info.b") + 1).as("b"))) | ||
| .withSchemaEvolution() | ||
| .merge() | ||
| } | ||
| assert(ex.getCondition === "UNRESOLVED_COLUMN.WITH_SUGGESTION") | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, this seems like a behavior change compared to the initial PR then? The code in master would evolve the schema in this case because we had that alias support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. The main motivation for the initial PR adding support for alias was to support nested assignments
SET col.x = s.col.x, which translates toAssignment("col.x", Alias(GetStructField("col", "x"), "x")I've noticed in the meantime that Spark actually strips the alias on struct field access after resolution (Delta doesn't).
We don't have a strong argument to support the remaining cases where the user provides an explicit alias using the dataframe API, so I'd rather not allow these