-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[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
base: master
Are you sure you want to change the base?
Conversation
@@ -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) |
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.
To explain this, the analyze() function in this class for strings does the following
- analyze/ checkAnalyze
- constant fold
- 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( |
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.
Some test cleanup, I didnt realize multiple ALTER COLUMN can be the same statement, so it was unnecessarily long
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionBase.scala
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionBase.scala
Show resolved
Hide resolved
column.newCurrentDefault() == expectedDefault, | ||
s"Default value mismatch for column '${column.toString}': " + | ||
s"expected $expectedDefault but found ${column.newCurrentDefault()}") | ||
private def checkDefaultValues( |
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.
I guess you could define new checkDefaultValues
using checkDefaultValue
to minimize changes.
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.
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)
a6750dd
to
38717a6
Compare
rebased |
@cloud-fan can you take a look at this one? Thanks ! test failure is unrelated, re-running. |
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala
Show resolved
Hide resolved
38717a6
to
b1995d7
Compare
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) => |
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.
to be safe, can we match all exceptions here? If anything gets wrong we just fail and say the type is incompatible.
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.
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 => |
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.
case ex: Exception => | |
case NonFatal(e) => |
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