-
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
@@ -81,6 +87,66 @@ abstract class TypeCoercionBase extends TypeCoercionHelper { | |||
} | |||
} | |||
|
|||
object DefaultValueExpressionCoercion extends Rule[LogicalPlan] { |
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.
Could you add a comment for this new rule?
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.
added
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.
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