kbendick commented on code in PR #5305:
URL: https://github.com/apache/iceberg/pull/5305#discussion_r927056436


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java:
##########
@@ -0,0 +1,319 @@
+/*
+ * 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.functions;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.StandardCharsets;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.util.ByteBuffers;
+import org.apache.iceberg.util.UnicodeUtil;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.connector.catalog.functions.BoundFunction;
+import org.apache.spark.sql.connector.catalog.functions.ScalarFunction;
+import org.apache.spark.sql.connector.catalog.functions.UnboundFunction;
+import org.apache.spark.sql.types.DataType;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.Decimal;
+import org.apache.spark.sql.types.DecimalType;
+import org.apache.spark.sql.types.IntegerType;
+import org.apache.spark.sql.types.LongType;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.unsafe.types.UTF8String;
+
+/**
+ * Implementation of {@link UnboundFunction} that matches the {@code truncate} 
transformation.
+ * The various implementations are registered with the {@link 
org.apache.iceberg.spark.SparkCatalog}
+ * such that the function can be used as {@code truncate(col, width)} or 
{@code truncate(col, 2)}.
+ */
+public class TruncateFunction implements UnboundFunction {
+
+  public TruncateFunction() {
+
+  }
+
+  private static void validateSourceTypeUsingSparkTypes(DataType dt) {
+    if (!DataTypes.IntegerType.acceptsType(dt) && 
!DataTypes.LongType.acceptsType(dt) &&
+        !DataTypes.StringType.acceptsType(dt) && 
!DataTypes.BinaryType.acceptsType(dt) &&
+        !(dt instanceof DecimalType)) {
+      throw new UnsupportedOperationException(
+          "Cannot truncate input type. Expected one of [IntegerType, LongType, 
DecimalType, StringType, BinaryType], " +
+              "but found " + dt.catalogString());
+    }
+  }
+
+  private static void validateTruncationWidthType(DataType widthType) {
+    if (!(widthType instanceof IntegerType ||
+        !(widthType instanceof LongType && 
DataTypes.IntegerType.acceptsType(widthType)))) {
+      throw new UnsupportedOperationException("Expected truncation width to be 
an integer type");
+    }
+  }
+
+  @Override
+  public BoundFunction bind(StructType inputType) {
+    if (inputType.fields().length != 2) {
+      throw new UnsupportedOperationException(
+          String.format("Invalid input type. Expected 2 fields but found %s", 
inputType.fields().length));
+    }
+
+    StructField fieldToTruncate = inputType.apply(0);
+    StructField truncationWidth = inputType.apply(1);
+
+    DataType toTruncateSparkType = fieldToTruncate.dataType();
+    Type sourceIcebergType = 
SparkSchemaUtil.convert(fieldToTruncate.dataType());
+
+    validateSourceTypeUsingSparkTypes(fieldToTruncate.dataType());
+    validateTruncationWidthType(truncationWidth.dataType());
+
+    switch (sourceIcebergType.typeId()) {
+      case INTEGER:
+        return new TruncateInteger();

Review Comment:
   Looking into this further, it seems that Spark will add a cast for us to 
Integer if we allow `smallint` or `tinyint` but only define a function for 
Integer type.
   
   When calling `EXPLAIN EXTENDED SELECT system.truncate(2, -1S)`, there is a 
`(CAST -1 as int)` in the logical plan:
   
   ```
   == Parsed Logical Plan ==
   'Project [unresolvedalias('testhadoop.system.truncate(2, -1), None)]
   +- OneRowRelation
   
   == Analyzed Logical Plan ==
   staticinvoke(class 
org.apache.iceberg.spark.functions.TruncateFunction$TruncateInteger, 
IntegerType, invoke, 2, -1, IntegerType, IntegerType, false, true, true): int
   Project [staticinvoke(class 
org.apache.iceberg.spark.functions.TruncateFunction$TruncateInteger, 
IntegerType, invoke, 2, cast(-1 as int), IntegerType, IntegerType, false, true, 
true) AS staticinvoke(class 
org.apache.iceberg.spark.functions.TruncateFunction$TruncateInteger, 
IntegerType, invoke, 2, -1, IntegerType, IntegerType, false, true, true)#98]
   +- OneRowRelation
   
   == Optimized Logical Plan ==
   Project [-2 AS staticinvoke(class 
org.apache.iceberg.spark.functions.TruncateFunction$TruncateInteger, 
IntegerType, invoke, 2, -1, IntegerType, IntegerType, false, true, true)#98]
   +- OneRowRelation
   
   == Physical Plan ==
   *(1) Project [-2 AS staticinvoke(class 
org.apache.iceberg.spark.functions.TruncateFunction$TruncateInteger, 
IntegerType, invoke, 2, -1, IntegerType, IntegerType, false, true, true)#98]
   +- *(1) Scan OneRowRelation[]
   ```
   
   However, the resulting row type will still be `IntegerType`, so we should 
consider still defining them.
   
   But I'm ok with allowing the cast for the width field from tinyint / 
smallint -> int as that doesn't affect the resulting input type and the static 
function is still called.



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