You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/07/20 21:18:55 UTC

[GitHub] [iceberg] kbendick commented on a diff in pull request #5305: Spark - Implement FunctionCatalog and Truncate

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


##########
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:
   When reading the documentation for `ScalarFunction`, it seemed like Spark wouls perform the required cast before invoking the magic function.



##########
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:
   https://spark.apache.org/docs/latest/api/java/index.html?org/apache/spark/sql/connector/catalog/functions/ScalarFunction.html
   
   I might have that wrong though.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org