alexeykudinkin commented on code in PR #7423:
URL: https://github.com/apache/hudi/pull/7423#discussion_r1080531144
##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/HoodieSparkSessionExtension.scala:
##########
@@ -44,6 +40,17 @@ class HoodieSparkSessionExtension extends
(SparkSessionExtensions => Unit)
extensions.injectPostHocResolutionRule(ruleBuilder(_))
}
+ HoodieAnalysis.customOptimizerRules.foreach { ruleBuilder =>
+ extensions.injectOptimizerRule(ruleBuilder(_))
+ }
+
+ /*
+ // CBO is only supported in Spark >= 3.1.x
+ HoodieAnalysis.customPreCBORules.foreach { ruleBuilder =>
+ extensions.injectPreCBORule(ruleBuilder(_))
+ }
+ */
Review Comment:
Yes, ideally this rule should be invoked in pre-CBO slot, but unfortunately
it's not supported for earlier Spark versions
##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodiePruneFileSourcePartitions.scala:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.hudi.analysis
+
+import org.apache.hudi.SparkAdapterSupport.sparkAdapter
+import org.apache.hudi.{HoodieBaseRelation, HoodieFileIndex}
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.CatalogStatistics
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference,
AttributeSet, Expression, ExpressionSet, NamedExpression, PredicateHelper,
SubqueryExpression}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import
org.apache.spark.sql.catalyst.plans.logical.statsEstimation.FilterEstimation
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.{HadoopFsRelation,
LogicalRelation}
+import
org.apache.spark.sql.hudi.analysis.HoodiePruneFileSourcePartitions.{HoodieRelationMatcher,
exprUtils, getPartitionFiltersAndDataFilters, rebuildPhysicalOperation}
+import org.apache.spark.sql.sources.BaseRelation
+import org.apache.spark.sql.types.StructType
+
+/**
+ * Prune the partitions of Hudi table based relations by the means of pushing
down the
+ * partition filters
+ *
+ * NOTE: [[HoodiePruneFileSourcePartitions]] is a replica in kind to Spark's
[[PruneFileSourcePartitions]]
+ */
+case class HoodiePruneFileSourcePartitions(spark: SparkSession) extends
Rule[LogicalPlan] {
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+ case op @ PhysicalOperation(projects, filters, lr @
LogicalRelation(HoodieRelationMatcher(fileIndex), _, _, _))
+ if sparkAdapter.isHoodieTable(lr, spark) && filters.nonEmpty &&
fileIndex.partitionSchema.nonEmpty =>
+
+ val deterministicFilters = filters.filter(f => f.deterministic &&
!SubqueryExpression.hasSubquery(f))
+ val normalizedFilters = exprUtils.normalizeExprs(deterministicFilters,
lr.output)
+
+ val (partitionPruningFilters, _) =
+ getPartitionFiltersAndDataFilters(fileIndex.partitionSchema,
normalizedFilters)
+
+ // NOTE: We should only push-down the predicates [[HoodieFileIndex]],
which we didn't
+ // prune on before
+ if (partitionPruningFilters.nonEmpty &&
!fileIndex.prunedFor(partitionPruningFilters)) {
Review Comment:
This check is only needed here to gate against re-entry -- w/o it this rule
will be looping ad infinitum
--
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]