Skip to content

[CALCITE-7523] Support the syntax SELECT * REPLACE(expr as column)#4939

Open
xuzifu666 wants to merge 12 commits into
apache:mainfrom
xuzifu666:repace_ck_syntax
Open

[CALCITE-7523] Support the syntax SELECT * REPLACE(expr as column)#4939
xuzifu666 wants to merge 12 commits into
apache:mainfrom
xuzifu666:repace_ck_syntax

Conversation

@xuzifu666
Copy link
Copy Markdown
Member

Comment thread babel/src/main/codegen/config.fmpp Outdated
Comment thread babel/src/test/java/org/apache/calcite/test/BabelTest.java
Comment thread site/_docs/reference.md Outdated
Note:

* `SELECT * EXCLUDE (...)` is recognized only when the Babel parser is enabled. It sets the generated parser configuration flag `includeStarExclude` to `true` (the standard parser leaves that flag `false`), which allows a `STAR` token followed by `EXCLUDE` (or the alias `EXCEPT`) and a parenthesized identifier list to be parsed into a `SqlStarExclude` node and ensures validators respect the exclusion list when expanding the projection. Reusing the same parser configuration elsewhere enables the same syntax for other components that need it.
* `SELECT * REPLACE (...)` is recognized only when the Babel parser is enabled. It sets the generated parser configuration flag `includeStarReplace` to `true` (the standard parser leaves that flag `false`), which allows a `STAR` token followed by `REPLACE` and a parenthesized list of `expression AS column` pairs to be parsed into a `SqlStarReplace` node and ensures validators substitute the specified expressions for the matching columns when expanding the projection. The column alias must either be a simple identifier or, for a table-qualified star such as `t.*`, a qualified identifier whose prefix matches the star's table alias. Reusing the same parser configuration elsewhere enables the same syntax for other components that need it.
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 don't understand exactly the last sentence. Does it even belong in the reference? Seems to be more useful for developers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did some changes here to make it more clear.

Comment thread core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java Outdated
Comment thread core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java Outdated
Comment thread babel/src/test/java/org/apache/calcite/test/BabelTest.java
}
final SqlNode replacement =
findReplacement(columnName, replaceMap, resolvedNameMatcher);
if (replacement != null) {
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.

if this check fails is there an error thrown later?
Then please document this after the if

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Had document this.

@xuzifu666 xuzifu666 requested a review from mihaibudiu May 14, 2026 04:39
});

// Same column replaced twice: the last one wins
fixture.withSql("select * replace(empno + 1 as empno, 'fixed' as empno) from emp")
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.

is this the semantics in other systems?
I find this a bit dangerous, I expected it to be an error
But we should emulate what others do

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That makes sense! I agree with this point. My tests on relevant databases (including ClickHouse https://fiddle.clickhouse.com/1f06c0d0-9704-4611-bd03-e5c31cd4e4fe) show that not support multiple replace statements. I've modified the output here to show an error. Thanks for the suggestion.

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 have already approved

@sonarqubecloud
Copy link
Copy Markdown

@xuzifu666
Copy link
Copy Markdown
Member Author

If there no other comments, I will squash and merge it in next 48h.

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.

2 participants