Skip to content

[Bug] Single-table UPDATE evaluates later assignments against pre-update values #27336

Closed

Description

@Lundomn

[Bug] Single-table UPDATE evaluates later assignments against pre-update values

Environment

  • Repository: matrixorigin/matrixone
  • Local reproduction commit from main: 621782d4634a36865bdb18c9fe3853bf70f43f1c
  • Runtime build: local MatrixOne build on macOS arm64
  • Client: Go MySQL driver over the MatrixOne MySQL protocol
  • SQL endpoint: embedded standalone MatrixOne test cluster
  • Test date: 2026-08-19

Describe the bug

For a single-table UPDATE, MatrixOne evaluates every right-hand-side expression against the original row. MySQL generally evaluates single-table assignments from left to right, so a later assignment can read the value produced by an earlier one.

This causes incorrect stored data for ordinary multi-column updates, including prepared statements and explicit transactions.

Steps to reproduce

CREATE DATABASE issue_update_assignment_order;
CREATE TABLE issue_update_assignment_order.t (
    id INT PRIMARY KEY,
    a INT,
    b INT,
    c INT
);
INSERT INTO issue_update_assignment_order.t VALUES (1, 1, 10, 100);

UPDATE issue_update_assignment_order.t
SET a = a + 1, b = a, c = b
WHERE id = 1;

SELECT a, b, c FROM issue_update_assignment_order.t WHERE id = 1;

Actual behavior

MatrixOne returns:

a | b | c
2 | 1 | 10

It evaluates both b = a and c = b using the row values before the UPDATE.

The same result was reproduced three times for the direct statement, a server-side prepared statement, and an explicit transaction. ROLLBACK restores the original row, so this is not transaction visibility or commit handling.

A swap assignment shows the same behavior:

CREATE TABLE t(a INT, b INT);
INSERT INTO t VALUES (1, 2);
UPDATE t SET a = b, b = a;
SELECT a, b FROM t;

MatrixOne returns (2,1).

Expected behavior

For a single-table UPDATE, later assignments should observe prior assignments in the same SET clause. The first reproduction should return:

a | b | c
2 | 2 | 2

The swap assignment should return (2,2).

MySQL documents this left-to-right behavior for single-table updates, including the equivalent example UPDATE t1 SET col1 = col1 + 1, col2 = col1: MySQL UPDATE documentation.

Source-level evidence

pkg/sql/plan/dml_context.go records update targets in updateCol2Expr, a map that does not retain the SQL SET order. In pkg/sql/plan/bind_update.go, assignments are planned from a projection of the original table row, so later right-hand-side expressions bind to pre-update column values.

Suggested fix

Preserve the source SET order for each target table. For a single-table update, plan assignments as sequential projections: each right-hand side reads the current result of preceding assignments, while a distinct original-row projection is retained for indexes, constraints, foreign keys, partitions, and row identifiers.

Multi-table UPDATE should remain a separate path because MySQL does not guarantee assignment order there.

Duplicate check

I searched MatrixOne issues and pull requests for UPDATE left to right, UPDATE assignment order, single-table UPDATE, a = a + 1, and b = a. No issue describing this table-row assignment-evaluation behavior was found. The superficially related closed issue #26685 concerns prepared session-variable assignments, not table-row update semantics.

