Repository navigation
fix view select star - #26964
fix view select star#26964
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep review at exact head 87a41a6836f5b9d53f3029debab83d2805a586a0 found correctness and performance blockers:
-
SAMPLE(*)is classified as an ordinary star byselectExprHasStar, butstableViewSQLWithExpandedStarsreplaces the entire select list withexpandedSelectList. This drops the sampling operator from the persisted view definition. I reproduced withCREATE VIEW v_sample AS SELECT SAMPLE(*, 100 PERCENT) FROM nation: the stored statement becomescreate view v_sample as select nation.n_nationkey, ... from nation, so subsequent reads execute an ordinary full projection rather than the requested sample. Star expansion must preserve the surroundingSampleExprsemantics (or exclude this syntax from this rewrite until it can be transformed correctly), with a create/rebind regression. -
The fix does not cover stars under a top-level set operation.
bindSelectreturns directly from the*tree.UnionClausecase before assigningctx.expandedSelectList, andviewSelectHasStardoes not traverseUnionClause. I reproducedCREATE VIEW v_union AS SELECT * FROM nation UNION ALL SELECT * FROM nation;ViewData.Stmtstill contains both stars. Adding a base-table column therefore still changes that view schema (or can make unequal branches fail). Please make the rewrite structurally cover every result-producing branch/set-op and test ALTER TABLE after UNION/UNION ALL views. -
ctx.expandedSelectList = cloneTreeSelectExprs(selectList)runs unconditionally for every SELECT query block, including normal queries and nested subqueries, and reflectively deep-clones even explicit expression lists that will never be used. That adds allocation/CPU to the global planner hot path for a CREATE/ALTER VIEW-only feature. Gate capture on an explicit view-DDL builder option/callback so ordinary SELECT planning pays no cost; add an allocation/benchmark check if needed. -
The BVT failures are deterministic consequences of the patch, not environment noise. At least
database/create_table_like.sql, threepessimistic_transaction/clone/clone.sqlassertions, statement-query-type cases, and information_schema view-definition assertions still expect the old stored/show definition. Decide the compatibility contract, then update all affected fixtures (and avoid unrelated whole-file output-format churn). Both standalone pessimistic and compose proxy BVT must be clean.
The core direction—freezing ordinary view wildcard outputs at creation—is correct, but the rewrite boundary must preserve every non-star operator, cover set operations, and avoid taxing all SELECTs.
|
Thanks for the review. Fixed in commit
Validation:
The new BVT checks are running on the pushed head. |
aptend
left a comment
There was a problem hiding this comment.
Re-review of 503792bdbe49479894d076c5e2ff12289205a4c6: the earlier SAMPLE/UNION/hot-path/CTE issues are addressed, but one blocking AST-coverage gap remains.
[P1] Expand stars inside expression subqueries before persisting the view SQL. The current detection/rewrite walks select lists, WITH branches, unions, and FROM table expressions, but it does not descend through expression nodes in the projection/predicates (and similarly other expression-bearing clauses). On this exact head I reproduced both:
create view v_nested as select (select * from one_col) as xcreate view v_nested as select n_nationkey from nation where n_nationkey in (select * from one_col)
with a one-column one_col table. Both successfully create the view and persist the literal * in ViewData.Stmt. After the source acquires another column, the scalar/IN subquery can return two columns, so the saved view no longer has stable semantics and may fail to rebind with “operand should contain 1 column”. Please extend star detection plus clone/rewrite through subquery expressions in all expression-bearing query positions, and add create/alter/rebind regressions for these cases.
Validation: focused view-star tests pass; full ./pkg/sql/plan passes under the repository CGo/thirdparties environment. The two temporary counterexamples above fail because the persisted SQL still contains *.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of exact head 048d5cbc9c3a00c08d192a30632e4a4868bf25ab: the prior SAMPLE/UNION/hot-path/CTE and SELECT/WHERE expression-subquery blockers are fixed, but one AST-coverage correctness gap remains.\n\n[P1] Rewrite subqueries inside JOIN ... ON conditions before persisting the view. tableExprHasStar and viewTableExprWithExpandedStars recurse through JoinTableExpr.Left and .Right, but neither inspects or clones/replaces JoinTableExpr.Cond (OnJoinCond.Expr). Therefore a wildcard reachable only through ON is not detected and the entire saved SQL remains dynamic.\n\nI reproduced on this exact head with a one-column one_col table:\n\nsql\nCREATE VIEW v_join_star AS\nSELECT n.n_nationkey\nFROM nation n JOIN region r\n ON n.n_regionkey = r.r_regionkey\n AND n.n_nationkey IN (SELECT * FROM one_col);\n\n\nViewData.Stmt is persisted verbatim with SELECT *. After adding a second column to one_col, reparsing and binding that persisted definition fails with invalid input: subquery returns more than 1 column. Thus the view is still schema-unstable even though the equivalent SELECT-list and WHERE shapes are now covered.\n\nPlease traverse and rewrite the ON expression (while preserving USING/NATURAL conditions), and add CREATE/ALTER plus post-ALTER rebind regressions. It is also worth auditing other expression-bearing TableExpr implementations, especially table-function arguments, so this does not become another one-clause-at-a-time fix.\n\nValidation: all existing focused view-star tests and the full ./pkg/sql/plan package pass; the temporary exact-head counterexample above fails only at the expected post-schema-change rebind.
|
@XuPeng-SH addressed the remaining P1 from the latest review in commit Changes made:
Local validation:
The branch has also been merged with latest |
XuPeng-SH
left a comment
There was a problem hiding this comment.
[P1] Preserve ordinary view wildcards when a different subtree contains SAMPLE(*).
On exact head 15af818, stableViewSQLWithExpandedStars returns the original SQL whenever viewSelectHasSampleStar(stmt) is true anywhere in the statement (pkg/sql/plan/build_ddl.go:289). That statement-wide guard prevents stabilization of unrelated ordinary wildcards.
I reproduced it with:
create view v_outer as select * from nation where exists (select sample(*, 100 percent) from region)
BuildPlan succeeds, but the persisted ViewData.Stmt still contains select * from nation. The nested SAMPLE expression does not define the outer output list, so adding a column to nation can still change the stored view shape, which is the bug this PR is intended to prevent.
Please keep SAMPLE() nodes intact while independently expanding ordinary wildcards in other select blocks, rather than disabling the whole rewrite. Add a create/alter/rebind regression combining an outer *, a nested SAMPLE(), and a base-table column addition. The latest JOIN ON and table-function traversal fixes are otherwise in the right direction.
iamlinjunhong
left a comment
There was a problem hiding this comment.
Reviewed exact head d296db0ce93e17fe326a372dcc356ae94e4412f0 against merge-base/origin main 1f9e501594509fb5d2e0284833faf8c3426e2285. Two P1 correctness gaps remain in the view-star stabilization closure: (1) a SELECT block mixing an ordinary wildcard with SAMPLE skips capture for the whole block; (2) ROLLUP/CUBE/grouping-set expansion returns before the original SELECT clause is captured. Both leave supported CREATE VIEW definitions with dynamic ordinary stars, so a later base-table column addition can change the view shape or make rebinding fail. No separate P2/P3 findings. Requesting changes.
iamlinjunhong
left a comment
There was a problem hiding this comment.
Reviewed exact head d4effd2bc95384e7bd95c8feca398a3e33b4bdfc against merge-base/origin main 1f9e501594509fb5d2e0284833faf8c3426e2285. The two P1s from my previous review—mixed ordinary-star/SAMPLE capture and the grouping-set early return—are fixed. One P1 correctness gap remains: a SAMPLE(*) output is still persisted as a wildcard and re-expands when the source schema changes, so the view's executable shape can diverge from its creation-time catalog shape. No separate P2/P3 findings. Requesting changes.
| } | ||
| expandedSelectList, ok := expandedSelectLists[selectStmt] | ||
| if ok { | ||
| if selectClauseHasSampleExpr(selectStmt) && !selectClauseHasOrdinaryStar(selectStmt) { |
There was a problem hiding this comment.
[P1] Freeze the columns inside SAMPLE(*) instead of keeping this query block's source AST verbatim. For example, create t(a,b), then CREATE VIEW v AS SELECT SAMPLE(*, 100 PERCENT) FROM t, and add t.c. The persisted statement still contains SAMPLE(*); bindView reparses and binds that statement on every use (query_builder.go:10051,10114), so the sample projection grows from two columns to three while the view catalog still has the creation-time two columns. The mixed SELECT *, SAMPLE(*) path has the same problem because cloneViewSelectExprsForView puts the original sample wildcard back. The current BVT only executes count(*) after the ALTER, which cannot detect this output-shape drift. Please preserve the sampling operator but replace its star with the captured creation-time column list, then cover SELECT * FROM v and rejection of the newly added column after source DDL.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-review of exact head d4effd2bc95384e7bd95c8feca398a3e33b4bdfc. The previous outer-star/nested-SAMPLE issue is fixed, but one blocking traversal hole remains.
[P1] Stabilize subquery stars inside SAMPLE's column expressions.
SampleExpr.columns is unexported (pkg/sql/parsers/tree/expr.go:1850), and SampleExpr.Accept does not visit it (:1876-1881). Both reflective passes introduced here silently skip fields for which CanInterface() is false (pkg/sql/plan/build_ddl.go:529-533 and :1006-1011). Therefore a subquery nested in a SAMPLE column expression is neither detected nor rewritten.
I reproduced this with the exact-head service (and separately with a focused planner test):
CREATE TABLE one_col(id INT);
CREATE TABLE src(k INT);
CREATE VIEW v AS
SELECT sample((SELECT * FROM one_col UNION ALL SELECT 1), 1 rows)
FROM src;
SHOW CREATE VIEW v;
ALTER TABLE one_col ADD COLUMN extra INT;
EXPLAIN SELECT * FROM v;SHOW CREATE VIEW still contains SELECT * FROM one_col; after the legal base-table change, rebinding fails with SQL parser error: SELECT statements have different number of columns. Thus an accepted view definition remains schema-dependent, contrary to this PR's invariant.
Please make SAMPLE participate in the normal exhaustive AST child traversal/cloning contract (or handle GetColumns() explicitly in both passes), rather than relying on reflection that silently ignores private children. Add a create/show-create/rebind regression for this shape, including the post-ALTER TABLE failure condition.
|
Addressed in commit
Validation passed:
Please re-review the latest commit. |
aunjgr
left a comment
There was a problem hiding this comment.
Reviewed exact green head 0a5809b. The earlier CTE, expression-subquery, JOIN ON, grouping-set, mixed-star, and SAMPLE(*) persistence gaps are closed. The current capture/rewrite path freezes creation-time columns while preserving SAMPLE semantics, and the rebinding tests cover the relevant public boundaries. No blocking findings.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of exact head 0a5809b. The latest SAMPLE fix closes the private-child traversal blocker: SampleExpr now participates in visitor traversal, view capture freezes SAMPLE wildcard columns at CREATE/ALTER time while preserving the sampling operator, and both detection and cloned rewrite paths explicitly handle its private columns. I re-audited set operations, CTEs, derived and expression subqueries, JOIN ON, table functions, grouping sets, and top-level expression carriers. Focused exact-head parser and planner regressions passed; public create/alter/rebind BVT coverage is green; git diff --check is clean; all current-head checks are terminal without failures.
What type of PR is this?
Which issue(s) this PR fixes:
issue #26957
What this PR does / why we need it:
此分支主要修复 View SELECT * 的列集合不稳定问题。
修改内容:
创建/替换 View 时,如果 View body 使用 SELECT * / table.*,在 planner bind 阶段捕获已经展开后的 select-list。
持久化 View 时把 * 改写成显式列引用,例如:
.a,t.bfromt``同步更新 ViewData.Stmt 和 mo_catalog.mo_tables.rel_createsql,避免后续查询、SHOW CREATE、dump/restore 再按基表当前 schema 重新展开 *。
保留非 star View 的原始 SQL 行为不变。
增加反向保护:COUNT(*) 不会被误判为需要展开的 View 输出列。
补充 UT,验证:
补充 BVT,覆盖: