Skip to content

[SPARK-52236][SQL] Standardize analyze exceptions for default value #50960

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

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

Conversation

szehon-ho
Copy link
Contributor

What changes were proposed in this pull request?

Default values are checked for various things like resolved, and deterministic. Standardize the errors for various statements (CREATE TABLE/REPLACE TABLE/ALTER TABLE ADD COLUMNS/ALTER TABLE ALTER COLUMNS). This pr just fixes the ones that were not standard before.

Why are the changes needed?

Cleanup some exceptions and messages. This is from the comment of https://github.com/apache/spark/pull/50864/files#r2087361821

Does this PR introduce any user-facing change?

No

How was this patch tested?

Add test in DatasourceV2SQLSuite

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

@github-actions github-actions bot added the SQL label May 20, 2025
}
}
}

case cmd: AddColumns if cmd.columnsToAdd.exists(_.default.isDefined) =>
// Wrap analysis errors for default values in a more user-friendly message.
val statement = "ALTER TABLE ADD COLUMNS"
Copy link
Contributor Author

@szehon-ho szehon-ho May 20, 2025

Choose a reason for hiding this comment

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

cmd.specs.foreach { c =>
c.newDefaultExpression.foreach { d =>
// Eagerly check resolved, as accessing datatype requires it to be resolved
Copy link
Contributor Author

@szehon-ho szehon-ho May 21, 2025

Choose a reason for hiding this comment

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

the AlterColumnSpec object doesnt have a type (unlike the others). If i dont eagerly check here check if d is resolved, the d.dataType call to get the argument for validateDefaultValueExpr() itself throws another UnresolvedError, in the cases where the default value is an unresvoled expression like 'badvalue'

Copy link
Contributor

Choose a reason for hiding this comment

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

how about we make the targetType parameter of def validateDefaultValueExpr to be Option[DataType]? For AlterColumns command, we don't really need to check type castability, as we use the expression's own data type as the target type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, done

@@ -3576,7 +3612,7 @@ class DataSourceV2SQLSuiteV1Filter
s"REPLACE TABLE tab (col1 DOUBLE DEFAULT rand()) USING $v2Source")
assert(exception.getSqlState == "42623")
assert(exception.errorClass.get == "INVALID_DEFAULT_VALUE.NON_DETERMINISTIC")
assert(exception.messageParameters("statement") == "CREATE TABLE")
assert(exception.messageParameters("statement") == "REPLACE TABLE")
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an error message bug fix, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea , my mistake before :)

@@ -1170,7 +1170,7 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
exception = intercept[AnalysisException] {
sql("create table t(i boolean, s bigint default badvalue) using parquet")
},
condition = "INVALID_DEFAULT_VALUE.NOT_CONSTANT",
condition = "INVALID_DEFAULT_VALUE.UNRESOLVED_EXPRESSION",
Copy link
Member

Choose a reason for hiding this comment

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

A good improvement.

Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

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

+1, LGTM. Thank you, @szehon-ho .

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.

3 participants