nssalian commented on code in PR #16827:
URL: https://github.com/apache/iceberg/pull/16827#discussion_r3779006716


##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/SparkHilbertUDF.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.actions;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import org.apache.iceberg.util.HilbertByteUtils;
+import org.apache.spark.sql.Column;
+import org.apache.spark.sql.expressions.UserDefinedFunction;
+import org.apache.spark.sql.functions;
+import org.apache.spark.sql.types.DataTypes;
+import scala.collection.JavaConverters;
+import scala.collection.Seq;
+
+/**
+ * Combines per-column ordered bytes (produced by {@link 
SparkZOrderUDF#sortedLexicographically})
+ * into a single Hilbert-curve value. Mirrors the combine step of {@link 
SparkZOrderUDF}
+ * (interleave), replacing it with {@link HilbertByteUtils#hilbertIndex}.
+ */
+class SparkHilbertUDF implements Serializable {
+
+  private transient ThreadLocal<ByteBuffer> outputBuffer;
+
+  private final int numCols;
+  private final int bitsPerColumn;
+  private final int outputBytes;
+
+  SparkHilbertUDF(int numCols, int bitsPerColumn) {
+    this.numCols = numCols;
+    this.bitsPerColumn = bitsPerColumn;
+    this.outputBytes = numCols * (bitsPerColumn / 8);
+  }
+
+  private void readObject(ObjectInputStream in) throws IOException, 
ClassNotFoundException {
+    in.defaultReadObject();
+    outputBuffer = ThreadLocal.withInitial(() -> 
ByteBuffer.allocate(outputBytes));
+  }
+
+  private byte[] hilbertValue(Seq<byte[]> scalaBinary) {
+    byte[][] columnsBinary = 
JavaConverters.seqAsJavaList(scalaBinary).toArray(new byte[numCols][]);

Review Comment:
   allocates a fresh `new byte[numCols][]` holder on every row, whereas the 
class it mirrors, `SparkZOrderUDF`, reuses a `ThreadLocal<byte[][]> 
inputHolder` (`.toArray(inputHolder.get())`). Should be an easy fix is by 
adding a transient `ThreadLocal<byte[][]>` reinitialized in `readObject`



##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java:
##########
@@ -400,6 +402,12 @@ public static Term toIcebergTerm(Expression expr) {
                   .map(ref -> DOT.join(ref.fieldNames()))
                   .map(org.apache.iceberg.expressions.Expressions::ref)
                   .collect(Collectors.toList()));
+        case "hilbert":
+          return new org.apache.iceberg.expressions.Hilbert(

Review Comment:
   can we use the imported name like ZOrder versus the fully qualified import?



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