You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "richardc-db (via GitHub)" <gi...@apache.org> on 2024/05/01 02:02:01 UTC
[PR] [SPARK-48067][SQL] Fix variant default columns [spark]
richardc-db opened a new pull request, #46312:
URL: https://github.com/apache/spark/pull/46312
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
lazily evaluates variant default column expressions. This is because Spark currently evaluates the default column expression and saves this as a string in the struct field's metadata. However, a variant's string representation is a JSONified string and cannot directly be coerced into a Variant. Thus, we lazily evaluate the expression so we do not need to store the variant string representation.
I am open to other implementation routes such as:
1. Wrap the output of `analyze` with a `ParseJson` expression. Thus, for an expression like `parse_json(format_string("%s", formatInput))`, the `format_string` expression will be evaluated like before. however, this will involve a variantToString -> parseJson, which feels like a waste.
2. Create a string-friendly representation of a variant, i.e. an encoding of the variant's binary encodings. This feels overly complicated if this is the only use case, though.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
3. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "dtenedor (via GitHub)" <gi...@apache.org>.
dtenedor commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586664236
##########
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]) {
Review Comment:
You are changing an OSS Spark file in DBR. You will need EDGE comments to indicate the part of the code that is different in DBR vs. OSS; please read go/dbr-devguide for instructions.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586783457
##########
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:
We could implement something like `variant{'key': 'value'}` instead of just `{'key': 'value'}` but I feel thats more or less what we have now. We'd still have to have redundant variant->string->variant conversions (for the sake of the default value metadata)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586697875
##########
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
Review Comment:
Hmm, i believe all default values are already stored as string sql expressions (usually literal expressions). I.e. the existing values (as a string sql expression) is retrieved [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L389) and is analyzed using `analyze` and eventually coerced into the target type [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L302). We could "coerce" the variant string into a variant by wrapping it with `parse_json` as mentioned in the PR description. However, this feels inefficient. We would evaluate the default expression to produce a variant, transform it to its string representation (by `expr.sql`) and then transform it back to its variant value later. Instead, we could just leave the expression as is and lazily evaluate it to avoid the extra variant-> strin
g and string -> variant conversions.
Regarding your other comment, I believe this doesn't work even if `ParseJson` can be constant folded (I quickly tried it) because `analyze` attempts to analyze the expression as a variant `{"k": "v"}`, which isn't valid sql syntax (there aren't even quotations around it). The root of the problem here is that there isn't a way to represent variant types as a literal
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "dtenedor (via GitHub)" <gi...@apache.org>.
dtenedor commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586662927
##########
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
Review Comment:
It sounds like we should implement the type coercion instead of doing this approach. Then we can correctly store the variant value in as the column default expression rather than a string.
##########
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]) {
Review Comment:
You are changing an OSS Spark file in DBR. You will need EDGE comments to indicate the part of the code that is different in DBR vs. OSS; please read go/dbr-devguide for instructions.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586798259
##########
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:
Yep, `parse_json('1')` works (even before this PR, but unintentionally). This is because the current code inserts a cast to variant, which is ok in this case because `CAST(1 as VARIANT)` succeeds
In the more complex case such as a default of `parse_json('{'k': 'v'}')`, the analyzer actually fails because it tries to analyze the sql text `{'k': 'v'}` (note the lack of outer quotations). An alternative to the taken approach in this PR is to create a string literal from `{'k': 'v'}` and wrap it with `parse_json` (effectively coercing it to the correct variant type). This feels inefficient, however, because we have variant->string->variant conversion rather than lazily evaluating it once
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586864884
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala:
##########
@@ -549,6 +549,7 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression {
s"${Literal(kv._1, mapType.keyType).sql}, ${Literal(kv._2, mapType.valueType).sql}"
}
s"MAP(${keysAndValues.mkString(", ")})"
+ case (v: VariantVal, variantType: VariantType) => s"PARSE_JSON('${v.toJson(timeZoneId)}')"
Review Comment:
Let's add some test cases for the change here.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586697875
##########
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
Review Comment:
Hmm, i believe all default values are already stored as string sql expressions (usually literal expressions). I.e. the existing values (as a string sql expression) is retrieved [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L389) and is analyzed using `analyze` and eventually coerced into the target type [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L302). We could "coerce" the variant string by wrapping it with `parse_json` as mentioned in the PR description. However, this feels inefficient. We would evaluate the default expression to produce a variant, transform it to its string representation (by `expr.sql`) and then transform it back to its variant value later. Instead, we could just leave the expression as is and lazily evaluate it to avoid the extra variant-> string and string ->
variant conversions.
Regarding your other comment, I believe this doesn't work even if `ParseJson` can be constant folded (I quickly tried it) because `analyze` attempts to analyze the expression as a variant `{"k": "v"}`, which isn't valid sql syntax (there aren't even quotations around it).
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang closed pull request #46312: [SPARK-48067][SQL] Fix variant default columns
URL: https://github.com/apache/spark/pull/46312
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586809054
##########
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:
> analyze the sql text {'k': 'v'}
shouldn't it be sql text `parse_json('{'k': 'v'}')`? I am confusing...
> create a string literal from {'k': 'v'} and wrap it with parse_json (effectively coercing it to the correct variant type). This feels inefficient, however, because we have variant->string->variant conversion rather than lazily evaluating it once
Since the `analyze` will perform `ConstantFolding`, it would be great if we can unify the code path here. The overhead is trivial here.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #46312:
URL: https://github.com/apache/spark/pull/46312#issuecomment-2089162196
Sync with @richardc-db offline. I suggest changing the `.sql` method of variant expressions instead.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on PR #46312:
URL: https://github.com/apache/spark/pull/46312#issuecomment-2091365435
Thanks, merging to master
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586697875
##########
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
Review Comment:
Hmm, i believe all default values are already stored as string sql expressions (usually literal expressions). I.e. the existing values (as a string sql expression) is retrieved [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L389) and is analyzed using `analyze` and eventually coerced into the target type [here](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L302). We could "coerce" the variant string by wrapping it with `parse_json` as mentioned in the PR description. However, this feels inefficient. We would evaluate the default expression to produce a variant, transform it to its string representation (by `expr.sql`) and then transform it back to its variant value later. Instead, we could just leave the expression as is and lazily evaluate it to avoid the extra variant-> string and string ->
variant conversions.
Regarding your other comment, I believe this doesn't work even if `ParseJson` can be constant folded (I quickly tried it) because `analyze` attempts to analyze the expression as a variant `{"k": "v"}`, which isn't valid sql syntax (there aren't even quotations around it). The root of the problem here is that there isn't a way to represent variant types as a literal
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586783457
##########
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:
We could implement something like `variant{'key': 'value'}` but I feel thats more or less what we have now. We'd still have to have redundant variant->string->variant conversions (for the sake of the default value metadata)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "dtenedor (via GitHub)" <gi...@apache.org>.
dtenedor commented on PR #46312:
URL: https://github.com/apache/spark/pull/46312#issuecomment-2088934443
```
previously we could not set a variant default column like
create table t(
v6 variant default parse_json('{\"k\": \"v\"}')
)
```
@richardc-db this would work if the `ParseJson` expression was constant-foldable. Is there a reason we cannot do that?
https://github.com/databricks/runtime/blob/bbc6e62658adfbce662a45190c9e755277743e84/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/variantExpressions.scala#L51
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586798259
##########
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:
Yep, `parse_json('1')` works (even before this PR). This is because the current code implicitly inserts a cast to variant, which is ok in this case because `CAST(1 as VARIANT)` succeeds
In the more complex case such as a default of `parse_json('{'k': 'v'}')`, the analyzer actually fails because it tries to analyze the sql text `{'k': 'v'}` (note the lack of outer quotations). An alternative to the taken approach in this PR is to create a string literal from `{'k': 'v'}` and wrap it with `parse_json` (effectively coercing it to the correct variant type). This feels inefficient, however, because we have variant->string->variant conversion rather than lazily evaluating it once
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586798259
##########
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:
Yep, `parse_json('1')` works (even before this PR). This is because the current code inserts a cast to variant, which is ok in this case because `CAST(1 as VARIANT)` succeeds
In the more complex case such as a default of `parse_json('{'k': 'v'}')`, the analyzer actually fails because it tries to analyze the sql text `{'k': 'v'}` (note the lack of outer quotations). An alternative to the taken approach in this PR is to create a string literal from `{'k': 'v'}` and wrap it with `parse_json` (effectively coercing it to the correct variant type). This feels inefficient, however, because we have variant->string->variant conversion rather than lazily evaluating it once
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586782740
##########
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:
hmm, I'm not sure if we have a literal presentation for the variant type (its a struct of two binary fields), which is why we have this problem. Similar to the year month time interval/calendar interval, for example, we always rely on `parse_json` (which can accept a json string) to create a literal variant.
The variant's `toString` method essentially calls `toJson` which converts the encoded variant into a JSON string
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586770080
##########
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:
Is there a literal presentation for the variant type? The inconsistency here looks weird. We should have something like `variant{'key': 'value'}`
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "richardc-db (via GitHub)" <gi...@apache.org>.
richardc-db commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586798259
##########
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:
Yep, `parse_json('1')` works (even before this PR). This is because the current code implicitly inserts a cast to variant, which is ok in this case because `CAST(1 as VARIANT)` succeeds
In the more complex case such as a default of `parse_json('{'k': 'v'}')`, the analyzer actually fails because it tries to analyze the sql text `{'k': 'v'}` (note the lack of outer quotations). An alternative here is to create a string literal from `{'k': 'v'}` and wrap it with `parse_json` (effectively coercing it to the correct variant type). This feels inefficient, however, because we have variant->string->variant conversion rather than lazily evaluating it once
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48067][SQL] Fix variant default columns [spark]
Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #46312:
URL: https://github.com/apache/spark/pull/46312#discussion_r1586788998
##########
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:
From the unit tests, can we store text like `parse_json('1')` in the default value?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org