← Back to Pull requests

[SPARK-60978][SQL] Fix wrong results by DSv2 DELETE, UPDATE and MERGE when column names differ only by case #60155

Closed
unknown closed this pull request without merging on Nov 06, 2026 06:49 UTC
View on GitHub
unknown unknown commented on Nov 06, 2026 02:21 UTC
Author

What changes were proposed in this pull request?

This PR aims to fix a correctness issue of DSv2 row-level DELETE, UPDATE and MERGE. RewriteRowLevelCommand.findColOrdinal now looks for the column with exactly the given name first, and falls back to conf.resolver only when there is no such column.

The fallback is still needed because a connector may refer to a column in a different case, e.g. the row ID pk of a column PK. V2ExpressionUtils.resolveRef gives the resolved attribute the name of the reference.

Why are the changes needed?

This is a correctness issue. When a table has columns whose names differ only by case, such as salary and SALARY, an UPDATE, a MERGE or a group-based DELETE in a case-insensitive session, which is the default, writes the value of one column into the other without any error. The statement does not need to refer to these columns, so nothing is ambiguous.

RewriteRowLevelCommand builds the row projections of ReplaceData and WriteDelta by looking up the ordinal of each table column in the query output by name. findColOrdinal returned the first column that conf.resolver matches, so SALARY got the ordinal of salary. For example, the row projection of the UPDATE below uses the ordinals [1, 2, 2, 4] instead of [1, 2, 3, 4].

// spark.sql.catalog.cat=org.apache.spark.sql.connector.catalog.InMemoryRowLevelOperationTableCatalog
// Create the table in a case-sensitive session.
spark.conf.set("spark.sql.caseSensitive", "true")
sql("CREATE TABLE cat.ns1.t (pk INT NOT NULL, salary INT, SALARY INT, dep STRING) PARTITIONED BY (dep)")
sql("INSERT INTO cat.ns1.t VALUES (1, 100, 200, 'hr'), (2, 300, 400, 'hr')")
spark.conf.set("spark.sql.caseSensitive", "false")

// Issue 1: It should keep `SALARY`, but the table ends up with (3, 100, 100, hr) and (2, 300, 300, hr).
sql("UPDATE cat.ns1.t SET pk = 3 WHERE pk = 1")

// Issue 2: On the same initial table, it should keep (2, 300, 400, hr), but the table ends up with
// (2, 300, 300, hr).
sql("DELETE FROM cat.ns1.t WHERE pk <= 1")

// Issue 3: On the same initial table, it should keep `SALARY`, but the table ends up with
// (1, 100, 100, x) and (2, 300, 300, hr).
Seq(1).toDF("pk").createOrReplaceTempView("src")
sql("MERGE INTO cat.ns1.t t USING src s ON t.pk = s.pk WHEN MATCHED THEN UPDATE SET dep = 'x'")

The results above are read with spark.sql.caseSensitive=true.

  • Group-based operations write wrong values into both the updated rows and the rows that they copy over. Delta-based operations write wrong values into the updated rows.

  • Delta-based DELETE writes no data columns, and an insert-only MERGE does not use these projections, so they are not affected.

  • If SALARY is NOT NULL and salary is nullable, the UPDATE fails with a misleading INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS error instead.

  • The lookup was added by SPARK-43324 (v3.5.0, 2023-09-09) for delta-based operations. SPARK-50820 (v4.0.0, 2025-05-19) started to use it for group-based operations.
  • Does this PR introduce _any_ user-facing change?

    Yes. An UPDATE, a MERGE or a group-based DELETE on a table whose column names differ only by case now writes each column's own value, and the NOT NULL case above succeeds. If such columns also differ in type or nullability, the scan side needs SPARK-60979 (#60156) as well. This is a long-standing issue in the released versions, including v4.3.0 (2026-10-27).

    How was this patch tested?

    Pass the CIs with newly added test cases.

  • SPARK-60978: update keeps columns whose names differ only by case fails without this PR in the five UPDATE suites.

  • SPARK-60978: delete keeps columns whose names differ only by case fails without this PR in the group-based DELETE suites.

  • SPARK-60978: merge keeps columns whose names differ only by case fails without this PR in every MERGE suite.

  • SPARK-60978: update with a row ID column whose name differs in case checks the fallback to conf.resolver in the delta-based UPDATE suites. It fails if findColOrdinal matches exact names only.
  • $ build/sbt 'sql/testOnly *BasedUpdateTableSuite *BasedNoMetadataUpdateTableSuite *DeltaBasedUpdateAsDeleteAndInsertTableSuite *BasedDeleteFromTableSuite *BasedNoMetadataDeleteFromTableSuite *BasedMergeIntoTableSuite *BasedNoMetadataMergeIntoTableSuite *DeltaBasedMergeIntoTableUpdateAsDeleteAndInsertSuite *MergeIntoDataFrameSuite *BasedRowLevelOperationCatalystRuntimeFilterSuite'
    $ build/sbt 'sql/testOnly *BasedMergeIntoSchemaEvolution*Suite *AlignUpdateAssignmentsSuite *AlignMergeAssignmentsSuite'
    

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Opus 5.5

    Diff: 629 lines (+471 / -158) across 7 files Reviewer: spark-test Review duration: 268m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 629 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #60155"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.8% · Safe to merge
    21.8%
    Repository