HyukjinKwon commented on code in PR #59943:
URL: https://github.com/apache/spark/pull/59943#discussion_r4465613507
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -1185,6 +1194,32 @@ class MicroBatchExecution(
}
}
+ /**
+ * Replaces the current time and date in the expressions of `plan` with the
time of the
+ * micro-batch whose timestamp is `batchTimestampMs`, which the optimizer
does not fold.
+ */
+ private def withBatchTimestamp(plan: LogicalPlan, batchTimestampMs: Long):
LogicalPlan = {
Review Comment:
Fixed in 9ef62c1. The plan optimized at the start no longer has the values
of the time: `StreamingV2ScanPushDown.reads` replaces every expression with the
pattern `CURRENT_LIKE` (`current_timestamp()`, `now()`, `current_date()`,
`current_time()`, `localtimestamp()`, their nanosecond variants,
`current_timezone()`, `current_database()`, ...) and the casts of times to
timestamps, which take the current date (the ones that `ComputeCurrentTime`
rewrites), with a placeholder that keeps their children. The optimizer does not
take the placeholder as deterministic, as it does not take the current time of
a micro-batch, and no filter that has one is pushed down. The time substitution
of `MicroBatchExecution` is back as it is on master.
The new tests "do not push down a filter on a value that depends on when it
is evaluated: ..." check each of them, and "do not push down a filter on the
current time of a micro-batch" runs `ts < now() AND id > 0` and `ts <
current_timestamp() AND id > 0`, which only push `id > 0` down. With the
previous commit, the new check of the pushed filters (see dongjoon-hyun's
comment) fails the first micro-batch of the `now()` query instead of dropping
rows.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamingV2ScanPushDown.scala:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.execution.streaming.runtime
+
+import scala.collection.mutable
+import scala.util.control.NonFatal
+
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference,
AttributeSet, Expression, ExpressionSet, SubqueryExpression}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.RuleExecutor
+import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.connector.catalog.SupportsRead
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder,
SupportsPushDownFilters, SupportsPushDownRequiredColumns,
SupportsPushDownV2Filters}
+import org.apache.spark.sql.connector.read.streaming.SparkDataStream
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.PushDownUtils
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.internal.connector.SupportsPushDownCatalystFilters
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Pushes the top-level columns and the filters that the optimized plan of a
streaming query reads
+ * from each Data Source V2 streaming source down to the scan of the source, as
+ * `V2ScanRelationPushDown` does for the scans of batch queries.
+ *
+ * The stream of a source is created from its scan, and has to exist before
any micro-batch is
+ * planned, so the scan is built when the query starts. The query is optimized
once at that point,
+ * by the optimizer of its micro-batches and with their configuration, and the
scan of each source
+ * gets what the planner takes from the optimized plan for the relation of the
source (see
+ * `DataSourceV2Strategy`): the projections and the filters over each
occurrence of the relation.
+ *
+ * The plan of the query does not change, so each micro-batch is optimized as
without these
+ * pushdowns: the optimizer keeps deciding which columns every operator gets,
and in which order,
+ * including the stateful ones, and Spark still evaluates all the filters over
the relation. Only
+ * the scan node of the physical plan outputs the columns that the scan reads,
and the planner
+ * checks that the micro-batch reads no other column of the relation.
+ *
+ * The other operations that the scans of batch queries can apply, such as
aggregates and limits,
+ * apply to the data of all the micro-batches of a streaming query, which the
scan of a micro-batch
+ * does not read, so they are not pushed down.
+ */
+private[sql] object StreamingV2ScanPushDown extends Logging {
+
+ /**
+ * What the optimized plan of a streaming query reads from a streaming
relation.
+ *
+ * @param requiredColumns the columns of the relation that the plan reads
+ * @param filters the deterministic filters without subqueries that the plan
applies over every
+ * occurrence of the relation, which its scan may apply too
+ */
+ case class Reads(requiredColumns: AttributeSet, filters: Seq[Expression])
+
+ /**
+ * Returns what the optimized `plan` reads from each of its streaming
relations of Data Source
+ * V2: what the planner takes from the projections and the filters over each
occurrence of the
+ * relation, or all its columns and no filter where nothing is projected or
filtered over it. A
+ * relation that the optimized plan does not have, or any relation if the
optimizer fails, has no
+ * entry, so that its scan reads all its columns and gets no filter.
+ *
+ * @param optimizer the optimizer of the micro-batches of the query
+ * @param conf the configuration that the micro-batches of the query run with
+ * @param plan the plan of the query, with the current time of a micro-batch
as the micro-batches
+ * have it, which the optimizer does not fold, so that no filter
on it is pushed down
+ */
+ def reads(
+ optimizer: RuleExecutor[LogicalPlan],
+ conf: SQLConf,
+ plan: LogicalPlan): Map[StreamingRelationV2, Reads] = {
+ SQLConf.withExistingConf(conf) {
+ try {
+ val occurrences = mutable.HashMap.empty[StreamingRelationV2,
mutable.Buffer[Reads]]
+ // Visits the nodes as the planner plans them: the projections and the
filters over a
+ // relation are planned with its scan, and the other nodes on their
own.
+ def visit(node: LogicalPlan): Unit = node match {
+ case PhysicalOperation(project, filters, relation:
StreamingRelationV2) =>
+ occurrences.getOrElseUpdate(relation, mutable.Buffer.empty) +=
Reads(
Review Comment:
I looked into this one, and the optimizer does not copy streaming relations:
`InlineCTE` (as well as `ReplaceCTERefWithRepartition` and
`PushDownJoinThroughUnion`) copies the plan with
`DeduplicateRelations.deduplicateRight`, whose `renewDuplicatedRelations`
returns a streaming plan as it is (`case p: LogicalPlan if p.isStreaming =>
(plan, false)`). So both references to the CTE have the same
`StreamingRelationV2`, with the same attribute IDs, and the second one gets a
`Project` that aliases its output. Both occurrences are combined for that
relation, as in the previous commit.
The new test "read the columns and apply the filters of every reference to a
common table expression" runs your example on the test table (`WITH c AS
(SELECT * FROM s) SELECT data FROM c WHERE extra > 15 UNION ALL SELECT data
FROM c WHERE extra < 15`): no filter is pushed down, and both branches return
their rows. It also runs dongjoon-hyun's self-join, whose scan reads `extra`
for the second reference. In case a later rule copies a streaming relation,
`reads` now pushes nothing down for the query when its optimized plan has a
streaming relation that the analyzed plan does not have. The planner checks of
the columns and the filters would also fail loudly rather than drop rows.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamingV2ScanPushDown.scala:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.execution.streaming.runtime
+
+import scala.collection.mutable
+import scala.util.control.NonFatal
+
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference,
AttributeSet, Expression, ExpressionSet, SubqueryExpression}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.RuleExecutor
+import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.connector.catalog.SupportsRead
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder,
SupportsPushDownFilters, SupportsPushDownRequiredColumns,
SupportsPushDownV2Filters}
+import org.apache.spark.sql.connector.read.streaming.SparkDataStream
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.PushDownUtils
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.internal.connector.SupportsPushDownCatalystFilters
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Pushes the top-level columns and the filters that the optimized plan of a
streaming query reads
+ * from each Data Source V2 streaming source down to the scan of the source, as
+ * `V2ScanRelationPushDown` does for the scans of batch queries.
+ *
+ * The stream of a source is created from its scan, and has to exist before
any micro-batch is
+ * planned, so the scan is built when the query starts. The query is optimized
once at that point,
+ * by the optimizer of its micro-batches and with their configuration, and the
scan of each source
+ * gets what the planner takes from the optimized plan for the relation of the
source (see
+ * `DataSourceV2Strategy`): the projections and the filters over each
occurrence of the relation.
+ *
+ * The plan of the query does not change, so each micro-batch is optimized as
without these
+ * pushdowns: the optimizer keeps deciding which columns every operator gets,
and in which order,
+ * including the stateful ones, and Spark still evaluates all the filters over
the relation. Only
+ * the scan node of the physical plan outputs the columns that the scan reads,
and the planner
+ * checks that the micro-batch reads no other column of the relation.
+ *
+ * The other operations that the scans of batch queries can apply, such as
aggregates and limits,
+ * apply to the data of all the micro-batches of a streaming query, which the
scan of a micro-batch
+ * does not read, so they are not pushed down.
+ */
+private[sql] object StreamingV2ScanPushDown extends Logging {
+
+ /**
+ * What the optimized plan of a streaming query reads from a streaming
relation.
+ *
+ * @param requiredColumns the columns of the relation that the plan reads
+ * @param filters the deterministic filters without subqueries that the plan
applies over every
+ * occurrence of the relation, which its scan may apply too
+ */
+ case class Reads(requiredColumns: AttributeSet, filters: Seq[Expression])
+
+ /**
+ * Returns what the optimized `plan` reads from each of its streaming
relations of Data Source
+ * V2: what the planner takes from the projections and the filters over each
occurrence of the
+ * relation, or all its columns and no filter where nothing is projected or
filtered over it. A
+ * relation that the optimized plan does not have, or any relation if the
optimizer fails, has no
+ * entry, so that its scan reads all its columns and gets no filter.
+ *
+ * @param optimizer the optimizer of the micro-batches of the query
+ * @param conf the configuration that the micro-batches of the query run with
+ * @param plan the plan of the query, with the current time of a micro-batch
as the micro-batches
+ * have it, which the optimizer does not fold, so that no filter
on it is pushed down
+ */
+ def reads(
+ optimizer: RuleExecutor[LogicalPlan],
+ conf: SQLConf,
+ plan: LogicalPlan): Map[StreamingRelationV2, Reads] = {
+ SQLConf.withExistingConf(conf) {
+ try {
+ val occurrences = mutable.HashMap.empty[StreamingRelationV2,
mutable.Buffer[Reads]]
+ // Visits the nodes as the planner plans them: the projections and the
filters over a
+ // relation are planned with its scan, and the other nodes on their
own.
+ def visit(node: LogicalPlan): Unit = node match {
+ case PhysicalOperation(project, filters, relation:
StreamingRelationV2) =>
+ occurrences.getOrElseUpdate(relation, mutable.Buffer.empty) +=
Reads(
+ AttributeSet((project ++ filters).flatMap(_.references)),
+ filters.filter(f => f.deterministic &&
!SubqueryExpression.hasSubquery(f)))
+ case _ =>
+ node.children.foreach(visit)
+ }
+ visit(optimizer.execute(plan))
+ occurrences.map { case (relation, reads) =>
+ // The scan of a relation reads the rows of all its occurrences, so
it can only apply the
+ // filters that all of them apply.
+ val filters = reads.map(r => ExpressionSet(r.filters)).reduce {
(left, right) =>
+ ExpressionSet(left.filter(right.contains))
+ }
+ relation -> Reads(
+ AttributeSet.fromAttributeSets(reads.map(_.requiredColumns)),
filters.toSeq)
+ }.toMap
+ } catch {
+ case NonFatal(e) =>
+ logWarning(log"Could not optimize the streaming query to push down
what it reads to " +
+ log"the scans of its sources, which read all their columns with no
filter.", e)
+ Map.empty
+ }
+ }
+ }
+
+ /**
+ * Builds the scan of a streaming source, and creates its stream with
`createStream`. If its
+ * scan builder supports it, and `conf` enables it, the scan only reads the
columns of `output`
+ * that the query reads, and gets the filters that the query applies to all
the rows that it reads
+ * from the source (see `reads`). `reads` is only evaluated for such a scan
builder. Returns the
+ * scan, its stream, and the columns of `output` that the scan reads, in the
order of its read
+ * schema, unless these are `output` itself.
+ *
+ * A pruned scan must read the required columns and only columns of
`output`, each once, as
+ * `SupportsPushDownRequiredColumns` requires. A scan that does not is
replaced by a scan of all
+ * the columns, as without pruning, and its stream is stopped. Spark asks a
scan for its read
+ * schema once the stream is created, since a scan may only know then what
its stream reads.
+ *
+ * @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 reads what the query reads from the relation, or None to read all
its columns
+ */
+ def buildScan[S <: SparkDataStream](
+ table: SupportsRead,
+ options: CaseInsensitiveStringMap,
+ output: Seq[AttributeReference],
+ conf: SQLConf,
+ reads: => Option[Reads])(
+ createStream: Scan => S): (Scan, S, Option[Seq[AttributeReference]]) = {
+ val builder = table.newScanBuilder(options)
+ val prunesColumns = conf.streamingV2ColumnPruningEnabled &&
+ builder.isInstanceOf[SupportsPushDownRequiredColumns]
+ val pushesFilters = conf.streamingV2FilterPushdownEnabled &&
supportsFilters(builder)
+ val known = if (prunesColumns || pushesFilters) reads else None
+ val requiredOutput = known.filter(_ => prunesColumns)
+ .map(r => output.filter(r.requiredColumns.contains))
+ .getOrElse(output)
+ val filters = known.filter(_ => pushesFilters)
+ .map(r => DataSourceStrategy.normalizeExprs(r.filters, output))
+ .getOrElse(Nil)
+
+ def build(builder: ScanBuilder, columns: Seq[AttributeReference]): Scan = {
+ if (filters.nonEmpty) {
+ // The plan of the query keeps the filters, which Spark evaluates
after the scan, so the
+ // scan may apply any of them.
+ PushDownUtils.pushFilters(builder, filters, partitionFields = None)
Review Comment:
Fixed in 9ef62c1: a `PythonScanBuilder` gets no filter for a streaming scan
(`supportsFilters`), since a Python data source pushes filters down with the
reader of its batch scans, which a streaming read does not use. It does not
implement `SupportsPushDownRequiredColumns` either, so a streaming read of a
Python data source no longer runs the startup optimization at all. The new test
in `PythonStreamingDataSourceSuite` filters the stream of a source that only
has a stream reader, with `spark.sql.python.filterPushdown.enabled`; without
the fix, it fails with `PySparkNotImplementedError: [NOT_IMPLEMENTED] reader is
not implemented`. The PR description and the migration guide now say that
Python data sources get no filter.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamingV2ScanPushDown.scala:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.execution.streaming.runtime
+
+import scala.collection.mutable
+import scala.util.control.NonFatal
+
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference,
AttributeSet, Expression, ExpressionSet, SubqueryExpression}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.RuleExecutor
+import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.connector.catalog.SupportsRead
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder,
SupportsPushDownFilters, SupportsPushDownRequiredColumns,
SupportsPushDownV2Filters}
+import org.apache.spark.sql.connector.read.streaming.SparkDataStream
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.PushDownUtils
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.internal.connector.SupportsPushDownCatalystFilters
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Pushes the top-level columns and the filters that the optimized plan of a
streaming query reads
+ * from each Data Source V2 streaming source down to the scan of the source, as
+ * `V2ScanRelationPushDown` does for the scans of batch queries.
+ *
+ * The stream of a source is created from its scan, and has to exist before
any micro-batch is
+ * planned, so the scan is built when the query starts. The query is optimized
once at that point,
+ * by the optimizer of its micro-batches and with their configuration, and the
scan of each source
+ * gets what the planner takes from the optimized plan for the relation of the
source (see
+ * `DataSourceV2Strategy`): the projections and the filters over each
occurrence of the relation.
+ *
+ * The plan of the query does not change, so each micro-batch is optimized as
without these
+ * pushdowns: the optimizer keeps deciding which columns every operator gets,
and in which order,
+ * including the stateful ones, and Spark still evaluates all the filters over
the relation. Only
+ * the scan node of the physical plan outputs the columns that the scan reads,
and the planner
+ * checks that the micro-batch reads no other column of the relation.
+ *
+ * The other operations that the scans of batch queries can apply, such as
aggregates and limits,
+ * apply to the data of all the micro-batches of a streaming query, which the
scan of a micro-batch
+ * does not read, so they are not pushed down.
+ */
+private[sql] object StreamingV2ScanPushDown extends Logging {
+
+ /**
+ * What the optimized plan of a streaming query reads from a streaming
relation.
+ *
+ * @param requiredColumns the columns of the relation that the plan reads
+ * @param filters the deterministic filters without subqueries that the plan
applies over every
+ * occurrence of the relation, which its scan may apply too
+ */
+ case class Reads(requiredColumns: AttributeSet, filters: Seq[Expression])
+
+ /**
+ * Returns what the optimized `plan` reads from each of its streaming
relations of Data Source
+ * V2: what the planner takes from the projections and the filters over each
occurrence of the
+ * relation, or all its columns and no filter where nothing is projected or
filtered over it. A
+ * relation that the optimized plan does not have, or any relation if the
optimizer fails, has no
+ * entry, so that its scan reads all its columns and gets no filter.
+ *
+ * @param optimizer the optimizer of the micro-batches of the query
+ * @param conf the configuration that the micro-batches of the query run with
+ * @param plan the plan of the query, with the current time of a micro-batch
as the micro-batches
+ * have it, which the optimizer does not fold, so that no filter
on it is pushed down
+ */
+ def reads(
+ optimizer: RuleExecutor[LogicalPlan],
+ conf: SQLConf,
+ plan: LogicalPlan): Map[StreamingRelationV2, Reads] = {
+ SQLConf.withExistingConf(conf) {
+ try {
+ val occurrences = mutable.HashMap.empty[StreamingRelationV2,
mutable.Buffer[Reads]]
+ // Visits the nodes as the planner plans them: the projections and the
filters over a
+ // relation are planned with its scan, and the other nodes on their
own.
+ def visit(node: LogicalPlan): Unit = node match {
+ case PhysicalOperation(project, filters, relation:
StreamingRelationV2) =>
+ occurrences.getOrElseUpdate(relation, mutable.Buffer.empty) +=
Reads(
Review Comment:
Thanks for the example. It is now a test ("read the columns and apply the
filters of every reference to a common table expression"), and it passes: the
scan reads `id`, `data` and `extra`, no filter is pushed down, and the join
returns `(1, 10)` and then `(3, 30)`. The optimizer does not copy streaming
relations: `InlineCTE` and `ReplaceCTERefWithRepartition` copy with
`DeduplicateRelations.deduplicateRight`, whose `renewDuplicatedRelations`
returns a streaming plan as it is (`case p: LogicalPlan if p.isStreaming =>
(plan, false)`). So both `a` and `b` have the same `StreamingRelationV2`, with
the same attribute IDs (`b` under a `Project` that aliases its output), and
both occurrences are combined for it, as in the previous commit. The same test
runs a union of disjoint filters over the CTE, which pushes nothing down.
In case a later rule copies a streaming relation, `reads` now pushes nothing
down for the query when its optimized plan has a streaming relation that the
analyzed plan does not have, and the new check of the pushed filters (your
other comment) would fail rather than drop rows.
--
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]