-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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 3 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 |
|---|---|---|
|
|
@@ -23,16 +23,20 @@ import org.apache.log4j.Level | |
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NoSuchIndexException} | ||
| import org.apache.spark.sql.catalyst.plans.logical.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 +288,83 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu | |
| testIndexUsingSQL(s"$catalogName.new_table") | ||
| } | ||
| } | ||
|
|
||
| def supportsTableSample: Boolean = false | ||
|
|
||
| test("Test TABLESAMPLE") { | ||
| require(supportsTableSample) | ||
| 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") | ||
|
|
||
| val df1 = sql(s"SELECT col1 FROM $catalogName.new_table TABLESAMPLE (BUCKET 6 OUT OF 10)" + | ||
|
cloud-fan marked this conversation as resolved.
Outdated
|
||
| s" REPEATABLE (12345)") | ||
| val scan1 = df1.queryExecution.optimizedPlan.collectFirst { | ||
| case s: DataSourceV2ScanRelation => s | ||
| }.get | ||
| assert(scan1.schema.names.sameElements(Seq("col1"))) | ||
|
|
||
| val sample1 = df1.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample1.isEmpty) | ||
| assert(df1.collect().length <= 7) | ||
|
|
||
| val df2 = sql(s"SELECT * FROM $catalogName.new_table TABLESAMPLE (50 PERCENT)" + | ||
| s" REPEATABLE (12345)") | ||
| val sample2 = df2.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample2.isEmpty) | ||
|
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. can we write a small method for this check?
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 can also add |
||
| assert(df2.collect().length <= 7) | ||
|
|
||
| val df3 = sql(s"SELECT col1 FROM $catalogName.new_table TABLESAMPLE (BUCKET 6 OUT OF 10)" + | ||
| s" LIMIT 2") | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| val sample3 = df3.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample3.isEmpty) | ||
| df3.queryExecution.optimizedPlan.collectFirst { | ||
| case s@DataSourceV2ScanRelation(_, scan, _) => scan match { | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| case v1: V1ScanWrapper => | ||
| assert(v1.pushedDownOperators.limit.nonEmpty && | ||
| v1.pushedDownOperators.limit.get === 2) | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| s.schema.names.sameElements(Seq("col1")) | ||
| } | ||
| } | ||
| assert(df3.collect().length == 2) | ||
|
|
||
| val df4 = sql(s"SELECT col1 FROM $catalogName.new_table" + | ||
| s" TABLESAMPLE (50 PERCENT) REPEATABLE (12345) LIMIT 2") | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| val sample4 = df4.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample4.isEmpty) | ||
| df4.queryExecution.optimizedPlan.collect { | ||
| case s@DataSourceV2ScanRelation(_, scan, _) => scan match { | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| case v1: V1ScanWrapper => | ||
| assert(v1.pushedDownOperators.limit.nonEmpty && | ||
| v1.pushedDownOperators.limit.get === 2) | ||
| s.schema.names.sameElements(Seq("col1")) | ||
| } | ||
| } | ||
| assert(df4.collect().length == 2) | ||
|
|
||
| // Push down order is filter -> sample -> limit | ||
|
huaxingao marked this conversation as resolved.
Outdated
|
||
| // in this test only limit is pushed down because sample is after limit | ||
| // Filter in combination with sample is not allowed so no need to test | ||
| val df5 = spark.read.table(s"$catalogName.new_table").limit(2).sample(0.5) | ||
| val sample5 = df5.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample5.nonEmpty) | ||
| df5.queryExecution.optimizedPlan.collect { | ||
| case DataSourceV2ScanRelation(_, scan, _) => scan match { | ||
| case v1: V1ScanWrapper => | ||
| assert(v1.pushedDownOperators.limit.nonEmpty && | ||
| v1.pushedDownOperators.limit.get === 2) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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.lowerBound} ${v.upperBound} ${v.withReplacement} ${v.seed}" | ||
|
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. can we generate the SAMPLE SQL syntax? |
||
| ) | ||
| } | ||
|
|
||
| // 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.
shall we add a JIRA number