-
Notifications
You must be signed in to change notification settings - Fork 122
Name struct columns from parenthesised-star expressions after their source #4182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3f7e9eb
13909cf
ecfea29
b6e3c30
08f75ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,7 +59,8 @@ | |||||||||||
| import com.apple.foundationdb.relational.util.Assert; | ||||||||||||
| import com.google.common.collect.ImmutableList; | ||||||||||||
| import com.google.common.collect.ImmutableMap; | ||||||||||||
| import com.google.common.collect.ImmutableSet; | ||||||||||||
| import com.google.common.collect.Iterables; | ||||||||||||
| import com.google.common.collect.Streams; | ||||||||||||
| import com.google.protobuf.ZeroCopyByteString; | ||||||||||||
| import org.antlr.v4.runtime.ParserRuleContext; | ||||||||||||
|
|
@@ -891,12 +892,22 @@ | |||||||||||
| } else { | ||||||||||||
| final var star = getDelegate().getSemanticAnalyzer().expandStar(Optional.of(id), getDelegate().getLogicalOperators()); | ||||||||||||
| final var resultValue = star.getUnderlying(); | ||||||||||||
| return Expression.ofUnnamed(resultValue); | ||||||||||||
| // Name the column after the qualifier (table name or alias) that was expanded. | ||||||||||||
| return Expression.of(resultValue, id); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| if (ctx.STAR() != null) { | ||||||||||||
| final var star = getDelegate().getSemanticAnalyzer().expandStar(Optional.empty(), getDelegate().getLogicalOperators()); | ||||||||||||
| final var resultValue = star.getUnderlying(); | ||||||||||||
| // When there is exactly one table in scope, name the column after that table/alias. | ||||||||||||
| // With multiple tables the expansion is ambiguous, so fall back to an unnamed column. | ||||||||||||
| final var forEachOps = getDelegate().getLogicalOperators().forEachOnly(); | ||||||||||||
| if (Iterables.size(forEachOps) == 1) { | ||||||||||||
| final Optional<Identifier> tableName = Iterables.getOnlyElement(forEachOps).getName(); | ||||||||||||
| if (tableName.isPresent()) { | ||||||||||||
| return Expression.of(resultValue, tableName.get()); | ||||||||||||
|
Comment on lines
+906
to
+908
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| return Expression.ofUnnamed(resultValue); | ||||||||||||
| } | ||||||||||||
| final var expressions = parseRecordFieldsUnderReorderings(ctx.expressionWithOptionalName()); | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,124 @@ | ||||||||||||||||
| # | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a test for
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure to add tests for PartiQL nesting vs. unnest syntax, something like: create type as struct s1(a bigint, b bigint)
create type as struct s2(c bigint, d s1 array)
create table t1(col1 bigint, col2 s2 array, primary key(col1))
select (*) from (select * from t1, t1.col2 as k, k.d as m) as p
select h.* from (select (*) from (select * from t1, t1.col2 as k, k.d as m) as h) and so on. |
||||||||||||||||
| # star-expression-metadata.yamsql | ||||||||||||||||
| # | ||||||||||||||||
| # This source file is part of the FoundationDB open source project | ||||||||||||||||
| # | ||||||||||||||||
| # Copyright 2015-2026 Apple Inc. and the FoundationDB project authors | ||||||||||||||||
| # | ||||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||||
| # | ||||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||
| # | ||||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||||
| # limitations under the License. | ||||||||||||||||
|
|
||||||||||||||||
| # Tests the result-set column names produced by parenthesised-star SELECT expressions. | ||||||||||||||||
| # | ||||||||||||||||
| # Parenthesised-star forms — SELECT (*) and SELECT (T.*) — pack the entire row into a | ||||||||||||||||
| # single struct-typed column. The outer column name is inferred as follows: | ||||||||||||||||
| # | ||||||||||||||||
| # SELECT (T.*) → column named T (explicit qualifier used as name) | ||||||||||||||||
| # SELECT (*) single source → column named after the sole table/alias/function in scope | ||||||||||||||||
| # SELECT (*) / (T.*) AS X → explicit alias overrides inferred name | ||||||||||||||||
| # SELECT (*) multi-table → column is anonymous ("_0") — source is ambiguous | ||||||||||||||||
| --- | ||||||||||||||||
| schema_template: | ||||||||||||||||
| create table foo(id bigint, val bigint, primary key(id)) | ||||||||||||||||
| create table bar(bid bigint, bval bigint, primary key(bid)) | ||||||||||||||||
| --- | ||||||||||||||||
| transaction_setups: | ||||||||||||||||
| tvf_sq: create temporary function sq(in x bigint) on commit drop function AS | ||||||||||||||||
| SELECT id, val FROM foo WHERE id > x | ||||||||||||||||
| --- | ||||||||||||||||
| setup: | ||||||||||||||||
| steps: | ||||||||||||||||
| - query: INSERT INTO foo VALUES (1, 10) | ||||||||||||||||
| - query: INSERT INTO bar VALUES (1, 20) | ||||||||||||||||
| --- | ||||||||||||||||
| test_block: | ||||||||||||||||
| name: table-source | ||||||||||||||||
| tests: | ||||||||||||||||
| - | ||||||||||||||||
| # Case 1: SELECT (*) FROM FOO → single table in scope → column named FOO | ||||||||||||||||
| - query: SELECT (*) FROM foo | ||||||||||||||||
| - resultMetadata: [{FOO: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{FOO: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # Case 2: SELECT (FOO.*) FROM FOO → qualifier is used as column name | ||||||||||||||||
| - query: SELECT (foo.*) FROM foo | ||||||||||||||||
| - resultMetadata: [{FOO: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{FOO: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # Case 3: SELECT (*) FROM FOO AS BAR → table alias in scope → column named BAR | ||||||||||||||||
| - query: SELECT (*) FROM foo AS bar | ||||||||||||||||
| - resultMetadata: [{BAR: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{BAR: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # Case 4: SELECT (BAR.*) FROM FOO AS BAR → qualifier (the alias) is used as column name | ||||||||||||||||
| - query: SELECT (bar.*) FROM foo AS bar | ||||||||||||||||
| - resultMetadata: [{BAR: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{BAR: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # Case 5: SELECT (*) AS BAR FROM FOO → explicit alias overrides inferred name | ||||||||||||||||
| - query: SELECT (*) AS bar FROM foo | ||||||||||||||||
| - resultMetadata: [{BAR: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{BAR: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # Case 6: SELECT (FOO.*) AS BAR FROM FOO → explicit alias overrides qualifier name | ||||||||||||||||
| - query: SELECT (foo.*) AS bar FROM foo | ||||||||||||||||
| - resultMetadata: [{BAR: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{BAR: {ID: 1, VAL: 10}}] | ||||||||||||||||
| --- | ||||||||||||||||
| test_block: | ||||||||||||||||
| name: subquery-source | ||||||||||||||||
| tests: | ||||||||||||||||
| - | ||||||||||||||||
| # Subquery with alias → column named after the alias | ||||||||||||||||
| - query: SELECT (*) FROM (SELECT id, val FROM foo) AS sub | ||||||||||||||||
| - resultMetadata: [{SUB: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{SUB: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # (T.*) where T is a subquery alias → same rule as for a table alias | ||||||||||||||||
| - query: SELECT (sub.*) FROM (SELECT id, val FROM foo) AS sub | ||||||||||||||||
| - resultMetadata: [{SUB: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{SUB: {ID: 1, VAL: 10}}] | ||||||||||||||||
| --- | ||||||||||||||||
| test_block: | ||||||||||||||||
| name: function-source | ||||||||||||||||
| tests: | ||||||||||||||||
| - | ||||||||||||||||
| # TVF without alias → column named after the function | ||||||||||||||||
| - query: SELECT (*) FROM sq(0) | ||||||||||||||||
| - setupReference: tvf_sq | ||||||||||||||||
| - resultMetadata: [{SQ: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{SQ: {ID: 1, VAL: 10}}] | ||||||||||||||||
| - | ||||||||||||||||
| # TVF with alias → alias wins | ||||||||||||||||
| - query: SELECT (*) FROM sq(0) AS f | ||||||||||||||||
| - setupReference: tvf_sq | ||||||||||||||||
| - resultMetadata: [{F: [{ID: BIGINT}, {VAL: BIGINT}]}] | ||||||||||||||||
| - result: [{F: {ID: 1, VAL: 10}}] | ||||||||||||||||
| --- | ||||||||||||||||
| test_block: | ||||||||||||||||
| name: values-source | ||||||||||||||||
| tests: | ||||||||||||||||
| - | ||||||||||||||||
| # VALUES without alias has no name → source is anonymous, column is _0 | ||||||||||||||||
| - query: SELECT (*) FROM VALUES (1, 2), (3, 4) | ||||||||||||||||
| - resultMetadata: [{_0: [{_0: INTEGER}, {_1: INTEGER}]}] | ||||||||||||||||
| - unorderedResult: [{_0: {_0: 1, _1: 2}}, {_0: {_0: 3, _1: 4}}] | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can also add a test for a named values source, I tested it locally and I think it works correctly
Suggested change
|
||||||||||||||||
| --- | ||||||||||||||||
| test_block: | ||||||||||||||||
| name: ambiguous-join-source | ||||||||||||||||
| tests: | ||||||||||||||||
| - | ||||||||||||||||
| # Two tables in scope → source is ambiguous, column is anonymous | ||||||||||||||||
| - query: SELECT (*) FROM foo, bar WHERE foo.id = bar.bid | ||||||||||||||||
| - resultMetadata: [{_0: [{ID: BIGINT}, {VAL: BIGINT}, {BID: BIGINT}, {BVAL: BIGINT}]}] | ||||||||||||||||
| - result: [{_0: {ID: 1, VAL: 10, BID: 1, BVAL: 20}}] | ||||||||||||||||
| ... | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is not very accurate, technically PartiQL expansions are also one-table expansion that end up with multiple for-each columns, which should be avoided you do, correctly so.
You can make the comment more accurate by stating that the we avoid all types of joins (standard and partiQL unnest) since their result include attributes originating from different quantifiers anyways.