HyukjinKwon commented on code in PR #60129:
URL: https://github.com/apache/spark/pull/60129#discussion_r4504036268
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamingV2ScanPushDown.scala:
##########
@@ -294,118 +383,201 @@ private[sql] object StreamingV2ScanPushDown extends
Logging {
/**
* Builds the scan of a streaming source, and creates its stream with
`createStream`. If the
- * table of the source has the capability `STREAMING_COLUMN_PRUNING`, its
scan builder supports
- * the pushdown, and `conf` enables it, the scan only reads the columns of
`output` that the query
- * reads (see `requiredColumnsOfSources`). `requiredColumns` is only
evaluated for such a scan.
- * Otherwise, the scan reads all the columns of `output`, as the scans of
streaming queries did
- * before this pushdown.
+ * table of the source has the capability `STREAMING_COLUMN_PRUNING` or
+ * `STREAMING_FILTER_PUSHDOWN`, its scan builder supports the pushdown, and
`conf` enables it,
+ * the scan only reads the columns of `output` that the query reads, or gets
the filters that the
+ * query applies to all the rows that it reads from the source (see
`columnsAndFiltersOfSources`).
+ * `reads` is only evaluated for such a scan. Otherwise, the scan reads all
the columns of
+ * `output` with no filter, as the scans of streaming queries did before
these pushdowns.
*
- * The streams of the scans of a table with the capability return the
columns of the read
- * schemas of the scans, in their order, so Spark asks each scan of such a
table for its read
+ * The streams of the scans of a table with `STREAMING_COLUMN_PRUNING`
return the columns of the
+ * read schemas of the scans, in their order, so Spark asks each scan of
such a table for its read
* schema before it creates the stream, whether the scan reads all the
columns or not. A pruned
* scan must read the required columns and only columns of `output`, each
once and with its
* type, as `SupportsPushDownRequiredColumns` requires. If the scan builder
or the scan fails
- * with the pruning, or the read schema of a pruned scan cannot be read or
does not follow
- * `SupportsPushDownRequiredColumns`, a scan of all the columns is built
instead, from a new scan
- * builder, as without this pushdown. If the read schema of a scan of all
the columns of such a
- * table does not follow the relation, the query fails with
STREAMING_SCAN_INVALID_READ_SCHEMA,
- * unless `conf` disables the column pruning, with which the scan node
outputs the columns of
- * the relation, as it did before the capability.
+ * with the pushdowns, or the read schema of a pruned scan cannot be read or
does not follow
+ * `SupportsPushDownRequiredColumns`, the scan is built again from a new
scan builder: with the
+ * column pruning alone and then with the filters alone if it had both, and
then with all the
+ * columns and no filter, as without these pushdowns. If the read schema of
a scan of all the
+ * columns of such a table, with or without filters, does not follow the
relation, the query
+ * fails with STREAMING_SCAN_INVALID_READ_SCHEMA, unless `conf` disables the
column pruning, with
+ * which the scan node outputs the columns of the relation, as it did before
the capability.
*
* The stream is created once, from the scan that the source reads, outside
of the fallback: a
* stream can start to use the checkpoint of the source, or other resources,
before it fails, so
- * a failure of `createStream` fails the query, as it did without this
pushdown.
+ * a failure of `createStream` fails the query, as it did without these
pushdowns.
*
* @param output the output of the relation of the source, with the metadata
columns that the
* query reads, which the analyzer added to it, so that
connectors implementing
* `SupportsMetadataColumns` include them in `readSchema()`
* @param conf the configuration of the query
- * @param requiredColumns the columns that the query reads from the
relation, or None to read
- * all its columns
+ * @param reads what the query reads from the relation, or None to read all
its columns with no
+ * filter
* @param source the name of the source in the errors of its scan, see
`sourceName`
*/
def buildScan(
table: SupportsRead,
options: CaseInsensitiveStringMap,
output: Seq[AttributeReference],
conf: SQLConf,
- requiredColumns: => Option[AttributeSet],
+ reads: => Option[Reads],
source: Seq[String])(
createStream: Scan => SparkDataStream): StreamingScan = {
- val returnsReadSchema =
table.capabilities().contains(TableCapability.STREAMING_COLUMN_PRUNING)
+ val capabilities = table.capabilities()
+ val returnsReadSchema =
capabilities.contains(TableCapability.STREAMING_COLUMN_PRUNING)
val builder = table.newScanBuilder(options)
val prunesColumns = returnsReadSchema &&
conf.streamingV2ColumnPruningEnabled &&
builder.isInstanceOf[SupportsPushDownRequiredColumns]
- val requiredOutput = (if (prunesColumns) requiredColumns else None)
- .map(columns => output.filter(columns.contains))
+ val pushesFilters = conf.streamingV2FilterPushdownEnabled &&
+ capabilities.contains(TableCapability.STREAMING_FILTER_PUSHDOWN) &&
+ PushDownUtils.supportsFilters(builder)
+ val known = if (prunesColumns || pushesFilters) reads else None
+ val requiredOutput = known.filter(_ => prunesColumns)
+ .map(r => output.filter(r.requiredColumns.contains))
.getOrElse(output)
+ // The filters that the scan builder gets in some form, which each
micro-batch applies first:
Review Comment:
Done in 9a090d6: both comments now say that the streams may apply the pushed
filters first, to the rows of the source, as the Scaladoc of `Reads.filters`
does. The test is now named "push down no filter that may fail after a filter
that is not pushed down", which matches its V1 case.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala:
##########
@@ -2527,6 +2527,19 @@ private[sql] object QueryExecutionErrors extends
QueryErrorsBase with ExecutionE
cause = null)
}
+ def streamingScanFiltersChangedError(
+ source: Seq[String],
+ filters: Seq[String],
+ cause: Throwable = null): Throwable = {
+ new SparkException(
+ errorClass = "STREAMING_SCAN_FILTERS_CHANGED",
+ messageParameters = Map(
+ "filters" -> filters.mkString(", "),
Review Comment:
Done in 9a090d6: `streamingScanFiltersChangedError` now takes `filters:
Seq[Expression]` and renders them with `toSQLExpr`, so the test expects
`"(extra < 1)"`.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamingV2ScanPushDown.scala:
##########
@@ -294,118 +383,201 @@ private[sql] object StreamingV2ScanPushDown extends
Logging {
/**
* Builds the scan of a streaming source, and creates its stream with
`createStream`. If the
- * table of the source has the capability `STREAMING_COLUMN_PRUNING`, its
scan builder supports
- * the pushdown, and `conf` enables it, the scan only reads the columns of
`output` that the query
- * reads (see `requiredColumnsOfSources`). `requiredColumns` is only
evaluated for such a scan.
- * Otherwise, the scan reads all the columns of `output`, as the scans of
streaming queries did
- * before this pushdown.
+ * table of the source has the capability `STREAMING_COLUMN_PRUNING` or
+ * `STREAMING_FILTER_PUSHDOWN`, its scan builder supports the pushdown, and
`conf` enables it,
+ * the scan only reads the columns of `output` that the query reads, or gets
the filters that the
+ * query applies to all the rows that it reads from the source (see
`columnsAndFiltersOfSources`).
+ * `reads` is only evaluated for such a scan. Otherwise, the scan reads all
the columns of
+ * `output` with no filter, as the scans of streaming queries did before
these pushdowns.
*
- * The streams of the scans of a table with the capability return the
columns of the read
- * schemas of the scans, in their order, so Spark asks each scan of such a
table for its read
+ * The streams of the scans of a table with `STREAMING_COLUMN_PRUNING`
return the columns of the
+ * read schemas of the scans, in their order, so Spark asks each scan of
such a table for its read
* schema before it creates the stream, whether the scan reads all the
columns or not. A pruned
* scan must read the required columns and only columns of `output`, each
once and with its
* type, as `SupportsPushDownRequiredColumns` requires. If the scan builder
or the scan fails
- * with the pruning, or the read schema of a pruned scan cannot be read or
does not follow
- * `SupportsPushDownRequiredColumns`, a scan of all the columns is built
instead, from a new scan
- * builder, as without this pushdown. If the read schema of a scan of all
the columns of such a
- * table does not follow the relation, the query fails with
STREAMING_SCAN_INVALID_READ_SCHEMA,
- * unless `conf` disables the column pruning, with which the scan node
outputs the columns of
- * the relation, as it did before the capability.
+ * with the pushdowns, or the read schema of a pruned scan cannot be read or
does not follow
+ * `SupportsPushDownRequiredColumns`, the scan is built again from a new
scan builder: with the
+ * column pruning alone and then with the filters alone if it had both, and
then with all the
+ * columns and no filter, as without these pushdowns. If the read schema of
a scan of all the
+ * columns of such a table, with or without filters, does not follow the
relation, the query
+ * fails with STREAMING_SCAN_INVALID_READ_SCHEMA, unless `conf` disables the
column pruning, with
+ * which the scan node outputs the columns of the relation, as it did before
the capability.
*
* The stream is created once, from the scan that the source reads, outside
of the fallback: a
* stream can start to use the checkpoint of the source, or other resources,
before it fails, so
- * a failure of `createStream` fails the query, as it did without this
pushdown.
+ * a failure of `createStream` fails the query, as it did without these
pushdowns.
*
* @param output the output of the relation of the source, with the metadata
columns that the
* query reads, which the analyzer added to it, so that
connectors implementing
* `SupportsMetadataColumns` include them in `readSchema()`
* @param conf the configuration of the query
- * @param requiredColumns the columns that the query reads from the
relation, or None to read
- * all its columns
+ * @param reads what the query reads from the relation, or None to read all
its columns with no
+ * filter
* @param source the name of the source in the errors of its scan, see
`sourceName`
*/
def buildScan(
table: SupportsRead,
options: CaseInsensitiveStringMap,
output: Seq[AttributeReference],
conf: SQLConf,
- requiredColumns: => Option[AttributeSet],
+ reads: => Option[Reads],
source: Seq[String])(
createStream: Scan => SparkDataStream): StreamingScan = {
- val returnsReadSchema =
table.capabilities().contains(TableCapability.STREAMING_COLUMN_PRUNING)
+ val capabilities = table.capabilities()
+ val returnsReadSchema =
capabilities.contains(TableCapability.STREAMING_COLUMN_PRUNING)
val builder = table.newScanBuilder(options)
val prunesColumns = returnsReadSchema &&
conf.streamingV2ColumnPruningEnabled &&
builder.isInstanceOf[SupportsPushDownRequiredColumns]
- val requiredOutput = (if (prunesColumns) requiredColumns else None)
- .map(columns => output.filter(columns.contains))
+ val pushesFilters = conf.streamingV2FilterPushdownEnabled &&
+ capabilities.contains(TableCapability.STREAMING_FILTER_PUSHDOWN) &&
+ PushDownUtils.supportsFilters(builder)
+ val known = if (prunesColumns || pushesFilters) reads else None
+ val requiredOutput = known.filter(_ => prunesColumns)
+ .map(r => output.filter(r.requiredColumns.contains))
.getOrElse(output)
+ // The filters that the scan builder gets in some form, which each
micro-batch applies first:
+ // the others, such as a filter with a UDF that cannot be translated to a
filter of the source,
+ // are evaluated after them, as for the scans of batch queries, rather
than on all the rows of
+ // the source. The filters of a `SupportsPushDownFilters` builder compare
columns with values,
+ // which cannot fail, so it gets those that all the reads apply, wherever
they are. The
+ // predicates of a `SupportsPushDownV2Filters` builder and the filters of
the others may fail
+ // on some rows, as a cast does with ANSI, so they are the leading filters
of the reads, up to
+ // the first that the builder does not get, in their order, which the
streams evaluate them in
+ // (see `TableCapability.STREAMING_FILTER_PUSHDOWN`): none runs before a
filter written before
+ // it. `PushDownUtils.pushFilters` decides which filters the builder gets
in the same way.
+ def gets(filter: Expression): Boolean = PushDownUtils.getsFilter(builder,
filter)
+ val filters = known.filter(_ => pushesFilters)
+ .map { r =>
+ builder match {
+ case _: SupportsPushDownFilters =>
+ DataSourceStrategy.normalizeExprs(r.filtersInAnyOrder,
output).filter(gets)
+ case _ =>
+ DataSourceStrategy.normalizeExprs(r.filters,
output).takeWhile(gets)
+ }
+ }
+ .getOrElse(Nil)
- // Builds the scan of `columns` with `builder`.
- def build(builder: ScanBuilder, columns: Seq[AttributeReference]): Scan = {
+ // Builds the scan of `columns` with `builder`, once it pushed `filters`
down to it.
+ def build(
+ builder: ScanBuilder,
+ columns: Seq[AttributeReference],
+ filters: Seq[Expression]): Scan = {
+ if (filters.nonEmpty) {
+ // Each micro-batch checks that its plan applies these filters to all
the rows that it
+ // reads from the source, see `checkPushedFilters`, so the scan may
apply any of them, in
+ // any form.
+ val (pushed, _) = PushDownUtils.pushFilters(builder, filters,
partitionFields = None)
+ val taken = pushed.fold(_.mkString(", "), _.mkString(", "))
+ logInfo(log"The scan builder of the streaming table " +
+ log"${MDC(LogKeys.STREAMING_TABLE, table)} took the filters " +
+ log"${MDC(LogKeys.PUSHED_FILTERS, taken)}.")
+ }
builder match {
case pruning: SupportsPushDownRequiredColumns =>
pruning.pruneColumns(DataTypeUtils.fromAttributes(columns))
case _ =>
}
builder.build()
}
- // The scan of all the columns, as without this pushdown, and the columns
that its scan node
- // outputs.
- def scanOfAllColumns(builder: ScanBuilder): (Scan,
Seq[AttributeReference]) = {
- val scan = build(builder, output)
+ // The columns that the scan node of `scan`, a scan of all the columns,
outputs.
+ def outputOfAllColumns(scan: Scan): Seq[AttributeReference] = {
if (returnsReadSchema) {
// The readers of the stream return the columns of the read schema, so
a read schema that
// does not follow the relation fails the query when it starts: the
scan node cannot
// output the columns of the relation in its place. Without the column
pruning, the scan
// node outputs them instead, as it did before the capability.
- def invalid(reason: String, cause: Throwable): (Scan,
Seq[AttributeReference]) = {
+ def invalid(reason: String, cause: Throwable): Seq[AttributeReference]
= {
if (conf.streamingV2ColumnPruningEnabled) {
throw
QueryExecutionErrors.streamingScanInvalidReadSchemaError(source, reason, cause)
}
logWarning(log"The read schema of the scan of all the columns of the
streaming table " +
log"${MDC(LogKeys.STREAMING_TABLE, table)} ${MDC(LogKeys.REASON,
reason)}. Its scan " +
log"node outputs the columns of the relation, as the column
pruning is disabled.")
- scan -> output
+ output
}
Try(readOutput(scan.readSchema(), output, output)) match {
- case Success(Right(columns)) => scan -> columns
+ case Success(Right(columns)) => columns
case Success(Left(reason)) => invalid(reason, null)
case Failure(e) => invalid(s"could not be read: $e", e)
}
} else {
- scan -> output
+ output
}
}
-
- val (scan, scanOutput) = if (requiredOutput.length == output.length) {
- scanOfAllColumns(builder)
- } else {
- // Logs why the pruned scan could not be built, and builds the scan of
all the columns
- // instead.
- def fallBack(reason: String, cause: Option[Throwable]): (Scan,
Seq[AttributeReference]) = {
- val message = log"The scan of the streaming table " +
- log"${MDC(LogKeys.STREAMING_TABLE, table)} could not be built to
read only the columns " +
- log"${MDC(LogKeys.COLUMN_NAMES,
requiredOutput.map(_.name).mkString(", "))}: " +
- log"${MDC(LogKeys.REASON, reason)}. It reads all the columns
instead."
- cause match {
- case Some(e) => logWarning(message, e)
- case None => logWarning(message)
- }
- scanOfAllColumns(table.newScanBuilder(options))
+ // The scan of all the columns with no filter, as without these pushdowns,
and the columns that
+ // its scan node outputs.
+ def scanOfAllColumns(builder: ScanBuilder): (Scan,
Seq[AttributeReference]) = {
+ val scan = build(builder, output, Nil)
+ scan -> outputOfAllColumns(scan)
+ }
+ // The scan of `columns` with `filters` and the columns that its scan node
outputs, or why its
+ // read schema does not follow `SupportsPushDownRequiredColumns`. A scan
of all the columns
+ // with filters is checked as one with no filter, outside of the fallback
(see `firstScan`), as
+ // its read schema fails the query in the same way: the columns of such a
scan are None.
+ def scanWith(
+ builder: ScanBuilder,
+ columns: Seq[AttributeReference],
+ filters: Seq[Expression]): Either[String, (Scan,
Option[Seq[AttributeReference]])] = {
+ val scan = build(builder, columns, filters)
+ if (!returnsReadSchema) {
Review Comment:
Done in 9a090d6, both. The `!returnsReadSchema` branch of `scanWith` is
gone, and `hasExactEquality` now always accepts the floating types, with a doc
that says why. `values` takes `hasExactEquality(t) && (isFloating(t) ||
hasNoFloatingZero(v, t))`.
##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingV2ScanPushDownSuite.scala:
##########
@@ -84,8 +97,15 @@ class StreamingV2ScanPushDownSuite extends StreamTest with
BeforeAndAfter {
}
/** The columns that a streaming query needs from each of its sources, when
it starts. */
Review Comment:
Done in 9a090d6: `reads` now has its own Scaladoc, the doc of
`requiredColumns` is back above it, and the doc of `createTable` says
"capabilities".
##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingV2ScanPushDownSuite.scala:
##########
@@ -1175,6 +2279,27 @@ private class PruningContinuousReaderFactory(
}
}
+/** A table whose scan builder takes V2 predicates, which it records. */
+private class V2FilterTable extends Table with SupportsRead {
Review Comment:
Done in 9a090d6:
- The test of the leading V2 predicates now uses `FilterStreamTable(...,
catalyst = false)`, and `V2FilterTable` is gone.
- `UnknownColumnFilterTable` is now `UnknownColumnReadSchemaTable(input,
takesFilters = true)`.
- The repeated `AssertOnQuery` after `assertPushedFilters()` is gone.
- The inline-table test now uses `requiredColumns` and `pushed`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]