Skip to content

[SPARK-52235][SQL] Add implicit cast to DefaultValue V2 Expressions passed to DSV2 #50959

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 2 commits into
base: master
Choose a base branch
from

Conversation

szehon-ho
Copy link
Contributor

@szehon-ho szehon-ho commented May 20, 2025

What changes were proposed in this pull request?

Add implicit cast to DefaultValue V2 expressions passed to DSV2, by adding rule to TypeCoercion.

Why are the changes needed?

Now default values are passed as V2 Expressions to DSV2 in Create/Replace/Alter table DDL statements.

We should match what the normal default value analysis path does for sql strings (ie, implicit cast).

Does this PR introduce any user-facing change?

No

How was this patch tested?

Add unit test to DatasourceV2DataFrameSuite

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

No

@github-actions github-actions bot added the SQL label May 20, 2025
@@ -432,18 +432,34 @@ object ResolveDefaultColumns extends QueryErrorsBase
targetType: DataType,
colName: String): Option[Expression] = {
expr match {
case l: Literal if !Seq(targetType, l.dataType).exists(_ match {
case l: Literal => defaultValueFromWiderType(l, targetType, colName)
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.

To explain this, the analyze() function in this class for strings does the following

  1. analyze/ checkAnalyze
  2. constant fold
  3. cast

However, because we add the rule in TypeCoercion (Analyzer phase), it runs before constant folding. Hence, relaxing the cast code to also take Expression instead of Literal.


val alterExecCol3 = executeAndKeepPhysicalPlan[AlterTableExec] {
sql(s"ALTER TABLE $tableName ALTER COLUMN salary SET DEFAULT CAST(0 AS BOOLEAN)")
sql(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some test cleanup, I didnt realize multiple ALTER COLUMN can be the same statement, so it was unnecessarily long

@@ -81,6 +87,66 @@ abstract class TypeCoercionBase extends TypeCoercionHelper {
}
}

object DefaultValueExpressionCoercion extends Rule[LogicalPlan] {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment for this new rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

column.newCurrentDefault() == expectedDefault,
s"Default value mismatch for column '${column.toString}': " +
s"expected $expectedDefault but found ${column.newCurrentDefault()}")
private def checkDefaultValues(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you could define new checkDefaultValues using checkDefaultValue to minimize changes.

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