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 5 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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think i prefer to use the new method in all the place, as it makes the test significantly shorter (no need to check each change individually)

@szehon-ho szehon-ho force-pushed the cast_default_value branch from a6750dd to 38717a6 Compare May 27, 2025 22:23
@szehon-ho
Copy link
Contributor Author

rebased

@szehon-ho
Copy link
Contributor Author

@cloud-fan can you take a look at this one? Thanks ! test failure is unrelated, re-running.

@szehon-ho szehon-ho force-pushed the cast_default_value branch from 38717a6 to b1995d7 Compare May 29, 2025 05:34
try {
Option(casted.eval(EmptyRow)).map(Literal(_, targetType))
} catch {
case e @ ( _: SparkThrowable | _: RuntimeException) =>
logWarning(log"Failed to cast default value '${MDC(COLUMN_DEFAULT_VALUE, l)}' " +
case ex @ ( _: SparkThrowable | _: RuntimeException) =>
Copy link
Contributor

@cloud-fan cloud-fan May 29, 2025

Choose a reason for hiding this comment

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

to be safe, can we match all exceptions here? If anything gets wrong we just fail and say the type is incompatible.

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, thanks

try {
Option(casted.eval(EmptyRow)).map(Literal(_, targetType))
} catch {
case e @ ( _: SparkThrowable | _: RuntimeException) =>
logWarning(log"Failed to cast default value '${MDC(COLUMN_DEFAULT_VALUE, l)}' " +
case ex: Exception =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
case ex: Exception =>
case NonFatal(e) =>

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.

4 participants