mgaido91 commented on a change in pull request #24442: [SPARK-27547][SQL] fix 
DataFrame self-join problems
URL: https://github.com/apache/spark/pull/24442#discussion_r277701243
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/analysis/ResolveDatasetColumnReference.scala
 ##########
 @@ -0,0 +1,136 @@
+/*
+ * 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.analysis
+
+import scala.collection.mutable
+import scala.util.Try
+
+import org.apache.spark.sql.{AnalysisException, Dataset}
+import org.apache.spark.sql.catalyst.AliasIdentifier
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference, 
Equality, EqualNullSafe, EqualTo}
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SubqueryAlias}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.MetadataBuilder
+
+/**
+ * Resolves the Dataset column reference by traversing the query plan and 
finding the plan subtree
+ * of the Dataset that the column reference belongs to.
+ *
+ * Dataset column reference is simply an [[AttributeReference]] that is 
returned by `Dataset#col`.
+ * Most of time we don't need to do anything special, as 
[[AttributeReference]] can point to
+ * the column precisely. However, in case of self-join, the analyzer generates
+ * [[AttributeReference]] with new expr IDs for the right side plan of the 
join. If the Dataset
+ * column reference points to a column in the right side plan of a self-join, 
we need to replace it
+ * with the corresponding newly generated [[AttributeReference]].
+ */
+class ResolveDatasetColumnReference(conf: SQLConf) extends Rule[LogicalPlan] {
+
+  // Dataset column reference is an `AttributeReference` with 2 special 
metadata.
+  private def isColumnReference(a: AttributeReference): Boolean = {
+    a.metadata.contains(Dataset.ID_PREFIX) && 
a.metadata.contains(Dataset.COL_POS_PREFIX)
+  }
+
+  private case class ColumnReference(datasetId: Long, colPos: Int)
+
+  private def toColumnReference(a: AttributeReference): ColumnReference = {
+    ColumnReference(
+      a.metadata.getLong(Dataset.ID_PREFIX),
+      a.metadata.getLong(Dataset.COL_POS_PREFIX).toInt)
 
 Review comment:
   do we really need this column prefix? We use it only to avoid a scan, but we 
get it with a scan using `indexOf`, so I think we can get rid of 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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to