-
Notifications
You must be signed in to change notification settings - Fork 539
[spark] partition filter pushdown for non-lake log and kv tables #3240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fresh-borzoni
merged 2 commits into
apache:main
from
fresh-borzoni:feat/spark-partition-filter-pushdown
May 9, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...ss-spark-common/src/main/scala/org/apache/fluss/spark/utils/SparkPartitionPredicate.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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.fluss.spark.utils | ||
|
|
||
| import org.apache.fluss.metadata.{PartitionInfo, TableInfo} | ||
| import org.apache.fluss.predicate.{CompoundPredicate, LeafPredicate, PartitionPredicateVisitor, Predicate => FlussPredicate, PredicateBuilder, PredicateVisitor} | ||
| import org.apache.fluss.row.{BinaryString, GenericRow} | ||
| import org.apache.fluss.types.{DataTypes, RowType} | ||
| import org.apache.fluss.utils.PartitionUtils | ||
|
|
||
| import org.apache.spark.sql.connector.expressions.filter.Predicate | ||
|
|
||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| /** Extracts a partition-key predicate and prunes the partition list at planning time. */ | ||
| object SparkPartitionPredicate { | ||
|
|
||
| def extract(tableInfo: TableInfo, predicates: Seq[Predicate]): Option[FlussPredicate] = { | ||
| val partitionKeys = tableInfo.getPartitionKeys | ||
| if (partitionKeys.isEmpty) return None | ||
|
|
||
| val rowType = partitionRowType(tableInfo) | ||
| val onlyPartitionKeys = new PartitionPredicateVisitor(partitionKeys) | ||
|
|
||
| val converted = predicates.flatMap { | ||
| sparkPredicate => | ||
| SparkPredicateConverter | ||
| .convert(rowType, sparkPredicate) | ||
| .filter(_.visit(onlyPartitionKeys)) | ||
| .map(stringifyLiterals) | ||
| } | ||
|
|
||
| converted match { | ||
| case Seq() => None | ||
| case Seq(single) => Some(single) | ||
| case many => Some(PredicateBuilder.and(many.asJava)) | ||
| } | ||
| } | ||
|
|
||
| def filterPartitions( | ||
| partitionInfos: Seq[PartitionInfo], | ||
| partitionPredicate: Option[FlussPredicate]): Seq[PartitionInfo] = | ||
| partitionPredicate match { | ||
| case None => partitionInfos | ||
| case Some(predicate) => partitionInfos.filter(p => predicate.test(toPartitionRow(p))) | ||
| } | ||
|
|
||
| private def partitionRowType(tableInfo: TableInfo): RowType = { | ||
| val schemaRowType = tableInfo.getRowType | ||
| val fieldNames = schemaRowType.getFieldNames | ||
| val partitionFieldIndexes = tableInfo.getPartitionKeys.asScala.map(fieldNames.indexOf).toArray | ||
| schemaRowType.project(partitionFieldIndexes) | ||
| } | ||
|
|
||
| private def toPartitionRow(partitionInfo: PartitionInfo): GenericRow = { | ||
| val values = partitionInfo.getResolvedPartitionSpec.getPartitionValues | ||
| val row = new GenericRow(values.size) | ||
| var i = 0 | ||
| while (i < values.size) { | ||
| row.setField(i, BinaryString.fromString(values.get(i))) | ||
| i += 1 | ||
| } | ||
| row | ||
| } | ||
|
|
||
| // Partition values are stored as strings; literals must be coerced before evaluation. | ||
| private val stringifier: PredicateVisitor[FlussPredicate] = new PredicateVisitor[FlussPredicate] { | ||
| override def visit(leaf: LeafPredicate): FlussPredicate = { | ||
| val converted: Seq[Object] = leaf.literals.asScala.toSeq.map { | ||
| case null => null | ||
| case literal => | ||
| BinaryString.fromString( | ||
| PartitionUtils.convertValueOfType(literal, leaf.`type`.getTypeRoot)) | ||
| } | ||
| new LeafPredicate( | ||
| leaf.function, | ||
| DataTypes.STRING, | ||
| leaf.index, | ||
| leaf.fieldName, | ||
| converted.asJava) | ||
| } | ||
|
|
||
| override def visit(compound: CompoundPredicate): FlussPredicate = { | ||
| val children = compound.children.asScala.map(_.visit(this)).asJava | ||
| new CompoundPredicate(compound.function, children) | ||
| } | ||
| } | ||
|
|
||
| private def stringifyLiterals(predicate: FlussPredicate): FlussPredicate = | ||
| predicate.visit(stringifier) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, toPartitionRow materializes all partition values as BinaryString, and stringifyLiterals rebuilds every LeafPredicate with DataTypes.STRING.
After that, predicate evaluation runs through
fluss-common/src/main/java/org/apache/fluss/predicate/LeafPredicate.javaandfluss-common/src/main/java/org/apache/fluss/predicate/CompareUtils.java, which means comparisons are performed using the predicate type.So for a non-string partition column, range predicates are no longer evaluated with the real type semantics, but with string lexicographic semantics.
A concrete incorrect case is an INT partition column. Suppose partitions include pt2=2 and pt2=10, and Spark pushes pt2 > 2. With correct integer semantics, partition 10 must be kept. With the current implementation, both the row value and the literal become strings, so "10" is compared to "2" lexicographically, and "10" < "2", causing partition 10 to be pruned incorrectly.
That is a correctness bug, not just a missed optimization, because the partition is skipped before scan.
Seems Flink also have similar issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observation, I'm thinking to lift this to fluss-common and use PartitionUtils.parseValueOfType in both Spark/Flink.
Created an issue to fix this: #3292