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



##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/OptimizeBeforeWrite.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.spark.SparkException
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, 
Expression, Or}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, 
InsertIntoStatement, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+case class OptimizeBeforeWrite(session: SparkSession) extends 
Rule[LogicalPlan] {

Review comment:
       `OptimizeBeforeWrite ` -> `ZorderBeforeWrite`

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/OptimizeBeforeWrite.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.spark.SparkException
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, 
Expression, Or}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, 
InsertIntoStatement, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+case class OptimizeBeforeWrite(session: SparkSession) extends 
Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    plan match {
+      case o@OptimizeZorderCommand(child) =>
+        var relation: Option[HiveTableRelation] = None
+        var partitionSpec: Map[String, Option[String]] = Map.empty
+        val hadoopConf = session.sessionState.newHadoopConf()
+        val newChild = child.resolveOperatorsUp {
+          case f@Filter(condition, _) if f.resolved =>
+            if (relation.isDefined) {
+              val tableRelation = relation.get
+              if (!tableRelation.isPartitioned) {
+                throw new ZorderException("Filters are only supported for 
partitioned table")
+              }
+
+              val partitionKeyIds = AttributeSet(tableRelation.partitionCols)
+              val predicates = splitConjunctivePredicates(condition)
+              val (_, otherPredicates) = predicates.partition { predicate =>
+                !predicate.references.isEmpty &&
+                  predicate.references.subsetOf(partitionKeyIds)
+              }
+
+              if (otherPredicates.nonEmpty) {
+                throw new ZorderException("Only partition column filters are 
allowed")
+              }
+
+              if (!hadoopConf.get("hive.exec.dynamic.partition", 
"true").toBoolean) {
+                throw new 
SparkException(ErrorMsg.DYNAMIC_PARTITION_DISABLED.getMsg)

Review comment:
       do we need the check ? Spark has already checked in 
`InsertIntoHiveTable`.

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/OptimizeBeforeWrite.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.spark.SparkException
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, 
Expression, Or}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, 
InsertIntoStatement, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+case class OptimizeBeforeWrite(session: SparkSession) extends 
Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    plan match {
+      case o@OptimizeZorderCommand(child) =>
+        var relation: Option[HiveTableRelation] = None
+        var partitionSpec: Map[String, Option[String]] = Map.empty
+        val hadoopConf = session.sessionState.newHadoopConf()
+        val newChild = child.resolveOperatorsUp {
+          case f@Filter(condition, _) if f.resolved =>
+            if (relation.isDefined) {
+              val tableRelation = relation.get
+              if (!tableRelation.isPartitioned) {
+                throw new ZorderException("Filters are only supported for 
partitioned table")
+              }
+
+              val partitionKeyIds = AttributeSet(tableRelation.partitionCols)
+              val predicates = splitConjunctivePredicates(condition)

Review comment:
       why not check reference directly instead of split + check ?

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/Zorder.scala
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{BoundReference, Expression}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.types.{BinaryType, BooleanType, ByteType, 
DataType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, 
LongType, ShortType, StringType, TimestampType}
+
+case class Zorder(children: Seq[Expression]) extends Expression with 
CodegenFallback {
+  private lazy val defaultNullValues = {
+    children.map {
+      case bf: BoundReference =>
+        bf.dataType match {
+          case BooleanType =>
+            false
+          case ByteType =>
+            Byte.MaxValue
+          case ShortType =>
+            Short.MaxValue
+          case IntegerType =>
+            Int.MaxValue
+          case LongType =>
+            Long.MaxValue
+          case FloatType =>
+            Float.MaxValue
+          case DoubleType =>
+            Double.MaxValue
+          case StringType =>
+            ""
+          case TimestampType =>
+            Long.MaxValue
+          case DateType =>
+            Int.MaxValue
+          case d: DecimalType =>
+            Long.MaxValue
+          case other: Any =>
+            throw new ZorderException("Unsupported z-order type: " + 
other.getClass)
+        }
+      case other: Any =>
+        throw new ZorderException("Unknown z-order column: " + other)
+    }
+  }
+
+  override def nullable: Boolean = true

Review comment:
       does zorder will produce null ?

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/OptimizeBeforeWrite.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.spark.SparkException
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, 
Expression, Or}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, 
InsertIntoStatement, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+case class OptimizeBeforeWrite(session: SparkSession) extends 
Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    plan match {
+      case o@OptimizeZorderCommand(child) =>
+        var relation: Option[HiveTableRelation] = None
+        var partitionSpec: Map[String, Option[String]] = Map.empty
+        val hadoopConf = session.sessionState.newHadoopConf()
+        val newChild = child.resolveOperatorsUp {
+          case f@Filter(condition, _) if f.resolved =>

Review comment:
       We can make this code more clearer since we only support two pattern 
here:
   ```
   case f @ Filter(condition, r: HiveTableRelation) =>
     checkRelation()
   
   case r: HiveTableRelation =>
     checkRelation()
   
   ```

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/Zorder.scala
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{BoundReference, Expression}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.types.{BinaryType, BooleanType, ByteType, 
DataType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, 
LongType, ShortType, StringType, TimestampType}
+
+case class Zorder(children: Seq[Expression]) extends Expression with 
CodegenFallback {
+  private lazy val defaultNullValues = {
+    children.map {
+      case bf: BoundReference =>
+        bf.dataType match {
+          case BooleanType =>
+            false
+          case ByteType =>
+            Byte.MaxValue
+          case ShortType =>
+            Short.MaxValue
+          case IntegerType =>
+            Int.MaxValue
+          case LongType =>
+            Long.MaxValue
+          case FloatType =>
+            Float.MaxValue
+          case DoubleType =>
+            Double.MaxValue
+          case StringType =>
+            ""
+          case TimestampType =>
+            Long.MaxValue
+          case DateType =>
+            Int.MaxValue
+          case d: DecimalType =>
+            Long.MaxValue
+          case other: Any =>
+            throw new ZorderException("Unsupported z-order type: " + 
other.getClass)
+        }
+      case other: Any =>
+        throw new ZorderException("Unknown z-order column: " + other)
+    }
+  }
+
+  override def nullable: Boolean = true
+
+  override def eval(input: InternalRow): Any = {
+    var i = 0
+    val evaluated = ArrayBuffer[Any]()

Review comment:
       ```scala
   val evaluated = children.zipWithIndex { case (child, index) =>
     val v = child.eval(input)
     if (v == null) {
       defaultNullValues(index)
     } else {
       v
     }
   }
   ```

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/OptimizeZorderCommand.scala
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, 
UnresolvedRelation}
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, 
Expression, NullsLast, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Sort, 
UnaryNode}
+
+case class OptimizeZorderCommand(child: LogicalPlan) extends UnaryNode {
+  override def output: Seq[Attribute] = child.output
+}
+
+object OptimizeZorderCommand {
+
+  def apply(tableIdent: Seq[String],
+            whereExp: Option[Expression],
+            sortArr: Seq[UnresolvedAttribute]): OptimizeZorderCommand = {
+    val table = UnresolvedRelation(tableIdent)
+    val child = whereExp match {
+      case Some(x) => Filter(x, table)
+      case None => table
+    }
+
+    val sortOrder = SortOrder(Zorder(sortArr), Ascending, NullsLast, Seq.empty)
+    val zorderSort = Sort(Seq(sortOrder), false, child)

Review comment:
       I think we support the global zorder now

##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/zorder/Zorder.scala
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.kyuubi.sql.zorder
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{BoundReference, Expression}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.types.{BinaryType, BooleanType, ByteType, 
DataType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, 
LongType, ShortType, StringType, TimestampType}
+
+case class Zorder(children: Seq[Expression]) extends Expression with 
CodegenFallback {
+  private lazy val defaultNullValues = {
+    children.map {
+      case bf: BoundReference =>
+        bf.dataType match {
+          case BooleanType =>
+            false
+          case ByteType =>
+            Byte.MaxValue
+          case ShortType =>
+            Short.MaxValue
+          case IntegerType =>
+            Int.MaxValue
+          case LongType =>
+            Long.MaxValue
+          case FloatType =>
+            Float.MaxValue
+          case DoubleType =>
+            Double.MaxValue
+          case StringType =>
+            ""
+          case TimestampType =>
+            Long.MaxValue
+          case DateType =>
+            Int.MaxValue
+          case d: DecimalType =>
+            Long.MaxValue
+          case other: Any =>
+            throw new ZorderException("Unsupported z-order type: " + 
other.getClass)
+        }
+      case other: Any =>
+        throw new ZorderException("Unknown z-order column: " + other)
+    }
+  }
+
+  override def nullable: Boolean = true
+
+  override def eval(input: InternalRow): Any = {
+    var i = 0
+    val evaluated = ArrayBuffer[Any]()
+    while (i < children.length) {
+      val ev = children(i).eval(input)
+      if (ev == null) {
+        evaluated += defaultNullValues(i)
+      } else {
+        evaluated += ev
+      }
+      i += 1
+    }
+
+    val binaryArr = evaluated.toArray.map {

Review comment:
       can we move this code into `ZorderBytesUtils` ?




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