Skip to content

[Bug]: View SELECT star dynamically changes columns after base-table DDL #26957

Closed

Description

@Ariznawlll

Description

MatrixOne stores the original SELECT * text for a View and expands the star again against the current base-table schema every time the View is queried. Adding, dropping, or renaming a base-table column therefore changes the View's result-column set after creation, while the persisted View catalog still reports the old columns.

This differs from MySQL, which expands * when the View is created and keeps that selected column set stable. The MatrixOne behavior can change result metadata and break INSERT ... SELECT or clients that prepared/scanned the original result shape.

Environment

  • Branch: main
  • Commit: b84a6627060539173f9ee243e8523792a3ad2200
  • Deployment: local standalone MatrixOne (etc/launch/launch.toml), 1 CN / 1 TN / 1 Log service, MySQL text protocol
  • Oracle: MySQL 8.0.45

Steps to reproduce

DROP DATABASE IF EXISTS view_star_dynamic;
CREATE DATABASE view_star_dynamic;
USE view_star_dynamic;

CREATE TABLE t(a INT PRIMARY KEY, b VARCHAR(5));
INSERT INTO t VALUES (1,'x'),(2,'yy');

CREATE VIEW v_star AS SELECT * FROM t;
CREATE VIEW v_explicit AS SELECT a,b FROM t;

ALTER TABLE t ADD COLUMN c INT DEFAULT 7;

DESC v_star;
SELECT * FROM v_star ORDER BY a;
SELECT c FROM v_star ORDER BY a;

CREATE TABLE target2(a INT,b VARCHAR(5));
INSERT INTO target2 SELECT * FROM v_star;
SELECT COUNT(*) FROM target2;

Actual behavior

DESC v_star still reports the two creation-time columns a,b, but query binding re-expands the stored star and exposes the new column:

SELECT * FROM v_star;
a  b   c
1  x   7
2  yy  7

SELECT c FROM v_star;
c
7
7

The two-column insert then fails and leaves the target empty:

ERROR 20301 (HY000): invalid input: insert values does not match the number of columns
COUNT(*)
0

SHOW CREATE VIEW continues to show CREATE VIEW v_star AS SELECT * FROM t, so the query-time re-expansion is persistent.

Expected behavior

MySQL 8.0.45 expands the star in the stored View definition:

CREATE VIEW v_star AS
SELECT t.a AS a,t.b AS b FROM t

After adding c, SELECT * FROM v_star still returns only a,b, SELECT c FROM v_star returns unknown-column error 1054, and INSERT INTO target2 SELECT * FROM v_star succeeds with two rows.

The View's executable result shape, DESC, information_schema.columns, CTAS, prepared execution, and INSERT SELECT must agree on the same stable column set.

Stability and controls

  • Reproducer: 3/3 fresh databases exposed a,b,c through a View whose catalog contained only a,b.
  • INSERT impact: 3/3 two-column INSERT ... SELECT * FROM v_star statements failed; every target remained at 0 rows.
  • CTAS impact: CREATE TABLE copied AS SELECT * FROM v_star created and stored all three visible columns, confirming the extra column reaches materialization rather than being a client-print artifact.
  • Explicit-column control: CREATE VIEW v_explicit AS SELECT a,b FROM t remained two columns and returned correct rows.
  • MySQL oracle: kept a,b, rejected direct reference to c, and inserted two rows into the two-column target.
  • Explicit View-column-list boundary: CREATE VIEW v_named(x,y) AS SELECT * FROM t becomes unusable after ADD COLUMN with error 1353 because the dynamically expanded star has three expressions for two persisted names; MySQL remains usable.
  • Nested View boundary: a View-of-View created before the ALTER also dynamically exposed the new column while retaining two-column catalog metadata.
  • DROP COLUMN boundary: MatrixOne dynamically removed the dropped column and continued querying the star View; MySQL marked the View invalid because its fixed selected column was removed.
  • No source data was modified by rejected inserts; the session and service remained usable with no panic, restart, or hang.

Evidence

The minimal reproducer, three fresh-database repetitions, MySQL oracle, explicit-column control, CTAS, nested View, explicit View column list, DROP COLUMN, and failure-atomicity checks were executed on 2026-08-11 against the exact commit above.

Code analysis

Confirmed path:

  • genViewTableDef in pkg/sql/plan/build_ddl.go binds the creation-time query and persists the two-column TableDef, but stores ctx.GetRootSql() unchanged in ViewData.Stmt.
  • QueryBuilder.bindView in pkg/sql/plan/query_builder.go parses that stored CREATE VIEW ... SELECT * ... again for every query and rebinds it against the current base-table schema.

The persisted schema and query-time schema can consequently diverge. The fix should preserve the creation-time bound output-column set for star projections while still allowing supported dependency changes to refresh the types/defaults/nullability of those selected columns.

Regression coverage

After the product fix, add public BVT coverage under test/distributed/cases/view/ for:

  • direct and nested SELECT * Views after base-table ADD COLUMN;
  • explicit View column lists;
  • ADD FIRST/AFTER ordering, RENAME, and DROP of selected/unselected columns;
  • DESC, information_schema.columns, direct SELECT, CTAS, and INSERT SELECT shape consistency;
  • prepared and ordinary execution after dependency DDL;
  • rejected DML atomicity and session recovery.

Do not baseline the current dynamically changed result shape.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 11, 2026
  2. added
    kind/bug
    on Aug 11, 2026
  3. added theissue type on Aug 11, 2026
  4. self-assigned this
    on Aug 11, 2026
  5. added a commit that references this issue on Aug 16, 2026
  6. Ariznawlll commented on Aug 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e32c7dc95946c347710e706e70dfee2927bc84e0
    • 部署方式:本地 standalone MatrixOne,1 CN / 1 TN / 1 Log,MySQL text protocol
    • 测试日期:2026-08-17

    原问题验证

    在 3 个全新数据库中创建 v_star AS SELECT * FROM t,随后 ALTER TABLE t ADD COLUMN c。3/3 次 DESC/SELECT * 仍只有创建时的 a,b,SHOW CREATE VIEW 已保存展开后的列,INSERT INTO target2 SELECT * FROM v_star 均成功并插入 2 行。

    Happy Path 覆盖

    1. ADD COLUMN 后直接 SELECT、DESC、information_schema.columns:列集合保持 a,b。
    2. CTAS、INSERT ... SELECT *、View-of-View:均保持两列并返回正确数据。
    3. ADD FIRST/AFTER、显式 View 列名、基表未选中列 DROP/RENAME:现有 BVT view_star_static_columns.sql 覆盖。

    Unhappy Path 覆盖

    1. 3/3 次 SELECT c FROM v_star 均按预期报 column c does not exist。
    2. DROP/RENAME 已选列后查询按预期失效;失败查询后目标表和会话未受污染。

    白盒验证

    go test -mod=readonly ./pkg/sql/plan -run TestGenViewTableDefPersistsExpandedStarSelectList -count=1:通过。

    回归测试

    已检查现有 BVT test/distributed/cases/view/view_star_static_columns.sql,覆盖直接/嵌套 View、CTAS、INSERT SELECT、ADD FIRST/AFTER、RENAME、DROP 和错误路径。

    测试证据

    • /private/tmp/mo-26957-1.txt、-2.txt、-3.txt
    • /private/tmp/mo-26957-extra-add.txt
    • /private/tmp/mo-26957-extra-rename.txt

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions