qphien commented on a change in pull request #990:
URL: https://github.com/apache/incubator-kyuubi/pull/990#discussion_r698129898



##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/spark/sql/execution/ZorderBytesUtils.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.execution
+
+import java.lang.{Float => jFloat}
+import java.lang.{Double => jDouble}
+import java.nio.charset.Charset
+
+object ZorderBytesUtils {
+  def interleaveMultiByteArray(arrays: Array[Array[Byte]]): Array[Byte] = {
+    var totalLength = 0
+    var maxLength = 0
+    arrays.foreach(array => {
+      totalLength += array.length
+      maxLength = maxLength.max(array.length * 8)
+    })
+    val result = new Array[Byte](totalLength)
+    var resultBit = 0
+
+    for (bit <- 0 to maxLength) {
+      val bytePos = Math.floor(bit / 8).toInt
+      val bitPos = bit % 8
+
+      for (arr <- arrays) {
+        if (bytePos < arr.length) {
+          val resultBytePos = totalLength - 1 - Math.floor(resultBit / 8).toInt
+          val resultBitPos = resultBit % 8
+          result(resultBytePos) = updatePos(result(resultBytePos), 
resultBitPos,
+            arr(arr.length - 1 - bytePos), bitPos)
+          resultBit += 1
+        }
+      }
+    }
+    result
+  }
+
+  def updatePos(a: Byte, apos: Int, b: Byte, bpos: Int): Byte = {
+    var temp = (b & (1 << bpos)).toByte
+    if (apos > bpos) {
+      temp = (temp << (apos - bpos)).toByte
+    } else if (apos < bpos) {
+      temp = (temp >> (bpos - apos)).toByte
+    }
+    val atemp = (a & (1 << apos)).toByte
+    if (atemp == temp) {
+      return a
+    }
+    (a ^ (1 << apos)).toByte
+  }
+
+  def booleanToByte(a: Boolean): Array[Byte] = {
+    if (a) {
+      intToByte(1)
+    } else {
+      intToByte(0)
+    }
+  }
+
+  def byteToByte(a: Int): Array[Byte] = {
+    val tmp = (a ^ (1 << 7)).toByte
+    Array(tmp)
+  }
+
+  def shortToByte(a: Short): Array[Byte] = {
+    val tmp = a ^ (1 << 15)
+    Array(((tmp >> 8) & 0xff).toByte, (tmp & 0xff).toByte)
+  }
+
+  def intToByte(a: Int): Array[Byte] = {
+    val tmp = a ^ (1 << 31)
+    val result = new Array[Byte](4)
+    for (i <- 0 to 3) {
+      val offset = i * 8
+      result(3 - i) = ((tmp >> offset) & 0xff).toByte
+    }
+    result
+  }
+
+  def longToByte(a: Long): Array[Byte] = {
+    val tmp = a ^ (1 << 63)
+    val result = new Array[Byte](8)
+    for (i <- 0 to 7) {
+      val offset = i * 8
+      result(7 - i) = ((tmp >> offset) & 0xff).toByte
+    }
+    result
+  }
+
+  def floatToByte(a: Float): Array[Byte] = {
+    val fi = jFloat.floatToRawIntBits(a)
+    intToByte(fi)
+  }
+
+  def doubleToByte(a: Double): Array[Byte] = {
+    val dl = jDouble.doubleToRawLongBits(a)
+    longToByte(dl)
+  }
+
+  def stringToByte(str: String): Array[Byte] = {
+    // truncate or padding str to 8 byte

Review comment:
       @cfmcgrady Yep, current implementation can not resolve this problem. I 
think a way to solve this problem is to map string to rank and then generate 
z-value with rank value. However, this solution needs to sort all string values 
globally. In consideration of globally sort cost and z-order data skipping 
effectiveness, a comprise solution is to take sample of rows and get some 
ordered bounds, then map each row to bound index. For example:
   ```
   Rows:
   ["http://www.example.com/a";,
   "http://www.example.com/d";,
   "http://www.example.com/g";,
   "http://www.example.com/h";,
   "http://www.example.com/i";,
   "http://www.example.com/n";,
   "http://www.example.com/o";,
   "http://www.example.com/q";,
   "http://www.example.com/t";]
   
   Sample Bounds:
   ["http://www.example.com/a";, "http://www.example.com/g";]
   ["http://www.example.com/h";, "http://www.example.com/n";]
   ["http://www.example.com/o";, "http://www.example.com/t";]
   
   Map row to bound index:
   { 
   ("http://www.example.com/a"; -> 0),
   ("http://www.example.com/d"; -> 0),
   ("http://www.example.com/g"; -> 0),
   ("http://www.example.com/h"; -> 1),
   ("http://www.example.com/i"; -> 1),
   ("http://www.example.com/n"; -> 1),
   ("http://www.example.com/o"; ->2),
   ("http://www.example.com/q"; -> 2),
   ("http://www.example.com/t"; -> 2)
   }
   ```
   
   Bound index is used to generate z-value




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


Reply via email to