Skip to content
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

[SPARK-50418][SQL] Allow an optional trailing comma at the end of SELECT lists #48961

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dtenedor
Copy link
Contributor

What changes were proposed in this pull request?

This PR extends the Spark SQL grammar to allow an optional trailing comma at the end of SELECT lists, and other named expression lists.

This behavior is controlled by the configuration spark.sql.optionalTrailingCommaInNamedExpressionLists (SQLConf.get.OPTIONAL_TRAILING_COMMA_IN_NAMED_EXPRESSION_LISTS). The new configuration is true by default as of its introduction in this PR.

For example, this query was not valid syntax before, but is now:

SELECT 1, 2, FROM t

Why are the changes needed?

After deliberation, we decided it was useful to have this support to help with migration from other legacy databases.

Does this PR introduce any user-facing change?

Yes, see above.

How was this patch tested?

This PR adds unit test cases with the configuration enabled and disabled.

Was this patch authored or co-authored using generative AI tooling?

No.

@dtenedor dtenedor marked this pull request as ready for review November 25, 2024 20:07
@github-actions github-actions bot added the SQL label Nov 25, 2024
@dtenedor
Copy link
Contributor Author

fix test
Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

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

to help with migration from other legacy databases.

Could you point out a couple such dbms in PR's description that support the feature, please.

@@ -5268,6 +5268,15 @@ object SQLConf {
.booleanConf
.createWithDefault(true)

val OPTIONAL_TRAILING_COMMA_IN_NAMED_EXPRESSION_LISTS =
buildConf("spark.sql.optionalTrailingCommaInNamedExpressionLists")
Copy link
Member

Choose a reason for hiding this comment

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

The config name confuses slightly. I would think if it is true, the trailing comma is optional otherwise is mandatory ;-).

Suggested change
buildConf("spark.sql.optionalTrailingCommaInNamedExpressionLists")
buildConf("spark.sql.allowTrailingCommaInNamedExpressionLists")

Copy link
Contributor

Choose a reason for hiding this comment

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

named expression list is not a user-facing concept, how about spark.sql.allowTrailingCommaInSelectList?

test("SPARK-50418: Support an optional trailing comma at the end of SELECT lists") {
withSQLConf(SQLConf.OPTIONAL_TRAILING_COMMA_IN_NAMED_EXPRESSION_LISTS.key -> "true") {
assertEqual("select 1, ", OneRowRelation().select(1))
assertEqual("select 1, 2", OneRowRelation().select(1, 2))
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's also test select 1, 2,

withSQLConf(SQLConf.OPTIONAL_TRAILING_COMMA_IN_NAMED_EXPRESSION_LISTS.key -> "false") {
assertEqual("select 1, 2", OneRowRelation().select(1, 2))
checkError(
exception = parseException("select 1,"),
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's also test select 1, 2,

@@ -425,7 +425,7 @@ org.apache.spark.sql.catalyst.parser.ParseException
"sqlState" : "42601",
"messageParameters" : {
"error" : "'BY'",
"hint" : ": extra input 'BY'"
"hint" : ""
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we lose the hint?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants