Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,30 @@ private[sql] object DataSourceStrategy extends Strategy with Logging {
// Scanning partitioned HadoopFsRelation
case PhysicalOperation(projects, filters, l @ LogicalRelation(t: HadoopFsRelation))
if t.partitionSpec.partitionColumns.nonEmpty =>
val selectedPartitions = prunePartitions(filters, t.partitionSpec).toArray
// We divide the filter expressions into 3 parts
val partitionColumnNames = t.partitionSpec.partitionColumns.map(_.name).toSet
val filterMap = filters.groupBy { f =>
// TODO this is case-senstive
val referencedColumnNames = f.references.map(_.name).toSet
if (referencedColumnNames.subsetOf(partitionColumnNames)) {
// Only reference the partition key
0
} else if (referencedColumnNames.intersect(partitionColumnNames).isEmpty) {
// Not reference any partition key at all. can be push down
1
} else {
// Reference both partition key and attributes
2
}
}
// Only prunning the partition keys
val partitionFilters = filterMap.getOrElse(0, Nil)
// Only pushes down predicates that do not reference partition keys.
val pushedFilters = filterMap.getOrElse(1, Nil)
// Predicates with both partition keys and attributes
val combineFilters = filterMap.getOrElse(2, Nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using groupBy, can we just use filter 3 times to split these 3 kinds of filters? I think performance doesn't matter here.


val selectedPartitions = prunePartitions(partitionFilters, t.partitionSpec).toArray

logInfo {
val total = t.partitionSpec.partitions.length
Expand All @@ -71,21 +94,16 @@ private[sql] object DataSourceStrategy extends Strategy with Logging {
s"Selected $selected partitions out of $total, pruned $percentPruned% partitions."
}

// Only pushes down predicates that do not reference partition columns.
val pushedFilters = {
val partitionColumnNames = t.partitionSpec.partitionColumns.map(_.name).toSet
filters.filter { f =>
val referencedColumnNames = f.references.map(_.name).toSet
referencedColumnNames.intersect(partitionColumnNames).isEmpty
}
}

buildPartitionedTableScan(
val scan = buildPartitionedTableScan(
l,
projects,
pushedFilters,
t.partitionSpec.partitionColumns,
selectedPartitions) :: Nil
selectedPartitions)

combineFilters
.reduceLeftOption(expressions.And)
.map(execution.Filter(_, scan)).getOrElse(scan) :: Nil

// Scanning non-partitioned HadoopFsRelation
case PhysicalOperation(projects, filters, l @ LogicalRelation(t: HadoopFsRelation)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,21 @@ class ParquetFilterSuite extends QueryTest with ParquetTest with SharedSQLContex
}
}
}

test("SPARK-10829: Filter combine partition key and attribute doesn't work in DataSource scan") {
import testImplicits._

withSQLConf(SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key -> "true") {
withTempPath { dir =>
val path = s"${dir.getCanonicalPath}/part=1"
(1 to 3).map(i => (i, i.toString)).toDF("a", "b").write.parquet(path)

// If the "part = 1" filter gets pushed down, this query will throw an exception since
// "part" is not a valid column in the actual Parquet file
checkAnswer(
sqlContext.read.parquet(path).filter("a > 0 and (part = 0 or a > 1)"),
(2 to 3).map(i => Row(i, i.toString, 1)))
}
}
}
}