-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-37038][SQL] DSV2 Sample Push Down #34451
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
Changes from all commits
43aba9b
ceffb92
e4fe27c
4628cf7
8e2a837
293f233
20f1a7f
4d22d3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,17 +22,22 @@ import java.util | |
| import org.apache.log4j.Level | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.DataFrame | ||
| import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NoSuchIndexException} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Filter, Sample} | ||
| import org.apache.spark.sql.connector.catalog.{Catalogs, Identifier, TableCatalog} | ||
| import org.apache.spark.sql.connector.catalog.index.SupportsIndex | ||
| import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference} | ||
| import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2ScanRelation, V1ScanWrapper} | ||
| import org.apache.spark.sql.jdbc.DockerIntegrationFunSuite | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.tags.DockerTest | ||
|
|
||
| @DockerTest | ||
| private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFunSuite { | ||
| import testImplicits._ | ||
|
|
||
| val catalogName: String | ||
| // dialect specific update column type test | ||
| def testUpdateColumnType(tbl: String): Unit | ||
|
|
@@ -284,4 +289,109 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu | |
| testIndexUsingSQL(s"$catalogName.new_table") | ||
| } | ||
| } | ||
|
|
||
| def supportsTableSample: Boolean = false | ||
|
|
||
| private def samplePushed(df: DataFrame): Boolean = { | ||
| val sample = df.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| sample.isEmpty | ||
| } | ||
|
|
||
| private def filterPushed(df: DataFrame): Boolean = { | ||
| val filter = df.queryExecution.optimizedPlan.collect { | ||
| case f: Filter => f | ||
| } | ||
| filter.isEmpty | ||
| } | ||
|
|
||
| private def limitPushed(df: DataFrame, limit: Int): Boolean = { | ||
| val filter = df.queryExecution.optimizedPlan.collect { | ||
| case relation: DataSourceV2ScanRelation => relation.scan match { | ||
| case v1: V1ScanWrapper => | ||
| return v1.pushedDownOperators.limit == Some(limit) | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| private def columnPruned(df: DataFrame, col: String): Boolean = { | ||
| val scan = df.queryExecution.optimizedPlan.collectFirst { | ||
| case s: DataSourceV2ScanRelation => s | ||
| }.get | ||
| scan.schema.names.sameElements(Seq(col)) | ||
| } | ||
|
|
||
| test("SPARK-37038: Test TABLESAMPLE") { | ||
| if (supportsTableSample) { | ||
huaxingao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| withTable(s"$catalogName.new_table") { | ||
| sql(s"CREATE TABLE $catalogName.new_table (col1 INT, col2 INT)") | ||
| spark.range(10).select($"id" * 2, $"id" * 2 + 1).write.insertInto(s"$catalogName.new_table") | ||
|
|
||
| // sample push down + column pruning | ||
| val df1 = sql(s"SELECT col1 FROM $catalogName.new_table TABLESAMPLE (BUCKET 6 OUT OF 10)" + | ||
| " REPEATABLE (12345)") | ||
| assert(samplePushed(df1)) | ||
| assert(columnPruned(df1, "col1")) | ||
| assert(df1.collect().length < 10) | ||
|
|
||
| // sample push down only | ||
| val df2 = sql(s"SELECT * FROM $catalogName.new_table TABLESAMPLE (50 PERCENT)" + | ||
| " REPEATABLE (12345)") | ||
| assert(samplePushed(df2)) | ||
| assert(df2.collect().length < 10) | ||
|
|
||
| // sample(BUCKET ... OUT OF) push down + limit push down + column pruning | ||
| val df3 = sql(s"SELECT col1 FROM $catalogName.new_table TABLESAMPLE (BUCKET 6 OUT OF 10)" + | ||
| " LIMIT 2") | ||
| assert(samplePushed(df3)) | ||
| assert(limitPushed(df3, 2)) | ||
| assert(columnPruned(df3, "col1")) | ||
| assert(df3.collect().length == 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think should be <=2, as the TABLESAMPLE is not repeatable and may only produce one row.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another way is to always specify the seed in table sample tests. @huaxingao can you help fix this? I don't have a strong opinion on which approach is better. |
||
|
|
||
| // sample(... PERCENT) push down + limit push down + column pruning | ||
| val df4 = sql(s"SELECT col1 FROM $catalogName.new_table" + | ||
| " TABLESAMPLE (50 PERCENT) REPEATABLE (12345) LIMIT 2") | ||
| assert(samplePushed(df4)) | ||
| assert(limitPushed(df4, 2)) | ||
| assert(columnPruned(df4, "col1")) | ||
| assert(df4.collect().length == 2) | ||
|
|
||
| // sample push down + filter push down + limit push down | ||
| val df5 = sql(s"SELECT * FROM $catalogName.new_table" + | ||
| " TABLESAMPLE (BUCKET 6 OUT OF 10) WHERE col1 > 0 LIMIT 2") | ||
| assert(samplePushed(df5)) | ||
| assert(filterPushed(df5)) | ||
| assert(limitPushed(df5, 2)) | ||
| assert(df5.collect().length == 2) | ||
|
|
||
| // sample + filter + limit + column pruning | ||
| // sample pushed down, filer/limit not pushed down, column pruned | ||
| // Todo: push down filter/limit | ||
| val df6 = sql(s"SELECT col1 FROM $catalogName.new_table" + | ||
| " TABLESAMPLE (BUCKET 6 OUT OF 10) WHERE col1 > 0 LIMIT 2") | ||
| assert(samplePushed(df6)) | ||
| assert(!filterPushed(df6)) | ||
| assert(!limitPushed(df6, 2)) | ||
| assert(columnPruned(df6, "col1")) | ||
| assert(df6.collect().length == 2) | ||
|
|
||
| // sample + limit | ||
| // Push down order is sample -> filter -> limit | ||
| // only limit is pushed down because in this test sample is after limit | ||
| val df7 = spark.read.table(s"$catalogName.new_table").limit(2).sample(0.5) | ||
| assert(!samplePushed(df7)) | ||
| assert(limitPushed(df7, 2)) | ||
|
|
||
| // sample + filter | ||
| // Push down order is sample -> filter -> limit | ||
| // only filter is pushed down because in this test sample is after filter | ||
| val df8 = spark.read.table(s"$catalogName.new_table").where($"col1" > 1).sample(0.5) | ||
| assert(!samplePushed(df8)) | ||
| assert(filterPushed(df8)) | ||
| assert(df8.collect().length < 10) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.connector.read; | ||
|
|
||
| import org.apache.spark.annotation.Evolving; | ||
|
|
||
| /** | ||
| * A mix-in interface for {@link Scan}. Data sources can implement this interface to | ||
| * push down SAMPLE. | ||
| * | ||
| * @since 3.3.0 | ||
| */ | ||
| @Evolving | ||
| public interface SupportsPushDownTableSample extends ScanBuilder { | ||
|
|
||
| /** | ||
| * Pushes down SAMPLE to the data source. | ||
| */ | ||
| boolean pushTableSample( | ||
| double lowerBound, | ||
| double upperBound, | ||
| boolean withReplacement, | ||
| long seed); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,9 +31,9 @@ import org.apache.spark.sql.catalyst.expressions._ | |
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning, UnknownPartitioning} | ||
| import org.apache.spark.sql.catalyst.util.truncatedString | ||
| import org.apache.spark.sql.connector.expressions.aggregate.Aggregation | ||
| import org.apache.spark.sql.execution.datasources._ | ||
| import org.apache.spark.sql.execution.datasources.parquet.{ParquetFileFormat => ParquetSource} | ||
| import org.apache.spark.sql.execution.datasources.v2.PushedDownOperators | ||
| import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.sources.{BaseRelation, Filter} | ||
|
|
@@ -103,8 +103,7 @@ case class RowDataSourceScanExec( | |
| requiredSchema: StructType, | ||
| filters: Set[Filter], | ||
| handledFilters: Set[Filter], | ||
| aggregation: Option[Aggregation], | ||
| limit: Option[Int], | ||
| pushedDownOperators: PushedDownOperators, | ||
| rdd: RDD[InternalRow], | ||
| @transient relation: BaseRelation, | ||
| tableIdentifier: Option[TableIdentifier]) | ||
|
|
@@ -135,9 +134,9 @@ case class RowDataSourceScanExec( | |
|
|
||
| def seqToString(seq: Seq[Any]): String = seq.mkString("[", ", ", "]") | ||
|
|
||
| val (aggString, groupByString) = if (aggregation.nonEmpty) { | ||
| (seqToString(aggregation.get.aggregateExpressions), | ||
| seqToString(aggregation.get.groupByColumns)) | ||
| val (aggString, groupByString) = if (pushedDownOperators.aggregation.nonEmpty) { | ||
| (seqToString(pushedDownOperators.aggregation.get.aggregateExpressions), | ||
| seqToString(pushedDownOperators.aggregation.get.groupByColumns)) | ||
| } else { | ||
| ("[]", "[]") | ||
| } | ||
|
|
@@ -155,7 +154,10 @@ case class RowDataSourceScanExec( | |
| "PushedFilters" -> seqToString(markedFilters.toSeq), | ||
| "PushedAggregates" -> aggString, | ||
| "PushedGroupby" -> groupByString) ++ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not have the above two entries if agg is not pushed. We can fix it in a followup. |
||
| limit.map(value => "PushedLimit" -> s"LIMIT $value") | ||
| pushedDownOperators.limit.map(value => "PushedLimit" -> s"LIMIT $value") ++ | ||
| pushedDownOperators.sample.map(v => "PushedSample" -> | ||
| s"SAMPLE (${(v.upperBound - v.lowerBound) * 100}) ${v.withReplacement} SEED(${v.seed})" | ||
| ) | ||
| } | ||
|
|
||
| // Don't care about `rdd` and `tableIdentifier` when canonicalizing. | ||
|
|
||
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.
This test is flaky:
https://github.com/apache/spark/runs/4259414295?check_suite_focus=true
I saw this few times but retriggered all of them so I can't fine now 😢 . I am going to monitor this a bit more but thought it's worth mentioning :-).