Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3661,20 +3661,23 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
object ResolveOutputRelation extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning(
_.containsPattern(COMMAND), ruleId) {
case v2Write: V2WriteCommand
if v2Write.table.resolved && v2Write.query.resolved && !v2Write.outputResolved =>
case v2Write: V2WriteCommand =>
validateStoreAssignmentPolicy()
TableOutputResolver.suitableForByNameCheck(v2Write.isByName,
expected = v2Write.table.output, queryOutput = v2Write.query.output)
val projection = TableOutputResolver.resolveOutputColumns(
v2Write.table.name, v2Write.table.output, v2Write.query, v2Write.isByName, conf)
if (projection != v2Write.query) {
val cleanedTable = v2Write.table match {
case r: DataSourceV2Relation =>
r.copy(output = r.output.map(CharVarcharUtils.cleanAttrMetadata))
case other => other
if (v2Write.table.resolved && v2Write.query.resolved && !v2Write.outputResolved) {
TableOutputResolver.suitableForByNameCheck(v2Write.isByName,
expected = v2Write.table.output, queryOutput = v2Write.query.output)
val projection = TableOutputResolver.resolveOutputColumns(
v2Write.table.name, v2Write.table.output, v2Write.query, v2Write.isByName, conf)
if (projection != v2Write.query) {
val cleanedTable = v2Write.table match {
case r: DataSourceV2Relation =>
r.copy(output = r.output.map(CharVarcharUtils.cleanAttrMetadata))
case other => other
}
v2Write.withNewQuery(projection).withNewTable(cleanedTable)
} else {
v2Write
}
v2Write.withNewQuery(projection).withNewTable(cleanedTable)
} else {
v2Write
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,18 @@ class DataFrameWriterV2Suite extends QueryTest with SharedSparkSession with Befo
condition = "CALL_ON_STREAMING_DATASET_UNSUPPORTED",
parameters = Map("methodName" -> "`writeTo`"))
}

test("SPARK-49975: write should fail with legacy store assignment policy") {
withSQLConf((SQLConf.STORE_ASSIGNMENT_POLICY.key, "LEGACY")) {
checkError(
exception = intercept[AnalysisException] {
spark.range(10)
.writeTo("table_name")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the behavior before? Do we add hard cast for the input query?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes on master branch. I was wrong about what's allowed with LEGACY store assignment policy.

The following is allowed with either SQL or DataFrameWriteV2 API when source has the same schema as v2Table.

insert into v2Table select * from source;
val df = spark.table("source")
df.writeTo("v2Table").append()

Copy link
Contributor

@cloud-fan cloud-fan Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so the current logic is: if Spark needs to adjust the input query before writing to the table (type coercion), then we disallow LEGACY store assignment. Otherwise, we don't check store assignment mode at all. This is consistent across all APIs and seems fine?

.append()
},
condition = "_LEGACY_ERROR_TEMP_1000",
parameters = Map("configKey" -> "spark.sql.storeAssignmentPolicy")
)
}
}
}