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


##########
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 =>

Review Comment:
   Why not update to use the matcher? It looks like Spark added an argument?



##########
spark/v3.3/spark/src/test/java/org/apache/iceberg/spark/TestSparkSchemaUtil.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.iceberg.spark;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.iceberg.MetadataColumns;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.catalyst.expressions.AttributeReference;
+import org.apache.spark.sql.types.StructType;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+
+public class TestSparkSchemaUtil {
+  private static final Schema TEST_SCHEMA = new Schema(
+      optional(1, "id", Types.IntegerType.get()),
+      optional(2, "data", Types.StringType.get())
+  );
+
+  private static final Schema TEST_SCHEMA_WITH_METADATA_COLS = new Schema(
+      optional(1, "id", Types.IntegerType.get()),
+      optional(2, "data", Types.StringType.get()),
+      MetadataColumns.FILE_PATH,
+      MetadataColumns.ROW_POSITION
+  );
+
+  @Test
+  public void testEstimateSizeMaxValue() throws IOException {
+    Assert.assertEquals("estimateSize returns Long max value", Long.MAX_VALUE,
+        SparkSchemaUtil.estimateSize(
+            null,
+            Long.MAX_VALUE));
+  }
+
+  @Test
+  public void testEstimateSizeWithOverflow() throws IOException {
+    long tableSize = 
SparkSchemaUtil.estimateSize(SparkSchemaUtil.convert(TEST_SCHEMA), 
Long.MAX_VALUE - 1);
+    Assert.assertEquals("estimateSize handles overflow", Long.MAX_VALUE, 
tableSize);
+  }
+
+  @Test
+  public void testEstimateSize() throws IOException {
+    long tableSize = 
SparkSchemaUtil.estimateSize(SparkSchemaUtil.convert(TEST_SCHEMA), 1);
+    Assert.assertEquals("estimateSize matches with expected approximation", 
24, tableSize);
+  }
+
+

Review Comment:
   Nit: unnecessary newline.



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