i7xh commented on a change in pull request #1086:
URL: https://github.com/apache/incubator-kyuubi/pull/1086#discussion_r707199426



##########
File path: 
dev/kyuubi-extension-spark-3-1/src/main/scala/org/apache/kyuubi/sql/watchdog/MaxHivePartitionStrategy.scala
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.watchdog
+
+import org.apache.spark.sql.{SparkSession, Strategy}
+import org.apache.spark.sql.catalyst.SQLConfHelper
+import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
+import org.apache.spark.sql.catalyst.planning.ScanOperation
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.execution.SparkPlan
+
+import org.apache.kyuubi.sql.KyuubiSQLConf
+
+/**
+ * Add maxHivePartitions Strategy to avoid scan excessive hive partitions on 
partitioned table
+ * 1 Check if scan exceed maxHivePartition
+ * 2 Check if Using partitionFilter on partitioned table
+ * This Strategy Add Planner Strategy after LogicalOptimizer
+ */
+case class MaxHivePartitionStrategy(session: SparkSession)
+  extends Strategy with SQLConfHelper {
+  override def apply(plan: LogicalPlan): Seq[SparkPlan] = {
+    conf.getConf(KyuubiSQLConf.WATCHDOG_MAX_HIVEPARTITION) match {
+      case Some(maxHivePartition) => plan match {
+        case ScanOperation(_, _, relation: HiveTableRelation) =>
+          if (relation.isPartitioned) {
+            relation.prunedPartitions match {
+              case Some(prunedPartitions) => if (prunedPartitions.size > 
maxHivePartition) {
+                throw new MaxHivePartitionExceedException(
+                  s"SQL job scan hive partition: ${prunedPartitions.size} " +
+                    s"exceed restrict of hive scan maxPartition 
$maxHivePartition \n" +
+                    s"You should optimize your SQL logical according partition 
structure" +
+                    s" or shorten query scope such as p_date, detail as below: 
\n" +
+                    s"Table: ${relation.tableMeta.qualifiedName} \n" +
+                    s"Owner: ${relation.tableMeta.owner} \n" +
+                    s"Partition Structure: " +
+                    s"${relation.partitionCols.map(_.name).mkString(" -> ")} 
\n")
+              } else {
+                Nil
+              }
+              case _ => throw new HivePartitionFilterUnusedException(

Review comment:
       > Similar to the hive table`PruneHiveTablePartitions` and datasource 
table `PruneFileSourcePartitions`. The datasource table is also very common for 
user. We can improve this rule in a new PR that also support check datasource 
table if it's partitioned.
   
   Understood your mean, it's OK!
   




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