Activity

  1. added
    kind/bug
    severity/s0
    on Aug 19, 2026
  2. reopened this on Sep 7, 2026
  3. assigned and unassigned on Sep 7, 2026
  4. Lundomn commented on Sep 7, 2026

    @Lundomn
    ContributorAuthor

    QA 转测(2026-09-07)

    • 修复 PR:PR #27337(已 merged)
    • 根因与修复范围:单表 UPDATE 的 SET 表达式未按 MySQL 兼容语义左到右求值,后续 assignment 读取旧值;PR 保留单表顺序投影,并不改变 multi-target/UPDATE FROM 的同时赋值。
    • 开发侧验证:端到端回归覆盖 chained/swap assignment、prepared、事务和 volatile expression,并更新相关 BVT 结果。
    • QA 请重点覆盖:单表 chained/swap assignment、prepared/text protocol、事务、volatile expression、multi-target UPDATE 与 UPDATE FROM 对照。

    请在本 Issue 记录实际测试版本/环境、通过或失败结论、未覆盖项及补测要求。按流程,QA 明确通过后再关闭 Issue;如发现失败或回归请保持 open 并反馈复现信息。

  5. slyunnn commented on Sep 8, 2026

    @slyunnn

    Regression and extended verification for #27336 (2026-09-08)

    Verification Environment

    • MatrixOne baseline: latest origin/main at b2f0f3fe69b23cde495c59d226c647aba555cfdc — refactor: define canonical compute-group execution and harden ingress-owned scheduling (#26109).
    • Related fix: PR #27337, fix: evaluate single-table UPDATE assignments left to right, merge commit 71c9eb37d3cbc5025c227460a0b1223462ac6734; it is an ancestor of the verified origin/main.
    • Topology: fresh local 1 LogService + 1 TN + 1 CN deployment, with no Proxy and one CN frontend at 127.0.0.1:26001.
    • Three test endpoints/entry points: direct mysql client at 127.0.0.1:26001; focused embedded Go MySQL-driver test; MOTR record/replay client at 127.0.0.1:26001. The direct and MOTR paths intentionally target the same physical CN in this single-CN topology.
    • Client protocol: MySQL TCP text protocol (mysql CLI and MOTR/PyMySQL); the focused upstream regression also exercises the MySQL driver against an embedded MatrixOne cluster.

    What Was Covered

    • OK: Original single-table chained assignment SET a = a + 1, b = a, c = b returned (2,2,2).
    • OK: Happy-path swap assignment returned (2,2) and repeated assignment SET a = a + 1, a = a + 1, b = a returned (3,3).
    • OK: Server-side PREPARE / EXECUTE preserved left-to-right assignment semantics.
    • OK: Transaction controls verified correct in-transaction values, ROLLBACK restoration, and COMMIT persistence.
    • OK: Volatile-expression control SET a = RAND(), b = a preserved the evaluated value (a = b).
    • OK: Compatibility controls confirmed simultaneous-assignment behavior remains for multi-target UPDATE and UPDATE ... FROM.
    • OK: Unhappy path: invalid qualified reference returned error code 20301 and left the target row unchanged.
    • Not covered: Multi-CN or Proxy routing; the verified environment has one CN and no Proxy. Binary prepared-statement protocol was not separately exercised because the defect is planner-level SQL SET ordering, and server-side prepared execution plus MySQL-protocol SQL coverage reached the same planner path.

    Findings

    The original regression is not reproducible on the current mainline. The single-table UPDATE now preserves source SET order while the explicitly excluded multi-target and UPDATE ... FROM forms retain their simultaneous-assignment behavior.

    Focused current-main validation passed: TestUpdateAssignmentEvaluationIsLeftToRight in ./pkg/tests/issues completed successfully via the MatrixOne CGo test wrapper (13.290s).

    MOTR / BVT Coverage

    Added and recorded:

    • suites/14_issue_regression/t/issue_27336_update_assignment_order.test
    • suites/14_issue_regression/r/issue_27336_update_assignment_order.result

    The case was recorded once and replayed twice successfully: 47 statements per replay, 47 passed, 0 failed. The regression commit is f36a7548413a28c3f6b6a9f7793139896c4eeb0c (test: add MOTR regression for #27336) on test/issue-27336-update-assignment-order, pushed to matrixorigin/motr via SSH.

    Large-data regression: not required. The defect is deterministic planner binding order within one target row and is independent of row count, block/batch boundaries, memory/spill, concurrency, data skew, network transport, or resource scale; larger data would not add relevant coverage.

    Final Conclusion

    Fixed. The mainline fix restores MySQL-compatible left-to-right SET evaluation for single-table UPDATE, while preserving the intended simultaneous semantics for multi-target UPDATE and UPDATE ... FROM. The single-CN result is fully verified; multi-CN/Proxy routing remains explicitly not covered.

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

Metadata

Metadata

Assignees

Labels

kind/bugphase/testingseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions