Skip to content

Commit 9ff54b1

Browse files
JiahuiJiangRobert Kruszewski
authored andcommitted
[SPARK-23823][SQL] Keep origin in transformExpression
Fixes https://issues.apache.org/jira/browse/SPARK-23823 Keep origin for all the methods using transformExpression ## What changes were proposed in this pull request? Keep origin in transformExpression ## How was this patch tested? Manually tested that this fixes https://issues.apache.org/jira/browse/SPARK-23823 and columns have correct origins after Analyzer.analyze Author: JiahuiJiang <[email protected]> Author: Jiahui Jiang <[email protected]> Closes apache#20961 from JiahuiJiang/jj/keep-origin.
1 parent ec3f4c5 commit 9ff54b1

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.spark.sql.catalyst.plans
1919

2020
import org.apache.spark.sql.catalyst.expressions._
21-
import org.apache.spark.sql.catalyst.trees.TreeNode
21+
import org.apache.spark.sql.catalyst.trees.{CurrentOrigin, TreeNode}
2222
import org.apache.spark.sql.internal.SQLConf
2323
import org.apache.spark.sql.types.{DataType, StructType}
2424

@@ -103,7 +103,9 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT
103103
var changed = false
104104

105105
@inline def transformExpression(e: Expression): Expression = {
106-
val newE = f(e)
106+
val newE = CurrentOrigin.withOrigin(e.origin) {
107+
f(e)
108+
}
107109
if (newE.fastEquals(e)) {
108110
e
109111
} else {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.plans
19+
20+
import org.apache.spark.SparkFunSuite
21+
import org.apache.spark.sql.catalyst.dsl.plans
22+
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, Literal, NamedExpression}
23+
import org.apache.spark.sql.catalyst.trees.{CurrentOrigin, Origin}
24+
import org.apache.spark.sql.types.IntegerType
25+
26+
class QueryPlanSuite extends SparkFunSuite {
27+
28+
test("origin remains the same after mapExpressions (SPARK-23823)") {
29+
CurrentOrigin.setPosition(0, 0)
30+
val column = AttributeReference("column", IntegerType)(NamedExpression.newExprId)
31+
val query = plans.DslLogicalPlan(plans.table("table")).select(column)
32+
CurrentOrigin.reset()
33+
34+
val mappedQuery = query mapExpressions {
35+
case _: Expression => Literal(1)
36+
}
37+
38+
val mappedOrigin = mappedQuery.expressions.apply(0).origin
39+
assert(mappedOrigin == Origin.apply(Some(0), Some(0)))
40+
}
41+
42+
}

0 commit comments

Comments
 (0)