singhpk234 commented on code in PR #5094:
URL: https://github.com/apache/iceberg/pull/5094#discussion_r903336140


##########
spark/v3.3/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/expressions/ExtendedV2ExpressionUtils.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.expressions
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.SQLConfHelper
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.connector.expressions.{Expression => V2Expression}
+import org.apache.spark.sql.connector.expressions.{SortDirection => 
V2SortDirection}
+import org.apache.spark.sql.connector.expressions.{NullOrdering => 
V2NullOrdering}
+import org.apache.spark.sql.connector.expressions.BucketTransform
+import org.apache.spark.sql.connector.expressions.DaysTransform
+import org.apache.spark.sql.connector.expressions.FieldReference
+import org.apache.spark.sql.connector.expressions.HoursTransform
+import org.apache.spark.sql.connector.expressions.IdentityTransform
+import org.apache.spark.sql.connector.expressions.MonthsTransform
+import org.apache.spark.sql.connector.expressions.NamedReference
+import org.apache.spark.sql.connector.expressions.SortValue
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.connector.expressions.TruncateTransform
+import org.apache.spark.sql.connector.expressions.YearsTransform
+import org.apache.spark.sql.errors.QueryCompilationErrors
+
+/**
+ * A class that is inspired by V2ExpressionUtils in Spark but supports Iceberg 
transforms.
+ */
+object ExtendedV2ExpressionUtils extends SQLConfHelper {
+  import 
org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper
+
+  def resolveRef[T <: NamedExpression](ref: NamedReference, plan: 
LogicalPlan): T = {
+    plan.resolve(ref.fieldNames.toSeq, conf.resolver) match {
+      case Some(namedExpr) =>
+        namedExpr.asInstanceOf[T]
+      case None =>
+        val name = ref.fieldNames.toSeq.quoted
+        val outputString = plan.output.map(_.name).mkString(",")
+        throw QueryCompilationErrors.cannotResolveAttributeError(name, 
outputString)
+    }
+  }
+
+  def resolveRefs[T <: NamedExpression](refs: Seq[NamedReference], plan: 
LogicalPlan): Seq[T] = {
+    refs.map(ref => resolveRef[T](ref, plan))
+  }
+
+  def toCatalyst(expr: V2Expression, query: LogicalPlan): Expression = {
+    expr match {
+      case SortValue(child, direction, nullOrdering) =>
+        val catalystChild = toCatalyst(child, query)
+        SortOrder(catalystChild, toCatalyst(direction), 
toCatalyst(nullOrdering), Seq.empty)
+      case IdentityTransform(ref) =>
+        resolveRef[NamedExpression](ref, query)
+      case t: Transform if BucketTransform.unapply(t).isDefined =>
+        t match {
+            // sort columns will be empty for bucket.
+          case BucketTransform(numBuckets, cols, _) =>

Review Comment:
   BucketTransform extractor now expects the unapply to be of type Transform 
post this [3.3 
commit](https://github.com/apache/spark/commit/2ed827a8169a0ad098c5ee4f6101bd003f8c6de2).
 since expr is of type V2Expression on specifying the matcher compiler 
complains with no corresponding unapply() found. Hence I had to it this way



-- 
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]

Reply via email to