Repository navigation
[spark] Fix MERGE INTO with merge-schema on VARCHAR/CHAR target columns - #11698
LuciferYang wants to merge 1 commit into
Conversation
…ns (apache#11401) When write.merge-schema is enabled, MERGE INTO a table that exposes a VARCHAR(n) or CHAR(n) column failed with: [INTERNAL_ERROR] Logical plan should not have output of char/varchar type Root cause: buildEvolvedOutput built the evolved DataSourceV2Relation output using fromPaimonRowType, which placed raw VarcharType(n)/CharType(n) on the AttributeReference. Spark's CheckAnalysis rejects any leaf relation with raw char/varchar output -- it expects StringType with the declared type kept in __CHAR_VARCHAR_TYPE_STRING metadata. Fix: - MergeSchemaEvolutionHelper.buildEvolvedOutput: apply CharVarcharUtils.replaceCharVarcharWithStringInSchema before mapping fields to attributes. For existing attrs, merge the stripped existing metadata with the transformed field metadata via MetadataBuilder so the char/varchar key is preserved when the type is unchanged and dropped when the column is widened to STRING. - RowLevelHelper: promote relationAttributeRewriter from PaimonUpdateTable (private) to RowLevelHelper (protected) so MERGE and UPDATE share one implementation. Restore the require size-equality guard. - PaimonUpdateTable: remove now-duplicate local definition. - PaimonMergeInto: update comment to reflect that alignment uses merge.targetTable.output (which includes the CHAR padding Project). New tests: - merge-schema adds a column to a target with a VARCHAR(4) column (also pins that the VARCHAR length limit is preserved after evolution) - merge-schema widens a VARCHAR column and adds a column (type-widening VARCHAR -> STRING end-to-end)
JingsongLi
left a comment
There was a problem hiding this comment.
I tested 5196c5d8ab merged with 055fe60215 on Spark 3.5.9. All 94 existing MERGE and UPDATE tests passed, including both new VARCHAR cases. An additional CHAR case failed.
[P2] Rebind the padded CHAR attribute before planning the V1 command. I created a primary-key target (a INT, b CHAR(4)) and a source (a INT, b STRING, c INT), then ran MERGE ON target.a = source.a WHEN MATCHED THEN UPDATE SET * WHEN NOT MATCHED THEN INSERT * with schema merge enabled and V2 writes disabled. The write fails in BindReferences with Couldn't find b#27159 in [a#27160,b#27161,c#27162,...]. The padding Project produces a separate attribute for b, while the aligned V1 expressions still use the bare relation attribute. The new shared rewriter is called by UPDATE only. Please wire the CHAR rebinding into the MERGE path and add this case alongside the two VARCHAR regressions. MERGE alignment
Purpose
Fixes #11401.
When
write.merge-schemais enabled,MERGE INTOa table that exposes aVARCHAR(n)orCHAR(n)column fails with:Root cause:
MergeSchemaEvolutionHelper.buildEvolvedOutputbuilt the evolvedDataSourceV2Relationoutput usingfromPaimonRowType, which placed rawVarcharType(n)/CharType(n)on theAttributeReference. Spark'sCheckAnalysisrejects any leaf relation with raw char/varchar output — it expectsStringTypewith the declared type kept in__CHAR_VARCHAR_TYPE_STRINGmetadata.Fix:
buildEvolvedOutput: applyCharVarcharUtils.replaceCharVarcharWithStringInSchemabefore mapping fields to attributes. For existing attrs, merge the stripped existing metadata with the transformed field metadata so the char/varchar key is preserved when the type is unchanged and dropped when the column is widened to STRING.RowLevelHelper: promoterelationAttributeRewriterfromPaimonUpdateTable(private) toRowLevelHelper(protected) so MERGE and UPDATE share one implementation. Restore therequiresize-equality guard.PaimonUpdateTable: remove the now-duplicate local definition.Tests
merge-schema adds a column to a target with a VARCHAR(4) column— pins the fix; also asserts the VARCHAR length limit still applies after evolution.merge-schema widens a VARCHAR column and adds a column— type-widening VARCHAR → STRING end-to-end, verified vialoadTable(...).rowType().Both fail on the unfixed code (CheckAnalysis aborts before any write). Verified across Spark 3.5/4.1/4.2/4.3 merge-into suites (all green) plus
spotless:check.Closes #11401.