-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-37038][SQL][WIP] DSV2 Sample Push Down #34311
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
94198c9
751578c
0dc45e6
28c392c
0d7ddbd
1ee1105
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,6 +23,7 @@ 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} | ||
|
|
@@ -284,4 +285,32 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu | |
| testIndexUsingSQL(s"$catalogName.new_table") | ||
| } | ||
| } | ||
|
|
||
| def supportsTableSample: Boolean = false | ||
|
|
||
| test("Test TABLESAMPLE") { | ||
| withTable(s"$catalogName.new_table") { | ||
| sql(s"CREATE TABLE $catalogName.new_table (col1 INT, col2 INT)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (1, 2)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (3, 4)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (5, 6)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (7, 8)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (9, 10)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (11, 12)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (13, 14)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (15, 16)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (17, 18)") | ||
| sql(s"INSERT INTO TABLE $catalogName.new_table values (19, 20)") | ||
| if (supportsTableSample) { | ||
|
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. If |
||
| val df = sql(s"SELECT * FROM $catalogName.new_table TABLESAMPLE (BUCKET 6 OUT OF 10)" + | ||
| s" REPEATABLE (12345)") | ||
| df.explain(true) | ||
| val sample = df.queryExecution.optimizedPlan.collect { | ||
| case s: Sample => s | ||
| } | ||
| assert(sample.isEmpty) | ||
| assert(df.collect().length <= 7) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -674,7 +674,7 @@ joinCriteria | |
| ; | ||
|
|
||
| sample | ||
| : TABLESAMPLE '(' sampleMethod? ')' | ||
| : TABLESAMPLE '(' sampleMethod? ')' (REPEATABLE '('seed=INTEGER_VALUE')')? | ||
|
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 make a separate PR for this SQL syntax change?
Contributor
Author
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. submitted #34442 for syntax change |
||
| ; | ||
|
|
||
| sampleMethod | ||
|
|
@@ -1194,6 +1194,7 @@ ansiNonReserved | |
| | REFRESH | ||
| | RENAME | ||
| | REPAIR | ||
| | REPEATABLE | ||
| | REPLACE | ||
| | RESET | ||
| | RESPECT | ||
|
|
@@ -1460,6 +1461,7 @@ nonReserved | |
| | REFRESH | ||
| | RENAME | ||
| | REPAIR | ||
| | REPEATABLE | ||
| | REPLACE | ||
| | RESET | ||
| | RESPECT | ||
|
|
@@ -1726,6 +1728,7 @@ REFERENCES: 'REFERENCES'; | |
| REFRESH: 'REFRESH'; | ||
| RENAME: 'RENAME'; | ||
| REPAIR: 'REPAIR'; | ||
| REPEATABLE: 'REPEATABLE'; | ||
| REPLACE: 'REPLACE'; | ||
| RESET: 'RESET'; | ||
| RESPECT: 'RESPECT'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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.expressions; | ||
|
|
||
| import org.apache.spark.annotation.Experimental; | ||
|
|
||
| /** | ||
| * Represents a TableSample in the public expression API. | ||
| * | ||
| * @since 3.3.0 | ||
| */ | ||
| @Experimental | ||
| public interface TableSample extends Expression { | ||
|
|
||
| /** | ||
| * Returns the sample method name. | ||
| */ | ||
| String methodName(); | ||
|
|
||
| /** | ||
| * Returns the lower-bound of the sampling probability (usually 0.0). | ||
| */ | ||
| double lowerBound(); | ||
|
|
||
| /** | ||
| * Returns the upper-bound of the sampling probability. The expected fraction sampled | ||
| * will be ub - lb. | ||
| */ | ||
| double upperBound(); | ||
|
|
||
| /** | ||
| * Returns whether to sample with replacement. | ||
| */ | ||
| boolean withReplacement(); | ||
|
|
||
| /** | ||
| * Returns the random seed. | ||
| */ | ||
| long seed(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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; | ||
| import org.apache.spark.sql.connector.expressions.TableSample; | ||
|
|
||
| /** | ||
| * 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(TableSample limit); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,15 @@ private[sql] object LogicalExpressions { | |
| nullOrdering: NullOrdering): SortOrder = { | ||
| SortValue(reference, direction, nullOrdering) | ||
| } | ||
|
|
||
| def tableSample( | ||
| methodName: String, | ||
| lowerBound: Double, | ||
| upperBound: Double, | ||
| withReplacement: Boolean, | ||
| seed: Long): TableSample = { | ||
| TableSampleValue(methodName: String, lowerBound, upperBound, withReplacement, seed) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -357,3 +366,14 @@ private[sql] object SortValue { | |
| None | ||
| } | ||
| } | ||
|
|
||
| private[sql] final case class TableSampleValue( | ||
| methodName: String, | ||
| lowerBound: Double, | ||
| upperBound: Double, | ||
| withReplacement: Boolean, | ||
| seed: Long) extends TableSample { | ||
|
|
||
| override def describe(): String = s"$methodName $lowerBound $lowerBound $upperBound" + | ||
|
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. two |
||
| s" $withReplacement $seed" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ 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.TableSample | ||
| 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} | ||
|
|
@@ -104,6 +105,7 @@ case class RowDataSourceScanExec( | |
| filters: Set[Filter], | ||
| handledFilters: Set[Filter], | ||
| aggregation: Option[Aggregation], | ||
| sample: Option[TableSample], | ||
|
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. So many pushdown related parameters, would it be better if they were wrapped by some parent case class? |
||
| rdd: RDD[InternalRow], | ||
| @transient relation: BaseRelation, | ||
| tableIdentifier: Option[TableIdentifier]) | ||
|
|
@@ -153,7 +155,10 @@ case class RowDataSourceScanExec( | |
| "ReadSchema" -> requiredSchema.catalogString, | ||
| "PushedFilters" -> seqToString(markedFilters.toSeq), | ||
| "PushedAggregates" -> aggString, | ||
| "PushedGroupby" -> groupByString) | ||
| "PushedGroupby" -> groupByString) ++ | ||
| sample.map(v => "PushedSample" -> | ||
| s"SAMPLE ${v.methodName} ${v.lowerBound} ${v.upperBound} ${v.withReplacement} ${v.seed}" | ||
| ) | ||
| } | ||
|
|
||
| // Don't care about `rdd` and `tableIdentifier` when canonicalizing. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,6 +336,7 @@ object DataSourceStrategy | |
| Set.empty, | ||
| Set.empty, | ||
| None, | ||
| null, | ||
|
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 here should be |
||
| toCatalystRDD(l, baseRelation.buildScan()), | ||
| baseRelation, | ||
| None) :: Nil | ||
|
|
@@ -410,6 +411,7 @@ object DataSourceStrategy | |
| pushedFilters.toSet, | ||
| handledFilters, | ||
| None, | ||
| null, | ||
| scanBuilder(requestedColumns, candidatePredicates, pushedFilters), | ||
| relation.relation, | ||
| relation.catalogTable.map(_.identifier)) | ||
|
|
@@ -433,6 +435,7 @@ object DataSourceStrategy | |
| pushedFilters.toSet, | ||
| handledFilters, | ||
| None, | ||
| null, | ||
| scanBuilder(requestedColumns, candidatePredicates, pushedFilters), | ||
| relation.relation, | ||
| relation.catalogTable.map(_.identifier)) | ||
|
|
||
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.
not grasping all context, so this work requires the underlying DS support sample expression?
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.
Yes. It requires the underlying support.
Sampleis not part of the ANSI SQL standard so not all data sources support it.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.
what about the treatment for csv parquet format, will it make difference when sampling is pushed down to scan ? would that be supported ?
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.
It should work if you make parquet scan or csv scan implement the interface
SupportsPushDownTableSample, but I am not sure how parquet or csv handles sample.