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


##########
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();
+      case LONG:
+        return new TruncateLong();
+      case DECIMAL:
+        return new TruncateDecimal(
+            ((DecimalType) toTruncateSparkType).precision(),
+            ((DecimalType) toTruncateSparkType).scale());
+      case STRING:
+        return new TruncateString();
+      case BINARY:
+        return new TruncateBinary();
+      default:
+        throw new UnsupportedOperationException(
+            "Cannot truncate type: " + sourceIcebergType.typeId());
+    }
+  }
+
+  @Override
+  public String description() {
+    return "Truncate - The Iceberg truncate function used for truncate 
partition transformations.";
+  }
+
+  @Override
+  public String name() {
+    return "truncate";
+  }
+
+  public abstract static class TruncateBase<T> implements ScalarFunction<T> {
+    @Override
+    public String name() {
+      return "truncate";
+    }
+
+    @Override
+    public boolean isResultNullable() {
+      return true;
+    }
+
+    @Override
+    public boolean isDeterministic() {
+      return true;

Review Comment:
   Removed the unnecessary overrides.



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