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
Next Next commit
SPARK-27411: DataSourceV2Strategy should not eliminate subquery
  • Loading branch information
francis0407 committed Apr 9, 2019
commit af74d517312b01c2061b719aa52f5b5bca597ae8
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ object DataSourceV2Strategy extends Strategy with PredicateHelper {
case PhysicalOperation(project, filters, relation: DataSourceV2Relation) =>
val scanBuilder = relation.newScanBuilder()

val (withSubquery, withoutSubquery) = filters.partition(SubqueryExpression.hasSubquery)
val normalizedFilters = DataSourceStrategy.normalizeFilters(
filters.filterNot(SubqueryExpression.hasSubquery), relation.output)
withoutSubquery, relation.output)

// `pushedFilters` will be pushed down and evaluated in the underlying data sources.
// `postScanFilters` need to be evaluated after the scan.
// `postScanFilters` and `pushedFilters` can overlap, e.g. the parquet row group filter.
val (pushedFilters, postScanFilters) = pushFilters(scanBuilder, normalizedFilters)
val (pushedFilters, postScanFiltersWithoutSubquery) =
pushFilters(scanBuilder, normalizedFilters)
val postScanFilters = postScanFiltersWithoutSubquery ++ withSubquery
val (scan, output) = pruneColumns(scanBuilder, relation, project ++ postScanFilters)
logInfo(
s"""
Expand All @@ -121,7 +124,6 @@ object DataSourceV2Strategy extends Strategy with PredicateHelper {
|Post-Scan Filters: ${postScanFilters.mkString(",")}
|Output: ${output.mkString(", ")}
""".stripMargin)

val plan = BatchScanExec(output, scan)

val filterCondition = postScanFilters.reduceLeftOption(And)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ class DataSourceV2Suite extends QueryTest with SharedSQLContext {
}
}
}

test("SPARK-27411: DataSourceV2Strategy should not eliminate subquery") {
val t2 = spark.read.format(classOf[SimpleDataSourceV2].getName).load()
sql("create temporary view t1 (a int) using parquet")
Copy link
Contributor

Choose a reason for hiding this comment

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

let's wrap the test with withTempView, so that the view can be cleaned up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

val df = t2.where("i < (select max(a) from t1)")
val subqueries = df.queryExecution.executedPlan.collect {
case p => p.subqueries
}.flatten
assert(subqueries.length == 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's also check the result. I think this is a correctness bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, thanks.

}
}


Expand Down