Skip to content
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

[SPARK-38978][SQL] DS V2 supports push down OFFSET operator #36295

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -23,7 +23,8 @@
* An interface for building the {@link Scan}. Implementations can mixin SupportsPushDownXYZ
* interfaces to do operator push down, and keep the operator push down result in the returned
* {@link Scan}. When pushing down operators, the push down order is:
* sample -> filter -> aggregate -> limit -> column pruning.
* sample -> filter -> aggregate -> limit/top-n(sort + limit) -> offset ->
* column pruning.
*
* @since 3.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

/**
* A mix-in interface for {@link ScanBuilder}. Data sources can implement this interface to
* push down LIMIT. Please note that the combination of LIMIT with other operations
* such as AGGREGATE, GROUP BY, SORT BY, CLUSTER BY, DISTRIBUTE BY, etc. is NOT pushed down.
* push down LIMIT. We can push down LIMIT with many other operations if they follow the
* operator order we defined in {@link ScanBuilder}'s class doc.
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 open a backport PR for 3.3 to fix the classdoc later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

*
* @since 3.3.0
*/
Expand Down
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;

/**
* A mix-in interface for {@link ScanBuilder}. Data sources can implement this interface to
* push down OFFSET. We can push down OFFSET with many other operations if they follow the
* operator order we defined in {@link ScanBuilder}'s class doc.
*
* @since 3.4.0
*/
@Evolving
public interface SupportsPushDownOffset extends ScanBuilder {

/**
* Pushes down OFFSET to the data source.
*/
boolean pushOffset(int offset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@

/**
* A mix-in interface for {@link ScanBuilder}. Data sources can implement this interface to
* push down top N(query with ORDER BY ... LIMIT n). Please note that the combination of top N
* with other operations such as AGGREGATE, GROUP BY, CLUSTER BY, DISTRIBUTE BY, etc.
* is NOT pushed down.
* push down top N(query with ORDER BY ... LIMIT n). We can push down top N with many other
* operations if they follow the operator order we defined in {@link ScanBuilder}'s class doc.
*
* @since 3.3.0
*/
@Evolving
public interface SupportsPushDownTopN extends ScanBuilder {

/**
* Pushes down top N to the data source.
*/
boolean pushTopN(SortOrder[] orders, int limit);
/**
* Pushes down top N to the data source.
*/
boolean pushTopN(SortOrder[] orders, int limit);

/**
* Whether the top N is partially pushed or not. If it returns true, then Spark will do top N
* again. This method will only be called when {@link #pushTopN} returns true.
*/
default boolean isPartiallyPushed() { return true; }
/**
* Whether the top N is partially pushed or not. If it returns true, then Spark will do top N
* again. This method will only be called when {@link #pushTopN} returns true.
*/
default boolean isPartiallyPushed() { return true; }
}
2 changes: 1 addition & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ class Dataset[T] private[sql](
}

/**
* Returns a new Dataset by skipping the first `m` rows.
* Returns a new Dataset by skipping the first `n` rows.
*
* @group typedrel
* @since 3.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ case class RowDataSourceScanExec(
s"ORDER BY ${seqToString(pushedDownOperators.sortValues.map(_.describe()))}" +
s" LIMIT ${pushedDownOperators.limit.get}"
Some("PushedTopN" -> pushedTopN)
} else {
pushedDownOperators.limit.map(value => "PushedLimit" -> s"LIMIT $value")
}
} else {
pushedDownOperators.limit.map(value => "PushedLimit" -> s"LIMIT $value")
}

val offsetInfo = pushedDownOperators.offset.map(value => "PushedOffset" -> s"OFFSET $value")

val pushedFilters = if (pushedDownOperators.pushedPredicates.nonEmpty) {
seqToString(pushedDownOperators.pushedPredicates.map(_.describe()))
Expand All @@ -164,6 +166,7 @@ case class RowDataSourceScanExec(
Map("PushedAggregates" -> seqToString(v.aggregateExpressions.map(_.describe())),
"PushedGroupByExpressions" -> seqToString(v.groupByExpressions.map(_.describe())))} ++
topNOrLimitInfo ++
offsetInfo ++
pushedDownOperators.sample.map(v => "PushedSample" ->
s"SAMPLE (${(v.upperBound - v.lowerBound) * 100}) ${v.withReplacement} SEED(${v.seed})"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ object DataSourceStrategy
l.output.toStructType,
Set.empty,
Set.empty,
PushedDownOperators(None, None, None, Seq.empty, Seq.empty),
PushedDownOperators(None, None, None, None, Seq.empty, Seq.empty),
toCatalystRDD(l, baseRelation.buildScan()),
baseRelation,
None) :: Nil
Expand Down Expand Up @@ -420,7 +420,7 @@ object DataSourceStrategy
requestedColumns.toStructType,
pushedFilters.toSet,
handledFilters,
PushedDownOperators(None, None, None, Seq.empty, Seq.empty),
PushedDownOperators(None, None, None, None, Seq.empty, Seq.empty),
scanBuilder(requestedColumns, candidatePredicates, pushedFilters),
relation.relation,
relation.catalogTable.map(_.identifier))
Expand All @@ -443,7 +443,7 @@ object DataSourceStrategy
requestedColumns.toStructType,
pushedFilters.toSet,
handledFilters,
PushedDownOperators(None, None, None, Seq.empty, Seq.empty),
PushedDownOperators(None, None, None, None, Seq.empty, Seq.empty),
scanBuilder(requestedColumns, candidatePredicates, pushedFilters),
relation.relation,
relation.catalogTable.map(_.identifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ class JDBCOptions(
// This only applies to Data Source V2 JDBC
val pushDownLimit = parameters.getOrElse(JDBC_PUSHDOWN_LIMIT, "false").toBoolean

// An option to allow/disallow pushing down OFFSET into V2 JDBC data source
// This only applies to Data Source V2 JDBC
val pushDownOffset = parameters.getOrElse(JDBC_PUSHDOWN_OFFSET, "false").toBoolean

// An option to allow/disallow pushing down TABLESAMPLE into JDBC data source
// This only applies to Data Source V2 JDBC
val pushDownTableSample = parameters.getOrElse(JDBC_PUSHDOWN_TABLESAMPLE, "false").toBoolean
Expand Down Expand Up @@ -283,6 +287,7 @@ object JDBCOptions {
val JDBC_PUSHDOWN_PREDICATE = newOption("pushDownPredicate")
val JDBC_PUSHDOWN_AGGREGATE = newOption("pushDownAggregate")
val JDBC_PUSHDOWN_LIMIT = newOption("pushDownLimit")
val JDBC_PUSHDOWN_OFFSET = newOption("pushDownOffset")
val JDBC_PUSHDOWN_TABLESAMPLE = newOption("pushDownTableSample")
val JDBC_KEYTAB = newOption("keytab")
val JDBC_PRINCIPAL = newOption("principal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ object JDBCRDD extends Logging {
groupByColumns: Option[Array[String]] = None,
sample: Option[TableSampleInfo] = None,
limit: Int = 0,
sortOrders: Array[String] = Array.empty[String]): RDD[InternalRow] = {
sortOrders: Array[String] = Array.empty[String],
offset: Int = 0): RDD[InternalRow] = {
val url = options.url
val dialect = JdbcDialects.get(url)
val quotedColumns = if (groupByColumns.isEmpty) {
Expand All @@ -145,7 +146,8 @@ object JDBCRDD extends Logging {
groupByColumns,
sample,
limit,
sortOrders)
sortOrders,
offset)
}
// scalastyle:on argcount
}
Expand All @@ -167,7 +169,8 @@ private[jdbc] class JDBCRDD(
groupByColumns: Option[Array[String]],
sample: Option[TableSampleInfo],
limit: Int,
sortOrders: Array[String])
sortOrders: Array[String],
offset: Int)
extends RDD[InternalRow](sc, Nil) {

/**
Expand Down Expand Up @@ -305,10 +308,11 @@ private[jdbc] class JDBCRDD(
}

val myLimitClause: String = dialect.getLimitClause(limit)
val myOffsetClause: String = dialect.getOffsetClause(offset)

val sqlText = options.prepareQuery +
s"SELECT $columnList FROM ${options.tableOrQuery} $myTableSampleClause" +
s" $myWhereClause $getGroupByClause $getOrderByClause $myLimitClause"
s" $myWhereClause $getGroupByClause $getOrderByClause $myLimitClause $myOffsetClause"
stmt = conn.prepareStatement(sqlText,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(options.fetchSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ private[sql] case class JDBCRelation(
groupByColumns: Option[Array[String]],
tableSample: Option[TableSampleInfo],
limit: Int,
sortOrders: Array[String]): RDD[Row] = {
sortOrders: Array[String],
offset: Int): RDD[Row] = {
// Rely on a type erasure hack to pass RDD[InternalRow] back as RDD[Row]
JDBCRDD.scanTable(
sparkSession.sparkContext,
Expand All @@ -317,7 +318,8 @@ private[sql] case class JDBCRelation(
groupByColumns,
tableSample,
limit,
sortOrders).asInstanceOf[RDD[Row]]
sortOrders,
offset).asInstanceOf[RDD[Row]]
}

override def insert(data: DataFrame, overwrite: Boolean): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.apache.spark.sql.catalyst.expressions.{AttributeReference, AttributeS
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
import org.apache.spark.sql.connector.expressions.SortOrder
import org.apache.spark.sql.connector.expressions.filter.Predicate
import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsPushDownFilters, SupportsPushDownLimit, SupportsPushDownRequiredColumns, SupportsPushDownTableSample, SupportsPushDownTopN, SupportsPushDownV2Filters}
import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsPushDownFilters, SupportsPushDownLimit, SupportsPushDownOffset, SupportsPushDownRequiredColumns, SupportsPushDownTableSample, SupportsPushDownTopN, SupportsPushDownV2Filters}
import org.apache.spark.sql.execution.datasources.DataSourceStrategy
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.sources
Expand Down Expand Up @@ -130,6 +130,19 @@ object PushDownUtils extends PredicateHelper {
}
}

/**
* Pushes down OFFSET to the data source Scan.
*
* @return the Boolean value represents whether to push down.
*/
def pushOffset(scanBuilder: ScanBuilder, offset: Int): Boolean = {
scanBuilder match {
case s: SupportsPushDownOffset =>
s.pushOffset(offset)
case _ => false
}
}

/**
* Pushes down top N to the data source Scan.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ case class PushedDownOperators(
aggregation: Option[Aggregation],
sample: Option[TableSampleInfo],
limit: Option[Int],
offset: Option[Int],
sortValues: Seq[SortOrder],
pushedPredicates: Seq[Predicate]) {
assert((limit.isEmpty && sortValues.isEmpty) || limit.isDefined)
Expand Down
Loading