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



##########
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:
       In the current implementation, only the first 8 bytes of string will be 
used to generate zvalue, It's limited when the input string is greater than 8 
bytes.
   Suppose we have the following table:
   
   |col_a|
   | --- |
   |http://www.example.com/a|
   |http://www.example.com/b|
   |http://www.example.com/c|
   |http://www.example.com/d|
   
   the zvalue of these rows:
   
   ```scala
   Seq(
     "http://www.example.com/a";,
     "http://www.example.com/b";,
     "http://www.example.com/c";,
     "http://www.example.com/d";,
   ).foreach(s => {
     println(s"input string: ${s}, zvalue: 
${ZorderBytesUtils.stringToByte(s).mkString(",")}")
   })
   ```
   ```
   input string: http://www.example.com/a, zvalue: 104,116,116,112,58,47,47,119
   input string: http://www.example.com/b, zvalue: 104,116,116,112,58,47,47,119
   input string: http://www.example.com/c, zvalue: 104,116,116,112,58,47,47,119
   input string: http://www.example.com/d, zvalue: 104,116,116,112,58,47,47,119
   ```
   
   All of these rows have the same zvalue, and we can't do effective data 
skipping 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to