richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586835635
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala:
##########
@@ -84,9 +84,16 @@ object ResolveDefaultColumns extends QueryErrorsBase
if (SQLConf.get.enableDefaultColumns) {
val newFields: Seq[StructField] = tableSchema.fields.map { field =>
if (field.metadata.contains(CURRENT_DEFAULT_COLUMN_METADATA_KEY)) {
- val analyzed: Expression = analyze(field, statementType)
+ val defaultSql: String = if
(field.dataType.isInstanceOf[VariantType]) {
+ // A variant's SQL/string representation is its JSON string which
cannot be directly
+ // casted to a variant type. Thus, we lazily evaluate the default
expression to avoid
Review Comment:
> shouldn't it be sql text parse_json('{'k': 'v'}')? I am confusing...
ah sorry, I skipped a few steps: when we create a table (or alter table add
column) and add a column default, `constantFoldCurrentDefaultsToExistDefaults`
[here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L81)
is called to constant fold the expression and save it as a sql expression
string under the `EXISTS_DEFAULT_COLUMN_METADATA_KEY` metadata key.
So for `parse_json('{'k': 'v'}')` the string `{'k': 'v'}` (no quotes) is
saved under the `EXISTS_DEFAULT_COLUMN_METADATA_KEY` key. Later on, in
`getExistenceDefaultValues`
[here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L387)
we retrieve the sql string in `EXISTS_DEFAULT_COLUMN_METADATA_KEY` and run
`analyze` on it again to get the actual sql expression used as the default
value. Now, `EXISTS_DEFAULT_COLUMN_METADATA_KEY` is `{'k': 'v'}` (no outer
quotations) and throws an analysis error.
> it would be great if we can unify the code path here
Happy to be wrong, but I personally think this approach is actually cleaner
than the alternative (wrapping `{'k': 'v'}` with parse_json). This is because
the alternative would require us to do something like:
```
val defaultSql: String = if (field.dataType.isInstanceOf[VariantType]) {
analyzed match {
case l: ExprLiteral if l.value == null => l.sql
// A variant's SQL/string representation is its JSON string
which cannot be directly
// casted to a variant type. Thus, `analyzed` is wrapped with
`parse_json` so the
// default value is transformed into the correct variant.
case l: ExprLiteral =>
ParseJson(ExprLiteral(UTF8String.fromString(analyzed.sql),
StringType)).sql
case c: Cast => c.sql
}
} else {
analyzed.sql
}
```
```
val defaultSql: String = if (field.dataType.isInstanceOf[VariantType]) {
// A variant's SQL/string representation is its JSON string
which cannot be directly
// casted to a variant type. Thus, we lazily evaluate the
default expression to avoid
// materializing a string representation of the variant.
field.metadata.getString(CURRENT_DEFAULT_COLUMN_METADATA_KEY)
} else {
analyze(field, statementType).sql
}
```
i.e. we would rely on `VariantValue.toString` to always return a json string
(which may or may not change in the future), wrap that as a string literal, and
input it to `parse_json`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]