c21 commented on a change in pull request #34640:
URL: https://github.com/apache/spark/pull/34640#discussion_r758700614



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ZOrder.scala
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.types.{AbstractDataType, BinaryType, ByteType, 
DataType, IntegerType, IntegralType, LongType, ShortType}
+
+/**
+ * An expression that calculates Z-order value 
(https://en.wikipedia.org/wiki/Z-order_curve)
+ * from `children` input data.
+ *
+ * At the high level, Z-order value is calculated by interleaving the binary 
representations of
+ * `children` input. So the Z-order value preserves locality of input data, 
while mapping the
+ * multi-dimensional input into one dimension output.
+ *
+ */
+@ExpressionDescription(
+  usage = """
+    _FUNC_(input1, input2, ...) - Returns Z-order value in binary type for 
inputs.
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_(1, 2);
+       [-64, 6]
+  """,
+  since = "3.3.0",
+  group = "misc_funcs")
+case class ZOrder(children: Seq[Expression])
+  extends Expression with ExpectsInputTypes with CodegenFallback {
+
+  override def nullable: Boolean = false
+
+  /**
+   * Supported input data types. Currently support [[IntegralType]].
+   *
+   * TODO(SPARK-37362): Support [[FractionalType]] for Z-order.
+   * TODO(SPARK-37363): Support [[StringType]] for Z-order.
+   */
+  override def inputTypes: Seq[AbstractDataType] = 
Seq.fill(children.size)(IntegralType)
+
+  override def dataType: DataType = BinaryType
+
+  override def prettyName: String = "zorder"
+
+  /**
+   * Number of bits per input in binary representation.
+   */
+  @transient private lazy val inputBitSizes = 
children.map(_.dataType.defaultSize * 8)
+
+  /**
+   * Maximal number of bits per input in binary representation.
+   */
+  @transient private lazy val inputBitMaxSize = inputBitSizes.max
+
+  /**
+   * Total number of bits per input in binary representation.
+   */
+  @transient private lazy val inputBitTotalSize = inputBitSizes.sum
+
+  /**
+   * The output for Z-order, represented in byte array format.
+   */
+  @transient private lazy val outputBytes = new Array[Byte](inputBitTotalSize 
/ 8)
+
+  /**
+   * Handle input with null value. Convert null into minimal value per each 
data type.

Review comment:
       I think mapping null to zeros is probably also fine. I am mapping to 
minimal value to mimic NULL first ordering here.




-- 
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: reviews-unsubscr...@spark.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to