Skip to content

[spark] Fix MERGE INTO with merge-schema on VARCHAR/CHAR target columns - #11698

Open
LuciferYang wants to merge 1 commit into
apache:masterfrom
LuciferYang:spark-merge-schema-varchar
Open

LuciferYang wants to merge 1 commit into
apache:masterfrom
LuciferYang:spark-merge-schema-varchar

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

Purpose

Fixes #11401.

When write.merge-schema is enabled, MERGE INTO a table that exposes a VARCHAR(n) or CHAR(n) column fails with:

[INTERNAL_ERROR] Logical plan should not have output of char/varchar type

Root cause: MergeSchemaEvolutionHelper.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:

  • buildEvolvedOutput: apply CharVarcharUtils.replaceCharVarcharWithStringInSchema before 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: promote relationAttributeRewriter from PaimonUpdateTable (private) to RowLevelHelper (protected) so MERGE and UPDATE share one implementation. Restore the require size-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 via loadTable(...).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.

…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 JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MERGE INTO with merge-schema fails on a target with a VARCHAR column

2 participants