-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-31317][SQL] Add withField method to Column #27066
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
Changes from all commits
8935221
c8bea43
1e944b3
5d0cdf8
20cffbc
32552c4
15f8ee4
1e3300e
9b98e7d
14b9822
ce1e030
1bfa465
238f2f2
e3c7930
a59d94f
1199550
156bf2d
337bdb5
40cbc59
ab36504
8159e47
38747e2
a54d3fb
3e540ac
35004cf
c2f9216
f5a9420
b66fcff
8ada917
1af96cd
6adbbd7
bc5d622
2a6ec1a
f428b54
98d5843
b18ac3e
b58f3c8
096bbf4
6edf5a8
445e2d8
9c3b2a2
b0a9cc7
9fb0a5d
b53930d
88244f0
4315e92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.WithFields | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
|
|
||
| /** | ||
| * Combines all adjacent [[WithFields]] expression into a single [[WithFields]] expression. | ||
| */ | ||
| object CombineWithFields extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions { | ||
| case WithFields(WithFields(struct, names1, valExprs1), names2, valExprs2) => | ||
| WithFields(struct, names1 ++ names2, valExprs1 ++ valExprs2) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Replaces [[WithFields]] expression with an evaluable expression. | ||
| */ | ||
| object ReplaceWithFieldsExpression extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions { | ||
| case w: WithFields => w.evalExpr | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Literal, WithFields} | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
|
|
||
|
|
||
| class CombineWithFieldsSuite extends PlanTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("CombineWithFields", FixedPoint(10), CombineWithFields) :: Nil | ||
| } | ||
|
|
||
| private val testRelation = LocalRelation('a.struct('a1.int)) | ||
|
|
||
| test("combines two WithFields") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("c1"), | ||
| Seq(Literal(5))), "out")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Alias(WithFields('a, Seq("b1", "c1"), Seq(Literal(4), Literal(5))), "out")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("combines three WithFields") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("c1"), | ||
| Seq(Literal(5))), | ||
| Seq("d1"), | ||
| Seq(Literal(6))), "out")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Alias(WithFields('a, Seq("b1", "c1", "d1"), Seq(4, 5, 6).map(Literal(_))), "out")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -871,6 +871,72 @@ class Column(val expr: Expression) extends Logging { | |
| */ | ||
| def getItem(key: Any): Column = withExpr { UnresolvedExtractValue(expr, Literal(key)) } | ||
|
|
||
| // scalastyle:off line.size.limit | ||
| /** | ||
| * An expression that adds/replaces field in `StructType` by name. | ||
| * | ||
| * {{{ | ||
| * val df = sql("SELECT named_struct('a', 1, 'b', 2) struct_col") | ||
| * df.select($"struct_col".withField("c", lit(3))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have any of you try to run these examples? The optimizer ConstantFolding rule will break these examples.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird, we have tests to cover these examples. @fqaiser94 can you take a look?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I failed to write a test case to cover this scenario, my bad.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for raising the issue @gatorsmile |
||
| * // result: {"a":1,"b":2,"c":3} | ||
| * | ||
| * val df = sql("SELECT named_struct('a', 1, 'b', 2) struct_col") | ||
| * df.select($"struct_col".withField("b", lit(3))) | ||
| * // result: {"a":1,"b":3} | ||
| * | ||
| * val df = sql("SELECT CAST(NULL AS struct<a:int,b:int>) struct_col") | ||
| * df.select($"struct_col".withField("c", lit(3))) | ||
| * // result: null of type struct<a:int,b:int,c:int> | ||
| * | ||
| * val df = sql("SELECT named_struct('a', 1, 'b', 2, 'b', 3) struct_col") | ||
| * df.select($"struct_col".withField("b", lit(100))) | ||
| * // result: {"a":1,"b":100,"b":100} | ||
| * | ||
| * val df = sql("SELECT named_struct('a', named_struct('a', 1, 'b', 2)) struct_col") | ||
| * df.select($"struct_col".withField("a.c", lit(3))) | ||
| * // result: {"a":{"a":1,"b":2,"c":3}} | ||
| * | ||
| * val df = sql("SELECT named_struct('a', named_struct('b', 1), 'a', named_struct('c', 2)) struct_col") | ||
| * df.select($"struct_col".withField("a.c", lit(3))) | ||
| * // result: org.apache.spark.sql.AnalysisException: Ambiguous reference to fields | ||
| * }}} | ||
| * | ||
| * @group expr_ops | ||
| * @since 3.1.0 | ||
| */ | ||
| // scalastyle:on line.size.limit | ||
| def withField(fieldName: String, col: Column): Column = withExpr { | ||
| require(fieldName != null, "fieldName cannot be null") | ||
| require(col != null, "col cannot be null") | ||
|
|
||
| val nameParts = if (fieldName.isEmpty) { | ||
| fieldName :: Nil | ||
fqaiser94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| CatalystSqlParser.parseMultipartIdentifier(fieldName) | ||
| } | ||
| withFieldHelper(expr, nameParts, Nil, col.expr) | ||
| } | ||
|
|
||
| private def withFieldHelper( | ||
| struct: Expression, | ||
| namePartsRemaining: Seq[String], | ||
| namePartsDone: Seq[String], | ||
| value: Expression) : WithFields = { | ||
| val name = namePartsRemaining.head | ||
| if (namePartsRemaining.length == 1) { | ||
| WithFields(struct, name :: Nil, value :: Nil) | ||
| } else { | ||
| val newNamesRemaining = namePartsRemaining.tail | ||
| val newNamesDone = namePartsDone :+ name | ||
| val newValue = withFieldHelper( | ||
| struct = UnresolvedExtractValue(struct, Literal(name)), | ||
| namePartsRemaining = newNamesRemaining, | ||
| namePartsDone = newNamesDone, | ||
| value = value) | ||
| WithFields(struct, name :: Nil, newValue :: Nil) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An expression that gets a field by name in a `StructType`. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.