HyukjinKwon commented on code in PR #60129:
URL: https://github.com/apache/spark/pull/60129#discussion_r4489012555


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingV2ScanPushDownSuite.scala:
##########
@@ -0,0 +1,1540 @@
+/*
+ * 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.streaming
+
+import java.util
+import java.util.concurrent.ConcurrentLinkedQueue
+import java.util.concurrent.atomic.{AtomicInteger, AtomicLong}
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.logging.log4j.Level
+import org.scalatest.BeforeAndAfter
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.{DataFrame, Row}
+import org.apache.spark.sql.catalyst.{InternalRow, ProjectingInternalRow}
+import org.apache.spark.sql.catalyst.expressions.{Alias, And, AttributeSet, 
Cast, EvalMode, Expression, GreaterThan, IsNotNull, Literal, RLike}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, Project}
+import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.classic.Dataset
+import org.apache.spark.sql.connector.catalog.{Identifier, InMemoryBaseTable, 
InMemoryTableCatalog, SupportsRead, Table, TableCapability}
+import org.apache.spark.sql.connector.expressions.filter.Predicate
+import org.apache.spark.sql.connector.read.{InputPartition, Scan, ScanBuilder, 
SupportsPushDownFilters, SupportsPushDownRequiredColumns, 
SupportsPushDownV2Filters}
+import 
org.apache.spark.sql.connector.read.streaming.{ContinuousPartitionReader, 
ContinuousPartitionReaderFactory, ContinuousStream, MicroBatchStream, Offset, 
PartitionOffset}
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, 
DataSourceV2Strategy, MicroBatchScanExec, StreamingDataSourceV2ScanRelation}
+import org.apache.spark.sql.execution.streaming.runtime.{MemoryStream, 
StreamExecution, StreamingQueryWrapper, StreamingV2ScanPushDown}
+import org.apache.spark.sql.execution.streaming.sources.ContinuousMemoryStream
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.sources.{Filter => SourceFilter}
+import org.apache.spark.sql.types.{IntegerType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+import org.apache.spark.unsafe.types.UTF8String
+
+class StreamingV2ScanPushDownSuite extends StreamTest with BeforeAndAfter {
+  import testImplicits._
+
+  private val tbl = "testcat.t"
+
+  before {
+    spark.conf.set("spark.sql.catalog.testcat", 
classOf[InMemoryTableCatalog].getName)
+  }
+
+  after {
+    spark.sessionState.catalogManager.reset()
+    spark.conf.unset("spark.sql.catalog.testcat")
+  }
+
+  // The in-memory tables only declare the column pruning by default: the 
tables of this suite
+  // declare the filter pushdown too.
+  private val bothPushdowns = s"'${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 
'columns,filters'"
+  private val pushdowns = s"TBLPROPERTIES ($bothPushdowns)"
+  // Both pushdowns, with streams that drop the rows that fail the pushed 
filters.
+  private val filtering =
+    s"TBLPROPERTIES ($bothPushdowns, 
'${InMemoryBaseTable.SIMULATE_STREAMING_FILTERS}' = 'true')"
+
+  private def withSourceTable(properties: String = pushdowns)(
+      f: => Unit): Unit = {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, extra int) $properties")
+      sql(s"INSERT INTO $tbl VALUES (1, 'a', 10), (2, 'b', 20)")
+      f
+    }
+  }
+
+  private def insert(values: String, table: String = tbl): StreamAction = 
Execute { _ =>
+    sql(s"INSERT INTO $table VALUES $values")
+  }
+
+  private def pruningEnabled(enabled: Boolean): Map[String, String] =
+    Map(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> enabled.toString)
+
+  /**
+   * Runs `f`, which restarts streams with other values of the confs of these 
pushdowns, and then
+   * restores their values: `testStream` restores the value that a conf had 
before the last
+   * `StartStream` that set it, rather than before the first.
+   */
+  private def withPushDownConfsRestored(f: => Unit): Unit = withSQLConf(
+    SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> "true",
+    SQLConf.STREAMING_V2_FILTER_PUSHDOWN_ENABLED.key -> "true")(f)
+
+  private def filterPushdownEnabled(enabled: Boolean): Map[String, String] =
+    Map(SQLConf.STREAMING_V2_FILTER_PUSHDOWN_ENABLED.key -> enabled.toString)
+
+  /** Returns the filters pushed down to the scan of each streaming source of 
the query. */
+  private def pushedFilters(query: StreamExecution): Seq[Seq[String]] = {
+    query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan match {
+          case scan: InMemoryBaseTable#InMemoryBatchScan =>
+            scan.pushedFilters.map(_.toString).toSeq.sorted
+          case scan =>
+            fail(s"The scan $scan of ${relation.name} is not a scan of an 
in-memory table.")
+        }
+    }
+  }
+
+  /** Checks that the filters pushed down to the scan of each streaming source 
are `filters`. */
+  private def assertPushedFilters(filters: String*): StreamAction = 
AssertOnQuery { query =>
+    val pushed = pushedFilters(query)
+    assert(pushed.nonEmpty)
+    pushed.foreach(p => assert(p === filters.sorted))
+    true
+  }
+
+  /** Checks the read schemas of the scans of the streaming sources of the 
query. */
+  private def assertReadSchemas(columns: Seq[String]*): StreamAction = 
AssertOnQuery { query =>
+    val readSchemas = query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan.readSchema().fieldNames.toSeq
+    }
+    assert(readSchemas.sortBy(_.mkString(",")) === 
columns.sortBy(_.mkString(",")))
+    true
+  }
+
+  /** Checks that the scan of each streaming source of the query reads 
`columns`. */
+  private def assertReadSchema(columns: String*): StreamAction = AssertOnQuery 
{ query =>
+    val readSchemas = query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan.readSchema().fieldNames.toSeq
+    }
+    assert(readSchemas.nonEmpty)
+    readSchemas.foreach(readSchema => assert(readSchema === columns))
+    true
+  }
+
+  /** Checks the output of the scan nodes of the last micro-batch. */
+  private def assertScanOutputs(columns: Seq[String]*): StreamAction = 
AssertOnQuery { query =>
+    val outputs = query.lastExecution.executedPlan.collect {
+      case scan: MicroBatchScanExec => scan.output.map(_.name)
+    }
+    assert(outputs.sortBy(_.mkString(",")) === columns.sortBy(_.mkString(",")))
+    true
+  }
+
+  test("read only the columns that a streaming query needs, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        assertScanOutputs(Seq("data", "extra")),
+        CheckAnswer(Row("b")),
+        StopStream,
+        insert("(3, 'c', 30), (4, 'd', 5)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        CheckAnswer(Row("b"), Row("c")))
+    }
+  }
+
+  // Each case is a query over the source, and the columns that the optimizer 
reads for it, which
+  // fewer than those that the operators of its analyzed plan reference.
+  Seq[(String, DataFrame => DataFrame, Seq[String])](
+    ("a column that a later projection drops",
+      _.withColumn("x", lit(1)).select("data"), Seq("data")),
+    ("chained projections", _.select("id", "data").select("data"), 
Seq("data")),
+    ("expressions and functions that a later projection drops",
+      _.select(upper($"data").as("u"), ($"id" * 2).as("i")).select("u"), 
Seq("data")),
+    ("several aliases of a column",
+      _.select($"id".as("x"), $"data".as("y")).select($"x".as("z")).where("z > 
1"), Seq("id")),
+    ("a field of a struct of columns",
+      _.select(struct($"id", $"data").as("s")).select("s.data"), Seq("data")),
+    ("the fields of a struct of columns",
+      _.select(struct($"id", $"extra").as("s")).select("s.*"), Seq("id", 
"extra")),
+    ("a filter between projections",
+      _.select("id", "data").where("id > 1").select("data"), Seq("id", 
"data")),
+    ("a projection of all the columns", _.select("*").select("data"), 
Seq("data"))
+  ).foreach { case (name, query, columns) =>
+    test(s"read only the columns that the optimized query reads: $name") {
+      withSourceTable() {
+        val expected = query(spark.read.table(tbl)).collect().toSeq
+        testStream(query(spark.readStream.table(tbl)))(
+          ProcessAllAvailable(),
+          assertReadSchema(columns: _*),
+          assertScanOutputs(columns),
+          CheckAnswer(expected: _*))
+      }
+    }
+  }
+
+  Seq[(String, DataFrame => DataFrame)](
+    ("from_json", _.select(from_json($"json", new StructType().add("a", 
"int")).as("p"))
+      .select("p.a")),
+    ("get_json_object", _.select(get_json_object($"json", "$.a").as("a"))),
+    ("parse_json", _.select(parse_json($"json").as("v"))
+      .select(variant_get($"v", "$.a", "int").as("a")))
+  ).foreach { case (name, query) =>
+    test(s"read only the parsed column of $name") {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (id bigint, json string, extra int) 
$pushdowns")
+        sql(s"""INSERT INTO $tbl VALUES (1, '{"a": 1}', 10), (2, '{"a": 2}', 
20)""")
+        val expected = query(spark.read.table(tbl)).collect().toSeq
+        testStream(query(spark.readStream.table(tbl)))(
+          ProcessAllAvailable(),
+          assertReadSchema("json"),
+          CheckAnswer(expected: _*))
+      }
+    }
+  }
+
+  test("read only the columns of a streaming aggregation, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).groupBy("data").count()
+      testStream(df, OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema("data"),
+        CheckAnswer(Row("a", 1L), Row("b", 1L)),
+        StopStream,
+        insert("(3, 'a', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("data"),
+        CheckAnswer(Row("a", 2L), Row("b", 1L)))
+    }
+  }
+
+  test("read only the event-time column of a time window") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(15))")
+      val df = spark.readStream.table(tbl).groupBy(window($"ts", "10 
seconds")).count()
+        .select($"window.start".cast("long"), $"count")
+      testStream(df, OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema("ts"),
+        CheckAnswer(Row(0L, 1L), Row(10L, 1L)))
+    }
+  }
+
+  test("read no column of the source when the query needs none, also after a 
restart") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).groupBy().count(), 
OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        assertScanOutputs(Nil),
+        CheckAnswer(Row(2L)),
+        StopStream,
+        insert("(3, 'c', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        CheckAnswer(Row(3L)))
+      testStream(spark.readStream.table(tbl).select(lit(1).as("one")))(
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        CheckAnswer(Row(1), Row(1), Row(1)))
+    }
+  }
+
+  test("read only the columns of a streaming deduplication, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).dropDuplicates("data").select("id")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row(1L), Row(2L)),
+        StopStream,
+        insert("(3, 'a', 30), (4, 'c', 40)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row(1L), Row(2L), Row(4L)))
+    }
+  }
+
+  test("read the metadata columns that a streaming query needs") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).select("data", "index")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "index"),
+        CheckAnswerRowsByFunc(rows => {
+          assert(rows.map(_.getString(0)).sorted === Seq("a", "b"))
+          assert(rows.forall(!_.isNullAt(1)))
+        }, lastOnly = false))
+    }
+  }
+
+  test("read only the columns of each child of a union") {
+    withSourceTable() {
+      // Each read of the table has its own relation and scan, and the 
optimizer prunes the
+      // columns of each child of the union by position.
+      val twoReads = 
spark.readStream.table(tbl).union(spark.readStream.table(tbl)).select("data")
+      testStream(twoReads)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("data"), Seq("data")),
+        CheckAnswer(Row("a"), Row("b"), Row("a"), Row("b")))
+
+      val differentColumns = spark.readStream.table(tbl).select("id")
+        .union(spark.readStream.table(tbl).select($"extra".cast("long")))
+      testStream(differentColumns)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id"), Seq("extra")),
+        CheckAnswer(Row(1L), Row(2L), Row(10L), Row(20L)))
+
+      // The children of a self-union read the same relation, whose scan reads 
the columns that
+      // both of them read.
+      val source = spark.readStream.table(tbl)
+      testStream(source.select("id", "data").union(source.select("id", 
"data")).select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relations = query.logicalPlan.collect {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }
+          assert(relations.length == 2 && 
relations.map(_.stream).distinct.length == 1)
+          assert(relations.head.scan.readSchema().fieldNames.toSeq === 
Seq("data"))
+          true
+        },
+        CheckAnswer(Row("a"), Row("b"), Row("a"), Row("b")))
+    }
+  }
+
+  test("read the columns that a scan returns when it reads more than the 
required ones") {
+    withSourceTable(
+        s"TBLPROPERTIES 
('${InMemoryBaseTable.SIMULATE_PARTIAL_COLUMN_PRUNING}' = 'true')") {
+      val df = spark.readStream.table(tbl).select("data", "index")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra", "index"),
+        CheckAnswerRowsByFunc(rows => {
+          assert(rows.map(_.getString(0)).sorted === Seq("a", "b"))
+          assert(rows.forall(!_.isNullAt(1)))
+        }, lastOnly = false))
+    }
+  }
+
+  test("read all the columns when a scan does not read the required ones") {
+    withSourceTable(
+        s"TBLPROPERTIES 
('${InMemoryBaseTable.SIMULATE_INCOMPLETE_COLUMN_PRUNING}' = 'true')") {
+      val table = spark.sessionState.catalogManager.catalog("testcat")
+        
.asInstanceOf[InMemoryTableCatalog].liveTable(Identifier.of(Array.empty, "t"))
+        .asInstanceOf[InMemoryBaseTable]
+      val logAppender = new LogAppender("the fallback of streaming column 
pruning")
+      withLogAppender(logAppender,
+          loggerNames = 
Seq(StreamingV2ScanPushDown.getClass.getName.stripSuffix("$")),
+          level = Some(Level.WARN)) {
+        testStream(spark.readStream.table(tbl).select("data"))(
+          ProcessAllAvailable(),
+          // The scan that does not read "data" is replaced by one of all the 
columns before its
+          // stream is created, so the source only has the stream of the scan 
of all the columns.
+          assertReadSchema("id", "data", "extra"),
+          AssertOnQuery(_ => table.createdMicroBatchStreams.get() == 1),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+      val warnings = 
logAppender.loggingEvents.map(_.getMessage.getFormattedMessage)
+      assert(warnings.exists { message =>
+        message.contains("could not be built with the pushdowns (columns: 
data, filters: none)") &&
+          message.contains("does not have the columns data") &&
+          message.contains("It reads all the columns with no filter instead")
+      }, warnings)
+    }
+  }
+
+  test("read the event-time column of a watermark that the query does not 
output") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(10))")
+      val df = spark.readStream.table(tbl).withWatermark("ts", "5 
seconds").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "ts"),
+        CheckAnswer(Row("a"), Row("b")),
+        // The watermark gets the event times of the rows.
+        AssertOnQuery { query =>
+          query.recentProgress.filter(_.numInputRows > 
0).map(_.eventTime.get("max")).toSeq ==
+            Seq("1970-01-01T00:00:10.000Z")
+        })
+    }
+  }
+
+  test("read only the columns of a stream-static join") {
+    withSourceTable() {
+      val static = spark.range(2, 4).toDF("sid")
+      val source = spark.readStream.table(tbl)
+      val df = source.join(static, source("id") === 
static("sid")).select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row("b")),
+        insert("(3, 'c', 30), (4, 'd', 40)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("c")))
+    }
+  }
+
+  test("read only the columns of each side of a stream-stream join, also after 
a restart") {
+    withSourceTable() {
+      val left = spark.readStream.table(tbl).select("id", "data")
+      val right = spark.readStream.table(tbl).selectExpr("id AS rid", "extra")
+      val df = left.join(right, left("id") === right("rid")).select("data", 
"extra")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id", "data"), Seq("id", "extra")),
+        CheckAnswer(Row("a", 10), Row("b", 20)),
+        StopStream,
+        insert("(3, 'c', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id", "data"), Seq("id", "extra")),
+        CheckAnswer(Row("a", 10), Row("b", 20), Row("c", 30)))
+    }
+  }
+
+  test("keep the layout of the state of a stream-stream join across the values 
of the conf " +
+      "when a scan returns the columns in another order") {
+    withPushDownConfsRestored {
+      val (leftTable, rightTable) = ("testcat.l", "testcat.r")
+      withTable(leftTable, rightTable) {
+        sql(s"CREATE TABLE $leftTable (k bigint, v bigint, unused int) 
TBLPROPERTIES " +
+          s"('${InMemoryBaseTable.SIMULATE_REORDERED_COLUMN_PRUNING}' = 
'true')")
+        sql(s"CREATE TABLE $rightTable (rk bigint, w int) $pushdowns")
+        // The left row is kept in the state of the join, which is written 
without pruning first.
+        sql(s"INSERT INTO $leftTable VALUES (1, 100, 0)")
+        val left = spark.readStream.table(leftTable)
+        val right = spark.readStream.table(rightTable)
+        val df = left.join(right, $"k" === $"rk").select("v", "w")
+        testStream(df)(
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          assertReadSchemas(Seq("k", "v", "unused"), Seq("rk", "w")),
+          CheckAnswer(),
+          StopStream,
+          // The pruned scan returns (v, k), and the join still gets (k, v).
+          insert("(1, 7)", rightTable),
+          StartStream(additionalConfs = pruningEnabled(true)),
+          ProcessAllAvailable(),
+          assertReadSchemas(Seq("v", "k"), Seq("rk", "w")),
+          CheckAnswer(Row(100L, 7)),
+          // And the other way around.
+          insert("(2, 200, 0)", leftTable),
+          ProcessAllAvailable(),
+          StopStream,
+          insert("(2, 8)", rightTable),
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(100L, 7), Row(200L, 8)))
+      }
+    }
+  }
+
+  test("keep the state of a streaming aggregation across the values of the 
conf when a scan " +
+      "returns the columns in another order") {
+    withPushDownConfsRestored {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (k bigint, v bigint, unused int) TBLPROPERTIES 
" +
+          s"('${InMemoryBaseTable.SIMULATE_REORDERED_COLUMN_PRUNING}' = 
'true')")
+        sql(s"INSERT INTO $tbl VALUES (1, 10, 0), (2, 20, 0)")
+        val df = spark.readStream.table(tbl).groupBy("k").agg(sum("v"))
+        testStream(df, OutputMode.Complete())(
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          assertReadSchema("k", "v", "unused"),
+          CheckAnswer(Row(1L, 10L), Row(2L, 20L)),
+          StopStream,
+          insert("(1, 1, 0)"),
+          StartStream(additionalConfs = pruningEnabled(true)),
+          ProcessAllAvailable(),
+          assertReadSchema("v", "k"),
+          CheckAnswer(Row(1L, 11L), Row(2L, 20L)),
+          StopStream,
+          insert("(2, 2, 0)"),
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(1L, 11L), Row(2L, 22L)))
+      }
+    }
+  }
+
+  test("fail to plan a micro-batch that reads a column that its scan does not 
read") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relation = query.logicalPlan.collectFirst {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }.get
+          assert(relation.output.map(_.name) === Seq("id", "data", "extra"))
+          assert(relation.prunedOutput.map(_.map(_.name)) === 
Some(Seq("data")))
+          assert(relation.simpleString(10).contains("PrunedOutput: [data#"))
+          val offset = relation.stream.initialOffset()
+          val batch = relation.copy(startOffset = Some(offset), endOffset = 
Some(offset))
+          // Unlike the query, this plan reads all the columns of the relation.
+          val e = intercept[SparkException] {
+            spark.sessionState.planner.plan(Project(relation.output, 
batch)).next()
+          }
+          assert(e.getCondition === "INTERNAL_ERROR")
+          assert(e.getMessage.contains("does not read the columns"))
+          
assert(e.getMessage.contains(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key))
+          true
+        })
+    }
+  }
+
+  test("read only the columns that a continuous query needs") {
+    val input = ContinuousMemoryStream.singlePartition[(Long, String, Int)]
+    val table = new PruningContinuousTable(input, takesFilters = false)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"pruning_continuous", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+    withTempView("continuous_pruning") {
+      val query = 
df.writeStream.format("memory").queryName("continuous_pruning")
+        .trigger(Trigger.Continuous(100)).start()
+      try {
+        input.addData((1L, "a", 10), (2L, "b", 20), (3L, "c", 30))
+        eventually(timeout(streamingTimeout)) {
+          checkAnswer(spark.table("continuous_pruning"), Seq(Row("b"), 
Row("c")))
+        }
+        val readSchemas = 
query.asInstanceOf[StreamingQueryWrapper].streamingQuery.logicalPlan
+          .collect { case r: StreamingDataSourceV2ScanRelation => 
r.scan.readSchema().fieldNames }
+        assert(readSchemas.map(_.toSeq) === Seq(Seq("_2", "_3")))
+      } finally {
+        query.stop()
+      }
+    }
+  }
+
+  test("push the filters of a streaming query down to the scan of its source") 
{
+    withPushDownConfsRestored {
+      withSourceTable() {
+        val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+        testStream(df)(
+          StartStream(additionalConfs = filterPushdownEnabled(true)),
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(extra,10)"),
+          // The scan of the in-memory table does not apply the filters, which 
Spark evaluates.
+          CheckAnswer(Row("b")),
+          StopStream,
+          insert("(3, 'c', 30), (4, 'd', 5)"),
+          StartStream(additionalConfs = filterPushdownEnabled(false)),
+          ProcessAllAvailable(),
+          assertPushedFilters(),
+          CheckAnswer(Row("b"), Row("c")),
+          StopStream,
+          insert("(5, 'e', 50), (6, 'f', 6)"),
+          StartStream(additionalConfs = filterPushdownEnabled(true)),
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(extra,10)"),
+          CheckAnswer(Row("b"), Row("c"), Row("e")))
+      }
+    }
+  }
+
+  test("push down only the filters that every read of a relation applies") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val df = source.where("extra > 10").union(source.where("extra > 10 AND 
id > 1"))
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row(2L, "b", 20), Row(2L, "b", 20)))
+    }
+  }
+
+  // Each case is a condition on a value that the optimizer of each 
micro-batch folds into its own
+  // value, such as its current time, and the column that it reads.
+  Seq(
+    "ts < current_timestamp()" -> "ts",
+    "ts < now()" -> "ts",
+    "CAST(ts AS DATE) < current_date()" -> "ts",
+    "CAST(ts AS TIMESTAMP_NTZ) < localtimestamp()" -> "ts",
+    "tod < current_time()" -> "tod",
+    "CAST(tod AS TIMESTAMP_NTZ) < TIMESTAMP_NTZ'2100-01-01 00:00:00'" -> "tod",
+    "CAST(tod AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'" -> "tod",
+    "data = current_database()" -> "data",
+    "data = current_timezone()" -> "data",
+    // The casts of the special strings, which SpecialDatetimeValues folds.
+    "d = 'today'" -> "d",
+    "ts < 'now'" -> "ts",
+    "d = to_date('today')" -> "d",
+    "d < CAST('tomorrow' AS DATE)" -> "d",
+    "CAST(ts AS TIMESTAMP_NTZ) > CAST('yesterday' AS TIMESTAMP_NTZ)" -> "ts",
+    // The casts of strings with only a time to timestamps, which take the 
current date.
+    "ts < '12:00:00'" -> "ts",
+    "ts > CAST('T09:30:00' AS TIMESTAMP)" -> "ts",
+    // Every cast of a string to a timestamp with local time zone is a 
placeholder when the query
+    // starts, also of a string with a date, or of a string column. 
TODO(SPARK-60926): push these
+    // down again when their values do not depend on the date.
+    "CAST(data AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'" -> "data",
+    "ts < '2100-01-01 12:00:00'" -> "ts"
+  ).foreach { case (condition, column) =>
+    test(s"do not push down a filter on a value that depends on when it is 
evaluated: $condition") {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp, tod 
time, d date) " +
+          pushdowns)
+        val df = spark.readStream.table(tbl).where(s"id > 1 AND 
$condition").select("id")
+        val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+        assert(reads.size === 1)
+        val read = reads.values.head
+        // The column of the condition is still read, but only the filter on 
the id is pushed down.
+        assert(read.requiredColumns.map(_.name).toSet === Set("id", column))
+        assert(read.filters.nonEmpty)
+        assert(read.filters.forall(_.references.map(_.name).toSet == 
Set("id")), read.filters)
+      }
+    }
+  }
+
+  test("do not push down a filter on the current time of a micro-batch") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(10))")
+      // Each micro-batch has its own current time, so the time when the query 
started cannot be
+      // pushed down in its place.
+      Seq("current_timestamp()", "now()").foreach { time =>
+        val df = spark.readStream.table(tbl).where(s"id > 0 AND ts < 
$time").select("data")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(id,0)"),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("read the columns and apply the filters of every reference to a common 
table expression") {
+    withSourceTable() {
+      withTempView("s") {
+        spark.readStream.table(tbl).createOrReplaceTempView("s")
+        // Both references to the common table expression have the relation of 
the source, so its
+        // scan reads the columns of both, and applies no filter, as only one 
of them filters.
+        val df = sql("WITH c AS (SELECT * FROM s) " +
+          "SELECT a.id, b.extra FROM c a JOIN c b ON a.id = b.id WHERE a.data 
= 'a'")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("id", "data", "extra"),
+          assertPushedFilters(),
+          CheckAnswer(Row(1L, 10)),
+          insert("(3, 'a', 30)"),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(1L, 10), Row(3L, 30)))
+
+        // Each reference applies its own filter.
+        val pruned = sql("WITH c AS (SELECT * FROM s) " +
+          "SELECT data FROM c WHERE extra > 15 UNION ALL SELECT data FROM c 
WHERE extra < 15")
+        testStream(pruned)(
+          ProcessAllAvailable(),
+          assertReadSchema("data", "extra"),
+          assertPushedFilters(),
+          CheckAnswer(Row("a"), Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("push down no filter that the optimizer infers from the filters of 
another source") {
+    val otherTable = "testcat.other"
+    withSourceTable() {
+      withTable(otherTable) {
+        sql(s"CREATE TABLE $otherTable (oid bigint, value string) $pushdowns")
+        val source = spark.readStream.table(tbl)
+        val other = spark.readStream.table(otherTable).where("oid < 3")
+        val df = source.join(other, source("id") === 
other("oid")).select("data", "value")
+        testStream(df)(
+          ProcessAllAvailable(),
+          // The optimizer infers a filter on the id of the source from the 
one on the other
+          // source, which is not pushed down: a scan only gets the filters 
that the query applies
+          // to its own source.
+          AssertOnQuery { query =>
+            assert(pushedFilters(query).sortBy(_.mkString) === Seq(Nil, 
Seq("LessThan(oid,3)")))
+            true
+          },
+          CheckAnswer(),
+          insert("(1, 'x'), (2, 'y'), (3, 'z')", otherTable),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("a", "x"), Row("b", "y")),
+          insert("(3, 'c', 30)"),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("a", "x"), Row("b", "y")))
+      }
+    }
+  }
+
+  test("plan the micro-batches whose filters imply the pushed filters in 
another form") {
+    val otherTable = "testcat.other"
+    withSourceTable() {
+      withTable(otherTable) {
+        sql(s"CREATE TABLE $otherTable (oid bigint, value string) $pushdowns")
+        sql(s"INSERT INTO $otherTable VALUES (1, 'x'), (2, 'y')")
+        // A micro-batch infers id = 2 from the equality of the static side, 
and puts its value in
+        // place of the column in the pushed filter id > 0, which it then 
drops: id = 2 implies it.
+        val source = spark.readStream.table(tbl)
+        val static = spark.range(1, 3).toDF("sid").where("sid = 2")
+        val joined = source.where("id > 0").join(static, source("id") === 
static("sid"))
+        testStream(joined.select("data"))(
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(id,0)"),
+          CheckAnswer(Row("b")))
+        // So does a stream-stream join, in the micro-batches where both 
sources have data.
+        val other = spark.readStream.table(otherTable).where("oid = 2")
+        val streams = source.where("id > 0").join(other, source("id") === 
other("oid"))
+        testStream(streams.select("data", "value"))(
+          ProcessAllAvailable(),
+          CheckAnswer(Row("b", "y")),
+          insert("(2, 'bb', 22)"),
+          insert("(2, 'yy')", otherTable),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("b", "y"), Row("b", "yy"), Row("bb", "y"), Row("bb", 
"yy")))
+      }
+    }
+  }
+
+  test("plan the micro-batches that fold a value of their own into a pushed 
filter") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, d date) $pushdowns")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), current_date()), (2, 
'other', NULL)")
+      // A micro-batch folds current_user() and now() into literals, which it 
then puts in place
+      // of the columns in the pushed filters owner <> '' and d >= 
DATE'2020-01-01'.
+      testStream(spark.readStream.table(tbl).where("owner <> '' AND owner = 
current_user()")
+          .select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Not(EqualTo(owner,))"),
+        CheckAnswer(Row(1L)))
+      testStream(spark.readStream.table(tbl)
+          .where("d >= DATE'2020-01-01' AND d = to_date(now())").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThanOrEqual(d,2020-01-01)"))
+    }
+  }
+
+  Seq(
+    "CAST('today' AS DATE) < DATE'2100-01-01'",
+    "CAST('12:00:00' AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'",
+    // The casts of strings to arrays, structs and maps of timestamps cast 
each string.
+    "element_at(CAST(array('12:00:00') AS ARRAY<TIMESTAMP>), 1) < 
TIMESTAMP'2100-01-01 00:00:00'",
+    "CAST(named_struct('t', '12:00:00') AS STRUCT<t: TIMESTAMP>).t < " +
+      "TIMESTAMP'2100-01-01 00:00:00'",
+    "element_at(CAST(map('k', '12:00:00') AS MAP<STRING, TIMESTAMP>), 'k') < " 
+
+      "TIMESTAMP'2100-01-01 00:00:00'"
+  ).foreach { condition =>
+    test(s"read the columns of a condition on a string that depends on when it 
is cast: " +
+        condition) {
+      withSourceTable() {
+        // The condition takes the date of each micro-batch, so the scan reads 
both columns.
+        val df = spark.readStream.table(tbl).select(expr(s"CASE WHEN 
$condition THEN data " +
+          "ELSE CAST(extra AS STRING) END AS v"))
+        val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+        assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+          Seq(Set("data", "extra")))
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("data", "extra"),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("read the columns of a condition on a cast whose string is a literal 
only once optimized") {
+    withSourceTable() {
+      // The cast of `cutoff` is only foldable once the optimizer replaces the 
alias with the
+      // string, and then takes the date of the start of the query, so the 
condition would be
+      // folded with it if the cast stayed a cast.
+      val df = spark.readStream.table(tbl).withColumn("cutoff", 
lit("12:00:00"))
+        .select(expr("CASE WHEN CAST(cutoff AS TIMESTAMP) < 
TIMESTAMP'2100-01-01 00:00:00' " +
+          "THEN data ELSE CAST(extra AS STRING) END AS v"))
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+        Seq(Set("data", "extra")))
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        CheckAnswer(Row("a"), Row("b")))
+    }
+  }
+
+  test("push down no filter after a filter that is not pushed down") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, qty int, payload 
string) $pushdowns")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), 1, '1'), (2, 'other', 
0, 'abc')")
+      val raise = "CASE WHEN qty <= 0 THEN raise_error('bad qty') ELSE true 
END"
+      def pushed(df: DataFrame): Seq[Set[String]] = {
+        StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, 
None).values.toSeq
+          .flatMap(_.filters).map(_.references.map(_.name).toSet)
+      }
+      val source = spark.readStream.table(tbl)
+      // The filter on the owner has a value of each micro-batch, so it is not 
pushed down, and
+      // neither is any filter written after it: each micro-batch evaluates 
the pushed filters
+      // first, so a filter that fails on the row of the other owner, as the 
cast of its payload
+      // does with ANSI, or the error raised for its quantity 0, would fail 
the query.
+      Seq(raise -> Nil, "CAST(payload AS INT) > 0" -> Nil, "id > 0" -> 
Seq("GreaterThan(id,0)"))
+        .foreach { case (filter, taken) =>
+          val df = source.where(s"owner = current_user() AND $filter")
+          assert(pushed(df) === Nil, filter)
+          // The filters of the V1 builder of the in-memory tables compare 
columns with values,
+          // which cannot fail, so it gets id > 0 wherever it is. It cannot 
take the others.
+          testStream(df.select("id"))(
+            ProcessAllAvailable(),
+            assertPushedFilters(taken: _*),
+            CheckAnswer(Row(1L)))
+        }
+      // The filters written before it are pushed down, as the query evaluates 
them first anyway.
+      assert(pushed(source.where(s"$raise AND id > 0 AND owner = 
current_user() AND qty > 0")) ===
+        Seq(Set("qty"), Set("id")))
+      // With several reads of the relation, the pushed filters are those that 
all of them
+      // evaluate first, in the same order.
+      val read = source.where(s"$raise AND id > 0")
+      assert(pushed(read.union(read)) === Seq(Set("qty"), Set("id")))
+      assert(pushed(read.union(source.where(s"$raise AND qty > 0"))) === 
Seq(Set("qty")))
+      assert(pushed(read.union(source.where(s"id > 0 AND $raise"))) === Nil)
+    }
+  }
+
+  test("drop the rows that fail the pushed filters in the stream of a source") 
{
+    withSourceTable(filtering) {
+      // The stream only returns the rows that pass the pushed filter, so the 
progress of the query
+      // only counts them.
+      def inputRows(query: StreamExecution): Long = 
query.recentProgress.map(_.numInputRows).sum
+      testStream(spark.readStream.table(tbl).where("extra > 
10").select("data"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row("b")),
+        AssertOnQuery(inputRows(_) == 1),
+        insert("(3, 'c', 30), (4, 'd', 5), (5, 'e', 50)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("c"), Row("e")),
+        AssertOnQuery(inputRows(_) == 3))
+    }
+  }
+
+  test("filter the rows of a continuous stream with the filters that its scan 
builder took") {
+    val input = ContinuousMemoryStream.singlePartition[(Long, String, Int)]
+    val table = new PruningContinuousTable(input, takesFilters = true)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"pruning_continuous", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+    PruningContinuousTable.returnedRows.set(0)
+    withTempView("continuous_filtering") {
+      val query = 
df.writeStream.format("memory").queryName("continuous_filtering")
+        .trigger(Trigger.Continuous(100)).start()
+      try {
+        input.addData((1L, "a", 10), (2L, "b", 20), (3L, "c", 30))
+        eventually(timeout(streamingTimeout)) {
+          checkAnswer(spark.table("continuous_filtering"), Seq(Row("b"), 
Row("c")))
+        }
+        // The readers dropped the row that fails the filter, which the plan 
of the query applies
+        // to the rows of the source too.
+        assert(PruningContinuousTable.returnedRows.get() === 2)
+        val filtered = 
query.asInstanceOf[StreamingQueryWrapper].streamingQuery.logicalPlan
+          .collect {
+            case Filter(_, relation: StreamingDataSourceV2ScanRelation) =>
+              relation.pushedFilters.map(_.references.map(_.name).toSet)
+          }
+        assert(filtered === Seq(Seq(Set("_3"))))
+      } finally {
+        query.stop()
+      }
+    }
+  }
+
+  test("plan the micro-batches whose filters fold the pushed filters in part") 
{
+    // The streams drop the rows that fail the pushed filters, so the answers 
depend on them.
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, d date) $filtering")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), current_date()), " +
+        "(6, current_user(), current_date()), (7, 'other', current_date())")
+      // A micro-batch folds current_user() into a literal, which it puts in 
place of the column
+      // in the pushed filter owner = '' OR id > 5, which it then folds into 
id > 5.
+      testStream(spark.readStream.table(tbl)
+          .where("(owner = '' OR id > 5) AND owner = 
current_user()").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(EqualTo(owner,),GreaterThan(id,5))"),
+        CheckAnswer(Row(6L)))
+      // So does a date that is not the date of the micro-batch, as on the day 
after the start.
+      // The micro-batch is planned whether the rows have its date or not.
+      testStream(spark.readStream.table(tbl)
+          .where("(d < DATE'2000-01-01' OR id > 5) AND d = 
to_date(now())").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(LessThan(d,2000-01-01),GreaterThan(id,5))"))
+    }
+    // And an equality that a micro-batch infers through a stream-static join.
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (a bigint, b bigint) $filtering")
+      sql(s"INSERT INTO $tbl VALUES (2, 0), (0, 0), (5, 3)")
+      val source = spark.readStream.table(tbl).where("a > 1 OR b > 2")
+      val static = spark.range(0, 2).toDF("x").where("x = 0")
+      testStream(source.join(static, source("b") === static("x")).select("a"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(GreaterThan(a,1),GreaterThan(b,2))"),
+        CheckAnswer(Row(2L)))
+    }
+  }
+
+  test("read all the columns with no filter from a table without the 
capabilities") {
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = '')") {
+      val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra"),
+        assertScanOutputs(Seq("id", "data", "extra")),
+        assertPushedFilters(),
+        AssertOnQuery { query =>
+          // The plan of the query only applies the filter of the query.
+          val relations = query.logicalPlan.collect {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }
+          relations.nonEmpty && relations.forall(_.pushedFilters.isEmpty)
+        },
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  test("push down only what the capabilities of the table allow") {
+    val df = () => spark.readStream.table(tbl).where("extra > 
10").select("data")
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 'columns')") {
+      testStream(df())(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        assertPushedFilters(),
+        CheckAnswer(Row("b")))
+    }
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 'filters')") {
+      testStream(df())(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra"),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  Seq("pushFilters", "pruneColumns", "build", "toMicroBatchStream").foreach { 
failingStep =>
+    test(s"read all the columns with no filter when the scan fails with the 
pushdowns: " +
+        failingStep) {
+      val input = MemoryStream[(Long, String, Int)]
+      val table = new FailingPushDownTable(input, failingStep)
+      val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"failing_push_down", table,
+        CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+        None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+      val logs = new LogAppender("the fallback of a streaming scan")
+      withLogAppender(logs,
+          loggerNames = 
Seq(StreamingV2ScanPushDown.getClass.getName.stripSuffix("$")),
+          level = Some(Level.WARN)) {
+        testStream(df)(
+          StartStream(),
+          AddData(input, (1L, "a", 10), (2L, "b", 20)),
+          ProcessAllAvailable(),
+          // A new scan builder builds a scan of all the columns, with no 
filter, as without the
+          // pushdowns.
+          assertReadSchema("_1", "_2", "_3"),
+          AssertOnQuery(_ => table.streams.asScala.toSeq == Seq(Seq("_1", 
"_2", "_3") -> Nil)),
+          CheckAnswer(Row("b")))
+      }
+      val warnings = logs.loggingEvents.map(_.getMessage.getFormattedMessage)
+      assert(warnings.exists { message =>
+        message.contains("could not be built with the pushdowns (columns: _2, 
_3, filters: ") &&
+          message.contains(s"$failingStep failed with the pushdowns")
+      }, warnings)
+    }
+  }
+
+  test("keep the placeholders of the values that depend on when they are 
evaluated apart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl)
+        .select(current_user(), current_database(), current_catalog(), 
col("data"))
+      val placeholders = 
StreamingV2ScanPushDown.forStartup(df.queryExecution.analyzed)
+        .collectFirst { case project: Project => project.projectList.take(3) 
}.get
+        .map(_.asInstanceOf[Alias].child)
+      // The placeholders of different expressions differ, although they have 
the same type.
+      assert(placeholders.map(_.dataType).distinct.length === 1)
+      assert(placeholders.map(_.canonicalized).distinct.length === 3)
+    }
+  }
+
+  test("replace the casts of strings to timestamps with a local time zone") {
+    withSourceTable() {
+      // Each cast, and whether it takes the current date for a string with 
only a time: those to
+      // timestamps with a local time zone, of any precision, at any depth of 
their types and
+      // whatever their child is, unlike a cast to a timestamp without time 
zone or to a date,
+      // which rejects such a string.
+      val casts = Seq(
+        "CAST('12:00:00' AS TIMESTAMP)" -> true,
+        "CAST('12:00:00' AS TIMESTAMP_LTZ(9))" -> true,
+        "CAST('2100-01-01 12:00:00' AS TIMESTAMP)" -> true,
+        "CAST(data AS TIMESTAMP)" -> true,
+        "CAST(array('12:00:00') AS ARRAY<TIMESTAMP>)" -> true,
+        "CAST(named_struct('t', '12:00:00') AS STRUCT<t: TIMESTAMP>)" -> true,
+        "CAST(map('k', '12:00:00') AS MAP<STRING, TIMESTAMP>)" -> true,
+        "CAST('12:00:00' AS TIMESTAMP_NTZ)" -> false,
+        "CAST('2100-01-01' AS DATE)" -> false,
+        "CAST(array(1) AS ARRAY<STRING>)" -> false)
+      val df = spark.readStream.table(tbl).select(casts.map(c => expr(c._1)) 
:+ col("data"): _*)
+      val replaced = 
StreamingV2ScanPushDown.forStartup(df.queryExecution.analyzed)
+        .collectFirst { case project: Project => 
project.projectList.take(casts.length) }.get
+        .map(_.asInstanceOf[Alias].child)
+      assert(replaced.map(!_.isInstanceOf[Cast]) === casts.map(_._2))
+    }
+  }
+
+  test("apply the filters that the scan builder got in the plan of each 
micro-batch") {
+    withSourceTable() {
+      // id + extra > 10 cannot be translated to a filter of the source, so 
the scan builder only
+      // gets extra > 15. Each micro-batch applies it to the rows of the 
source, which the query
+      // applies anyway, so the scan cannot drop a row that the plan keeps, 
whatever it does with
+      // the filter. The filter that the scan builder does not get is 
evaluated after it, as for
+      // a batch scan, rather than on every row of the source.
+      testStream(spark.readStream.table(tbl).where("id + extra > 10 AND extra 
> 15")
+          .select("data"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,15)"),
+        AssertOnQuery { query =>
+          val filtered = query.logicalPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition -> relation
+          }
+          assert(filtered.length === 1)
+          val (condition, relation) = filtered.head
+          assert(relation.pushedFilters.map(_.references.map(_.name).toSet) 
=== Seq(Set("extra")))
+          assert(condition === relation.pushedFilters.head)
+          true
+        },
+        CheckAnswer(Row("b")),
+        insert("(3, 'c', 5), (4, 'd', 30)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("d")))
+    }
+  }
+
+  test("push down the filters of a query that joins a source with an inline 
table") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val inline = sql("SELECT * FROM VALUES (2, current_date()) AS s(k, d)")
+      val df = source.where("extra > 10").join(inline, source("id") === 
inline("k"))
+        .select("data", "d")
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+        Seq(Set("id", "data", "extra")))
+      
assert(reads.values.flatMap(_.filters).map(_.references.map(_.name).toSet).toSeq
 ===
+        Seq(Set("extra")))
+    }
+  }
+
+  test("filter the rows of each read of a source with the filters that its 
scan builder got") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val df = source.where("extra > 10").select("data")
+        .union(source.where("extra > 10 AND id > 1").select("data"))
+      testStream(df)(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          // Both reads of the relation are filtered with the filter that they 
both apply.
+          val filtered = query.logicalPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition -> relation
+          }
+          assert(filtered.length === 2)
+          filtered.foreach { case (condition, relation) =>
+            assert(relation.pushedFilters.map(_.toString) ===
+              Seq(s"(${relation.output(2)} > 10)"))
+            assert(condition === relation.pushedFilters.head)
+          }
+          // So is each read of the relation in the optimized plan of a 
micro-batch.
+          val microBatchFilters = query.lastExecution.optimizedPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition.exists(c => 
relation.pushedFilters.exists(c.semanticEquals))
+          }
+          assert(microBatchFilters === Seq(true, true))
+          true
+        },
+        CheckAnswer(Row("b"), Row("b")))
+    }
+  }
+
+  test("do not build the scans of the batch relations of a streaming query 
when it starts") {
+    withSourceTable() {
+      val table = new CountingBatchTable
+      val static = Dataset.ofRows(spark, DataSourceV2Relation.create(
+        table, None, None, CaseInsensitiveStringMap.empty()))
+      val source = spark.readStream.table(tbl)
+      val df = source.join(static, source("id") === 
static("sid")).select("data")
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq === 
Seq(Set("id", "data")))
+      assert(table.scanBuilders.get() === 0)
+    }
+  }
+
+  test("read all the columns of a source whose pruned scan has no read 
schema") {
+    val input = MemoryStream[(Long, String, Int)]
+    val table = new NoPrunedReadSchemaTable(input)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"no_pruned_read_schema", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").select("data")
+    testStream(df)(
+      StartStream(),
+      AddData(input, (1L, "a", 10), (2L, "b", 20)),
+      ProcessAllAvailable(),
+      // The pruned scan is replaced by a scan of all the columns before its 
stream is created.
+      assertReadSchema("_1", "_2", "_3"),
+      AssertOnQuery(_ => table.streams.get() == 1),
+      CheckAnswer(Row("a"), Row("b")))
+  }
+
+  test("read all the columns when the pruning of streaming scans is disabled") 
{
+    withSQLConf(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> "false") {
+      withSourceTable() {
+        val df = spark.readStream.table(tbl).select("data")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("id", "data", "extra"),
+          assertScanOutputs(Seq("id", "data", "extra")),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("canonicalize and show the pushed filters of a streaming scan 
relation") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).where("extra > 
10").select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relation = query.logicalPlan.collectFirst {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }.get
+          assert(relation.pushedFilters.nonEmpty)
+          // A copy of the relation has new attributes, also in its pushed 
filters, and the same
+          // result.
+          val copy = 
relation.newInstance().asInstanceOf[StreamingDataSourceV2ScanRelation]
+          assert(copy.pushedFilters.flatMap(_.references).map(_.exprId).toSet
+            .intersect(relation.output.map(_.exprId).toSet).isEmpty)
+          assert(copy.sameResult(relation))
+          // The plan applies the pushed filters with those of the query, so 
the relation shows
+          // the filters that its scan builder was given.
+          val string = relation.simpleString(100)
+          assert(string.contains("FiltersOfScanBuilder: [") && 
string.contains(" > 10)]"), string)
+          true
+        },
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  test("push down no filter above a projection of a value of each micro-batch 
without " +
+      "constraint propagation") {
+    withSourceTable() {
+      def pushed(df: DataFrame): Seq[Set[String]] = {
+        StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, 
None).values.toSeq
+          .flatMap(_.filters).map(_.references.map(_.name).toSet)
+      }
+      val source = spark.readStream.table(tbl)
+      // The query outputs the value, so that the optimizer keeps its 
projection.
+      val df = source.withColumn("pt", current_timestamp()).where("extra > 10")
+        .select("data", "pt")
+      assert(pushed(df) === Seq(Set("extra")))
+      // The propagation of the constraints prunes the copy of the filter of 
the query that each
+      // micro-batch keeps above the projection, so it evaluates the filter 
once.
+      testStream(df)(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          def conjuncts(e: Expression): Seq[Expression] = e match {

Review Comment:
   Done, thank you. The local helper is gone. The new suite-level 
`assertEvaluatedOnce` uses `splitConjunctivePredicates` from `PredicateHelper`.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingV2ScanPushDownSuite.scala:
##########
@@ -0,0 +1,1540 @@
+/*
+ * 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.streaming
+
+import java.util
+import java.util.concurrent.ConcurrentLinkedQueue
+import java.util.concurrent.atomic.{AtomicInteger, AtomicLong}
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.logging.log4j.Level
+import org.scalatest.BeforeAndAfter
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.{DataFrame, Row}
+import org.apache.spark.sql.catalyst.{InternalRow, ProjectingInternalRow}
+import org.apache.spark.sql.catalyst.expressions.{Alias, And, AttributeSet, 
Cast, EvalMode, Expression, GreaterThan, IsNotNull, Literal, RLike}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, Project}
+import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.classic.Dataset
+import org.apache.spark.sql.connector.catalog.{Identifier, InMemoryBaseTable, 
InMemoryTableCatalog, SupportsRead, Table, TableCapability}
+import org.apache.spark.sql.connector.expressions.filter.Predicate
+import org.apache.spark.sql.connector.read.{InputPartition, Scan, ScanBuilder, 
SupportsPushDownFilters, SupportsPushDownRequiredColumns, 
SupportsPushDownV2Filters}
+import 
org.apache.spark.sql.connector.read.streaming.{ContinuousPartitionReader, 
ContinuousPartitionReaderFactory, ContinuousStream, MicroBatchStream, Offset, 
PartitionOffset}
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, 
DataSourceV2Strategy, MicroBatchScanExec, StreamingDataSourceV2ScanRelation}
+import org.apache.spark.sql.execution.streaming.runtime.{MemoryStream, 
StreamExecution, StreamingQueryWrapper, StreamingV2ScanPushDown}
+import org.apache.spark.sql.execution.streaming.sources.ContinuousMemoryStream
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.sources.{Filter => SourceFilter}
+import org.apache.spark.sql.types.{IntegerType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+import org.apache.spark.unsafe.types.UTF8String
+
+class StreamingV2ScanPushDownSuite extends StreamTest with BeforeAndAfter {
+  import testImplicits._
+
+  private val tbl = "testcat.t"
+
+  before {
+    spark.conf.set("spark.sql.catalog.testcat", 
classOf[InMemoryTableCatalog].getName)
+  }
+
+  after {
+    spark.sessionState.catalogManager.reset()
+    spark.conf.unset("spark.sql.catalog.testcat")
+  }
+
+  // The in-memory tables only declare the column pruning by default: the 
tables of this suite
+  // declare the filter pushdown too.
+  private val bothPushdowns = s"'${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 
'columns,filters'"
+  private val pushdowns = s"TBLPROPERTIES ($bothPushdowns)"
+  // Both pushdowns, with streams that drop the rows that fail the pushed 
filters.
+  private val filtering =
+    s"TBLPROPERTIES ($bothPushdowns, 
'${InMemoryBaseTable.SIMULATE_STREAMING_FILTERS}' = 'true')"
+
+  private def withSourceTable(properties: String = pushdowns)(
+      f: => Unit): Unit = {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, extra int) $properties")
+      sql(s"INSERT INTO $tbl VALUES (1, 'a', 10), (2, 'b', 20)")
+      f
+    }
+  }
+
+  private def insert(values: String, table: String = tbl): StreamAction = 
Execute { _ =>
+    sql(s"INSERT INTO $table VALUES $values")
+  }
+
+  private def pruningEnabled(enabled: Boolean): Map[String, String] =
+    Map(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> enabled.toString)
+
+  /**
+   * Runs `f`, which restarts streams with other values of the confs of these 
pushdowns, and then
+   * restores their values: `testStream` restores the value that a conf had 
before the last
+   * `StartStream` that set it, rather than before the first.
+   */
+  private def withPushDownConfsRestored(f: => Unit): Unit = withSQLConf(
+    SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> "true",
+    SQLConf.STREAMING_V2_FILTER_PUSHDOWN_ENABLED.key -> "true")(f)
+
+  private def filterPushdownEnabled(enabled: Boolean): Map[String, String] =
+    Map(SQLConf.STREAMING_V2_FILTER_PUSHDOWN_ENABLED.key -> enabled.toString)
+
+  /** Returns the filters pushed down to the scan of each streaming source of 
the query. */
+  private def pushedFilters(query: StreamExecution): Seq[Seq[String]] = {
+    query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan match {
+          case scan: InMemoryBaseTable#InMemoryBatchScan =>
+            scan.pushedFilters.map(_.toString).toSeq.sorted
+          case scan =>
+            fail(s"The scan $scan of ${relation.name} is not a scan of an 
in-memory table.")
+        }
+    }
+  }
+
+  /** Checks that the filters pushed down to the scan of each streaming source 
are `filters`. */
+  private def assertPushedFilters(filters: String*): StreamAction = 
AssertOnQuery { query =>
+    val pushed = pushedFilters(query)
+    assert(pushed.nonEmpty)
+    pushed.foreach(p => assert(p === filters.sorted))
+    true
+  }
+
+  /** Checks the read schemas of the scans of the streaming sources of the 
query. */
+  private def assertReadSchemas(columns: Seq[String]*): StreamAction = 
AssertOnQuery { query =>
+    val readSchemas = query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan.readSchema().fieldNames.toSeq
+    }
+    assert(readSchemas.sortBy(_.mkString(",")) === 
columns.sortBy(_.mkString(",")))
+    true
+  }
+
+  /** Checks that the scan of each streaming source of the query reads 
`columns`. */
+  private def assertReadSchema(columns: String*): StreamAction = AssertOnQuery 
{ query =>
+    val readSchemas = query.logicalPlan.collect {
+      case relation: StreamingDataSourceV2ScanRelation =>
+        relation.scan.readSchema().fieldNames.toSeq
+    }
+    assert(readSchemas.nonEmpty)
+    readSchemas.foreach(readSchema => assert(readSchema === columns))
+    true
+  }
+
+  /** Checks the output of the scan nodes of the last micro-batch. */
+  private def assertScanOutputs(columns: Seq[String]*): StreamAction = 
AssertOnQuery { query =>
+    val outputs = query.lastExecution.executedPlan.collect {
+      case scan: MicroBatchScanExec => scan.output.map(_.name)
+    }
+    assert(outputs.sortBy(_.mkString(",")) === columns.sortBy(_.mkString(",")))
+    true
+  }
+
+  test("read only the columns that a streaming query needs, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        assertScanOutputs(Seq("data", "extra")),
+        CheckAnswer(Row("b")),
+        StopStream,
+        insert("(3, 'c', 30), (4, 'd', 5)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        CheckAnswer(Row("b"), Row("c")))
+    }
+  }
+
+  // Each case is a query over the source, and the columns that the optimizer 
reads for it, which
+  // fewer than those that the operators of its analyzed plan reference.
+  Seq[(String, DataFrame => DataFrame, Seq[String])](
+    ("a column that a later projection drops",
+      _.withColumn("x", lit(1)).select("data"), Seq("data")),
+    ("chained projections", _.select("id", "data").select("data"), 
Seq("data")),
+    ("expressions and functions that a later projection drops",
+      _.select(upper($"data").as("u"), ($"id" * 2).as("i")).select("u"), 
Seq("data")),
+    ("several aliases of a column",
+      _.select($"id".as("x"), $"data".as("y")).select($"x".as("z")).where("z > 
1"), Seq("id")),
+    ("a field of a struct of columns",
+      _.select(struct($"id", $"data").as("s")).select("s.data"), Seq("data")),
+    ("the fields of a struct of columns",
+      _.select(struct($"id", $"extra").as("s")).select("s.*"), Seq("id", 
"extra")),
+    ("a filter between projections",
+      _.select("id", "data").where("id > 1").select("data"), Seq("id", 
"data")),
+    ("a projection of all the columns", _.select("*").select("data"), 
Seq("data"))
+  ).foreach { case (name, query, columns) =>
+    test(s"read only the columns that the optimized query reads: $name") {
+      withSourceTable() {
+        val expected = query(spark.read.table(tbl)).collect().toSeq
+        testStream(query(spark.readStream.table(tbl)))(
+          ProcessAllAvailable(),
+          assertReadSchema(columns: _*),
+          assertScanOutputs(columns),
+          CheckAnswer(expected: _*))
+      }
+    }
+  }
+
+  Seq[(String, DataFrame => DataFrame)](
+    ("from_json", _.select(from_json($"json", new StructType().add("a", 
"int")).as("p"))
+      .select("p.a")),
+    ("get_json_object", _.select(get_json_object($"json", "$.a").as("a"))),
+    ("parse_json", _.select(parse_json($"json").as("v"))
+      .select(variant_get($"v", "$.a", "int").as("a")))
+  ).foreach { case (name, query) =>
+    test(s"read only the parsed column of $name") {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (id bigint, json string, extra int) 
$pushdowns")
+        sql(s"""INSERT INTO $tbl VALUES (1, '{"a": 1}', 10), (2, '{"a": 2}', 
20)""")
+        val expected = query(spark.read.table(tbl)).collect().toSeq
+        testStream(query(spark.readStream.table(tbl)))(
+          ProcessAllAvailable(),
+          assertReadSchema("json"),
+          CheckAnswer(expected: _*))
+      }
+    }
+  }
+
+  test("read only the columns of a streaming aggregation, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).groupBy("data").count()
+      testStream(df, OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema("data"),
+        CheckAnswer(Row("a", 1L), Row("b", 1L)),
+        StopStream,
+        insert("(3, 'a', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("data"),
+        CheckAnswer(Row("a", 2L), Row("b", 1L)))
+    }
+  }
+
+  test("read only the event-time column of a time window") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(15))")
+      val df = spark.readStream.table(tbl).groupBy(window($"ts", "10 
seconds")).count()
+        .select($"window.start".cast("long"), $"count")
+      testStream(df, OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema("ts"),
+        CheckAnswer(Row(0L, 1L), Row(10L, 1L)))
+    }
+  }
+
+  test("read no column of the source when the query needs none, also after a 
restart") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).groupBy().count(), 
OutputMode.Complete())(
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        assertScanOutputs(Nil),
+        CheckAnswer(Row(2L)),
+        StopStream,
+        insert("(3, 'c', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        CheckAnswer(Row(3L)))
+      testStream(spark.readStream.table(tbl).select(lit(1).as("one")))(
+        ProcessAllAvailable(),
+        assertReadSchema(),
+        CheckAnswer(Row(1), Row(1), Row(1)))
+    }
+  }
+
+  test("read only the columns of a streaming deduplication, also after a 
restart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).dropDuplicates("data").select("id")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row(1L), Row(2L)),
+        StopStream,
+        insert("(3, 'a', 30), (4, 'c', 40)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row(1L), Row(2L), Row(4L)))
+    }
+  }
+
+  test("read the metadata columns that a streaming query needs") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl).select("data", "index")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "index"),
+        CheckAnswerRowsByFunc(rows => {
+          assert(rows.map(_.getString(0)).sorted === Seq("a", "b"))
+          assert(rows.forall(!_.isNullAt(1)))
+        }, lastOnly = false))
+    }
+  }
+
+  test("read only the columns of each child of a union") {
+    withSourceTable() {
+      // Each read of the table has its own relation and scan, and the 
optimizer prunes the
+      // columns of each child of the union by position.
+      val twoReads = 
spark.readStream.table(tbl).union(spark.readStream.table(tbl)).select("data")
+      testStream(twoReads)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("data"), Seq("data")),
+        CheckAnswer(Row("a"), Row("b"), Row("a"), Row("b")))
+
+      val differentColumns = spark.readStream.table(tbl).select("id")
+        .union(spark.readStream.table(tbl).select($"extra".cast("long")))
+      testStream(differentColumns)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id"), Seq("extra")),
+        CheckAnswer(Row(1L), Row(2L), Row(10L), Row(20L)))
+
+      // The children of a self-union read the same relation, whose scan reads 
the columns that
+      // both of them read.
+      val source = spark.readStream.table(tbl)
+      testStream(source.select("id", "data").union(source.select("id", 
"data")).select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relations = query.logicalPlan.collect {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }
+          assert(relations.length == 2 && 
relations.map(_.stream).distinct.length == 1)
+          assert(relations.head.scan.readSchema().fieldNames.toSeq === 
Seq("data"))
+          true
+        },
+        CheckAnswer(Row("a"), Row("b"), Row("a"), Row("b")))
+    }
+  }
+
+  test("read the columns that a scan returns when it reads more than the 
required ones") {
+    withSourceTable(
+        s"TBLPROPERTIES 
('${InMemoryBaseTable.SIMULATE_PARTIAL_COLUMN_PRUNING}' = 'true')") {
+      val df = spark.readStream.table(tbl).select("data", "index")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra", "index"),
+        CheckAnswerRowsByFunc(rows => {
+          assert(rows.map(_.getString(0)).sorted === Seq("a", "b"))
+          assert(rows.forall(!_.isNullAt(1)))
+        }, lastOnly = false))
+    }
+  }
+
+  test("read all the columns when a scan does not read the required ones") {
+    withSourceTable(
+        s"TBLPROPERTIES 
('${InMemoryBaseTable.SIMULATE_INCOMPLETE_COLUMN_PRUNING}' = 'true')") {
+      val table = spark.sessionState.catalogManager.catalog("testcat")
+        
.asInstanceOf[InMemoryTableCatalog].liveTable(Identifier.of(Array.empty, "t"))
+        .asInstanceOf[InMemoryBaseTable]
+      val logAppender = new LogAppender("the fallback of streaming column 
pruning")
+      withLogAppender(logAppender,
+          loggerNames = 
Seq(StreamingV2ScanPushDown.getClass.getName.stripSuffix("$")),
+          level = Some(Level.WARN)) {
+        testStream(spark.readStream.table(tbl).select("data"))(
+          ProcessAllAvailable(),
+          // The scan that does not read "data" is replaced by one of all the 
columns before its
+          // stream is created, so the source only has the stream of the scan 
of all the columns.
+          assertReadSchema("id", "data", "extra"),
+          AssertOnQuery(_ => table.createdMicroBatchStreams.get() == 1),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+      val warnings = 
logAppender.loggingEvents.map(_.getMessage.getFormattedMessage)
+      assert(warnings.exists { message =>
+        message.contains("could not be built with the pushdowns (columns: 
data, filters: none)") &&
+          message.contains("does not have the columns data") &&
+          message.contains("It reads all the columns with no filter instead")
+      }, warnings)
+    }
+  }
+
+  test("read the event-time column of a watermark that the query does not 
output") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(10))")
+      val df = spark.readStream.table(tbl).withWatermark("ts", "5 
seconds").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "ts"),
+        CheckAnswer(Row("a"), Row("b")),
+        // The watermark gets the event times of the rows.
+        AssertOnQuery { query =>
+          query.recentProgress.filter(_.numInputRows > 
0).map(_.eventTime.get("max")).toSeq ==
+            Seq("1970-01-01T00:00:10.000Z")
+        })
+    }
+  }
+
+  test("read only the columns of a stream-static join") {
+    withSourceTable() {
+      val static = spark.range(2, 4).toDF("sid")
+      val source = spark.readStream.table(tbl)
+      val df = source.join(static, source("id") === 
static("sid")).select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data"),
+        CheckAnswer(Row("b")),
+        insert("(3, 'c', 30), (4, 'd', 40)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("c")))
+    }
+  }
+
+  test("read only the columns of each side of a stream-stream join, also after 
a restart") {
+    withSourceTable() {
+      val left = spark.readStream.table(tbl).select("id", "data")
+      val right = spark.readStream.table(tbl).selectExpr("id AS rid", "extra")
+      val df = left.join(right, left("id") === right("rid")).select("data", 
"extra")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id", "data"), Seq("id", "extra")),
+        CheckAnswer(Row("a", 10), Row("b", 20)),
+        StopStream,
+        insert("(3, 'c', 30)"),
+        StartStream(),
+        ProcessAllAvailable(),
+        assertReadSchemas(Seq("id", "data"), Seq("id", "extra")),
+        CheckAnswer(Row("a", 10), Row("b", 20), Row("c", 30)))
+    }
+  }
+
+  test("keep the layout of the state of a stream-stream join across the values 
of the conf " +
+      "when a scan returns the columns in another order") {
+    withPushDownConfsRestored {
+      val (leftTable, rightTable) = ("testcat.l", "testcat.r")
+      withTable(leftTable, rightTable) {
+        sql(s"CREATE TABLE $leftTable (k bigint, v bigint, unused int) 
TBLPROPERTIES " +
+          s"('${InMemoryBaseTable.SIMULATE_REORDERED_COLUMN_PRUNING}' = 
'true')")
+        sql(s"CREATE TABLE $rightTable (rk bigint, w int) $pushdowns")
+        // The left row is kept in the state of the join, which is written 
without pruning first.
+        sql(s"INSERT INTO $leftTable VALUES (1, 100, 0)")
+        val left = spark.readStream.table(leftTable)
+        val right = spark.readStream.table(rightTable)
+        val df = left.join(right, $"k" === $"rk").select("v", "w")
+        testStream(df)(
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          assertReadSchemas(Seq("k", "v", "unused"), Seq("rk", "w")),
+          CheckAnswer(),
+          StopStream,
+          // The pruned scan returns (v, k), and the join still gets (k, v).
+          insert("(1, 7)", rightTable),
+          StartStream(additionalConfs = pruningEnabled(true)),
+          ProcessAllAvailable(),
+          assertReadSchemas(Seq("v", "k"), Seq("rk", "w")),
+          CheckAnswer(Row(100L, 7)),
+          // And the other way around.
+          insert("(2, 200, 0)", leftTable),
+          ProcessAllAvailable(),
+          StopStream,
+          insert("(2, 8)", rightTable),
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(100L, 7), Row(200L, 8)))
+      }
+    }
+  }
+
+  test("keep the state of a streaming aggregation across the values of the 
conf when a scan " +
+      "returns the columns in another order") {
+    withPushDownConfsRestored {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (k bigint, v bigint, unused int) TBLPROPERTIES 
" +
+          s"('${InMemoryBaseTable.SIMULATE_REORDERED_COLUMN_PRUNING}' = 
'true')")
+        sql(s"INSERT INTO $tbl VALUES (1, 10, 0), (2, 20, 0)")
+        val df = spark.readStream.table(tbl).groupBy("k").agg(sum("v"))
+        testStream(df, OutputMode.Complete())(
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          assertReadSchema("k", "v", "unused"),
+          CheckAnswer(Row(1L, 10L), Row(2L, 20L)),
+          StopStream,
+          insert("(1, 1, 0)"),
+          StartStream(additionalConfs = pruningEnabled(true)),
+          ProcessAllAvailable(),
+          assertReadSchema("v", "k"),
+          CheckAnswer(Row(1L, 11L), Row(2L, 20L)),
+          StopStream,
+          insert("(2, 2, 0)"),
+          StartStream(additionalConfs = pruningEnabled(false)),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(1L, 11L), Row(2L, 22L)))
+      }
+    }
+  }
+
+  test("fail to plan a micro-batch that reads a column that its scan does not 
read") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relation = query.logicalPlan.collectFirst {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }.get
+          assert(relation.output.map(_.name) === Seq("id", "data", "extra"))
+          assert(relation.prunedOutput.map(_.map(_.name)) === 
Some(Seq("data")))
+          assert(relation.simpleString(10).contains("PrunedOutput: [data#"))
+          val offset = relation.stream.initialOffset()
+          val batch = relation.copy(startOffset = Some(offset), endOffset = 
Some(offset))
+          // Unlike the query, this plan reads all the columns of the relation.
+          val e = intercept[SparkException] {
+            spark.sessionState.planner.plan(Project(relation.output, 
batch)).next()
+          }
+          assert(e.getCondition === "INTERNAL_ERROR")
+          assert(e.getMessage.contains("does not read the columns"))
+          
assert(e.getMessage.contains(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key))
+          true
+        })
+    }
+  }
+
+  test("read only the columns that a continuous query needs") {
+    val input = ContinuousMemoryStream.singlePartition[(Long, String, Int)]
+    val table = new PruningContinuousTable(input, takesFilters = false)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"pruning_continuous", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+    withTempView("continuous_pruning") {
+      val query = 
df.writeStream.format("memory").queryName("continuous_pruning")
+        .trigger(Trigger.Continuous(100)).start()
+      try {
+        input.addData((1L, "a", 10), (2L, "b", 20), (3L, "c", 30))
+        eventually(timeout(streamingTimeout)) {
+          checkAnswer(spark.table("continuous_pruning"), Seq(Row("b"), 
Row("c")))
+        }
+        val readSchemas = 
query.asInstanceOf[StreamingQueryWrapper].streamingQuery.logicalPlan
+          .collect { case r: StreamingDataSourceV2ScanRelation => 
r.scan.readSchema().fieldNames }
+        assert(readSchemas.map(_.toSeq) === Seq(Seq("_2", "_3")))
+      } finally {
+        query.stop()
+      }
+    }
+  }
+
+  test("push the filters of a streaming query down to the scan of its source") 
{
+    withPushDownConfsRestored {
+      withSourceTable() {
+        val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+        testStream(df)(
+          StartStream(additionalConfs = filterPushdownEnabled(true)),
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(extra,10)"),
+          // The scan of the in-memory table does not apply the filters, which 
Spark evaluates.
+          CheckAnswer(Row("b")),
+          StopStream,
+          insert("(3, 'c', 30), (4, 'd', 5)"),
+          StartStream(additionalConfs = filterPushdownEnabled(false)),
+          ProcessAllAvailable(),
+          assertPushedFilters(),
+          CheckAnswer(Row("b"), Row("c")),
+          StopStream,
+          insert("(5, 'e', 50), (6, 'f', 6)"),
+          StartStream(additionalConfs = filterPushdownEnabled(true)),
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(extra,10)"),
+          CheckAnswer(Row("b"), Row("c"), Row("e")))
+      }
+    }
+  }
+
+  test("push down only the filters that every read of a relation applies") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val df = source.where("extra > 10").union(source.where("extra > 10 AND 
id > 1"))
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row(2L, "b", 20), Row(2L, "b", 20)))
+    }
+  }
+
+  // Each case is a condition on a value that the optimizer of each 
micro-batch folds into its own
+  // value, such as its current time, and the column that it reads.
+  Seq(
+    "ts < current_timestamp()" -> "ts",
+    "ts < now()" -> "ts",
+    "CAST(ts AS DATE) < current_date()" -> "ts",
+    "CAST(ts AS TIMESTAMP_NTZ) < localtimestamp()" -> "ts",
+    "tod < current_time()" -> "tod",
+    "CAST(tod AS TIMESTAMP_NTZ) < TIMESTAMP_NTZ'2100-01-01 00:00:00'" -> "tod",
+    "CAST(tod AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'" -> "tod",
+    "data = current_database()" -> "data",
+    "data = current_timezone()" -> "data",
+    // The casts of the special strings, which SpecialDatetimeValues folds.
+    "d = 'today'" -> "d",
+    "ts < 'now'" -> "ts",
+    "d = to_date('today')" -> "d",
+    "d < CAST('tomorrow' AS DATE)" -> "d",
+    "CAST(ts AS TIMESTAMP_NTZ) > CAST('yesterday' AS TIMESTAMP_NTZ)" -> "ts",
+    // The casts of strings with only a time to timestamps, which take the 
current date.
+    "ts < '12:00:00'" -> "ts",
+    "ts > CAST('T09:30:00' AS TIMESTAMP)" -> "ts",
+    // Every cast of a string to a timestamp with local time zone is a 
placeholder when the query
+    // starts, also of a string with a date, or of a string column. 
TODO(SPARK-60926): push these
+    // down again when their values do not depend on the date.
+    "CAST(data AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'" -> "data",
+    "ts < '2100-01-01 12:00:00'" -> "ts"
+  ).foreach { case (condition, column) =>
+    test(s"do not push down a filter on a value that depends on when it is 
evaluated: $condition") {
+      withTable(tbl) {
+        sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp, tod 
time, d date) " +
+          pushdowns)
+        val df = spark.readStream.table(tbl).where(s"id > 1 AND 
$condition").select("id")
+        val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+        assert(reads.size === 1)
+        val read = reads.values.head
+        // The column of the condition is still read, but only the filter on 
the id is pushed down.
+        assert(read.requiredColumns.map(_.name).toSet === Set("id", column))
+        assert(read.filters.nonEmpty)
+        assert(read.filters.forall(_.references.map(_.name).toSet == 
Set("id")), read.filters)
+      }
+    }
+  }
+
+  test("do not push down a filter on the current time of a micro-batch") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, data string, ts timestamp) 
$pushdowns")
+      sql(s"INSERT INTO $tbl VALUES " +
+        "(1, 'a', timestamp_seconds(0)), (2, 'b', timestamp_seconds(10))")
+      // Each micro-batch has its own current time, so the time when the query 
started cannot be
+      // pushed down in its place.
+      Seq("current_timestamp()", "now()").foreach { time =>
+        val df = spark.readStream.table(tbl).where(s"id > 0 AND ts < 
$time").select("data")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(id,0)"),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("read the columns and apply the filters of every reference to a common 
table expression") {
+    withSourceTable() {
+      withTempView("s") {
+        spark.readStream.table(tbl).createOrReplaceTempView("s")
+        // Both references to the common table expression have the relation of 
the source, so its
+        // scan reads the columns of both, and applies no filter, as only one 
of them filters.
+        val df = sql("WITH c AS (SELECT * FROM s) " +
+          "SELECT a.id, b.extra FROM c a JOIN c b ON a.id = b.id WHERE a.data 
= 'a'")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("id", "data", "extra"),
+          assertPushedFilters(),
+          CheckAnswer(Row(1L, 10)),
+          insert("(3, 'a', 30)"),
+          ProcessAllAvailable(),
+          CheckAnswer(Row(1L, 10), Row(3L, 30)))
+
+        // Each reference applies its own filter.
+        val pruned = sql("WITH c AS (SELECT * FROM s) " +
+          "SELECT data FROM c WHERE extra > 15 UNION ALL SELECT data FROM c 
WHERE extra < 15")
+        testStream(pruned)(
+          ProcessAllAvailable(),
+          assertReadSchema("data", "extra"),
+          assertPushedFilters(),
+          CheckAnswer(Row("a"), Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("push down no filter that the optimizer infers from the filters of 
another source") {
+    val otherTable = "testcat.other"
+    withSourceTable() {
+      withTable(otherTable) {
+        sql(s"CREATE TABLE $otherTable (oid bigint, value string) $pushdowns")
+        val source = spark.readStream.table(tbl)
+        val other = spark.readStream.table(otherTable).where("oid < 3")
+        val df = source.join(other, source("id") === 
other("oid")).select("data", "value")
+        testStream(df)(
+          ProcessAllAvailable(),
+          // The optimizer infers a filter on the id of the source from the 
one on the other
+          // source, which is not pushed down: a scan only gets the filters 
that the query applies
+          // to its own source.
+          AssertOnQuery { query =>
+            assert(pushedFilters(query).sortBy(_.mkString) === Seq(Nil, 
Seq("LessThan(oid,3)")))
+            true
+          },
+          CheckAnswer(),
+          insert("(1, 'x'), (2, 'y'), (3, 'z')", otherTable),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("a", "x"), Row("b", "y")),
+          insert("(3, 'c', 30)"),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("a", "x"), Row("b", "y")))
+      }
+    }
+  }
+
+  test("plan the micro-batches whose filters imply the pushed filters in 
another form") {
+    val otherTable = "testcat.other"
+    withSourceTable() {
+      withTable(otherTable) {
+        sql(s"CREATE TABLE $otherTable (oid bigint, value string) $pushdowns")
+        sql(s"INSERT INTO $otherTable VALUES (1, 'x'), (2, 'y')")
+        // A micro-batch infers id = 2 from the equality of the static side, 
and puts its value in
+        // place of the column in the pushed filter id > 0, which it then 
drops: id = 2 implies it.
+        val source = spark.readStream.table(tbl)
+        val static = spark.range(1, 3).toDF("sid").where("sid = 2")
+        val joined = source.where("id > 0").join(static, source("id") === 
static("sid"))
+        testStream(joined.select("data"))(
+          ProcessAllAvailable(),
+          assertPushedFilters("GreaterThan(id,0)"),
+          CheckAnswer(Row("b")))
+        // So does a stream-stream join, in the micro-batches where both 
sources have data.
+        val other = spark.readStream.table(otherTable).where("oid = 2")
+        val streams = source.where("id > 0").join(other, source("id") === 
other("oid"))
+        testStream(streams.select("data", "value"))(
+          ProcessAllAvailable(),
+          CheckAnswer(Row("b", "y")),
+          insert("(2, 'bb', 22)"),
+          insert("(2, 'yy')", otherTable),
+          ProcessAllAvailable(),
+          CheckAnswer(Row("b", "y"), Row("b", "yy"), Row("bb", "y"), Row("bb", 
"yy")))
+      }
+    }
+  }
+
+  test("plan the micro-batches that fold a value of their own into a pushed 
filter") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, d date) $pushdowns")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), current_date()), (2, 
'other', NULL)")
+      // A micro-batch folds current_user() and now() into literals, which it 
then puts in place
+      // of the columns in the pushed filters owner <> '' and d >= 
DATE'2020-01-01'.
+      testStream(spark.readStream.table(tbl).where("owner <> '' AND owner = 
current_user()")
+          .select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Not(EqualTo(owner,))"),
+        CheckAnswer(Row(1L)))
+      testStream(spark.readStream.table(tbl)
+          .where("d >= DATE'2020-01-01' AND d = to_date(now())").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThanOrEqual(d,2020-01-01)"))
+    }
+  }
+
+  Seq(
+    "CAST('today' AS DATE) < DATE'2100-01-01'",
+    "CAST('12:00:00' AS TIMESTAMP) < TIMESTAMP'2100-01-01 00:00:00'",
+    // The casts of strings to arrays, structs and maps of timestamps cast 
each string.
+    "element_at(CAST(array('12:00:00') AS ARRAY<TIMESTAMP>), 1) < 
TIMESTAMP'2100-01-01 00:00:00'",
+    "CAST(named_struct('t', '12:00:00') AS STRUCT<t: TIMESTAMP>).t < " +
+      "TIMESTAMP'2100-01-01 00:00:00'",
+    "element_at(CAST(map('k', '12:00:00') AS MAP<STRING, TIMESTAMP>), 'k') < " 
+
+      "TIMESTAMP'2100-01-01 00:00:00'"
+  ).foreach { condition =>
+    test(s"read the columns of a condition on a string that depends on when it 
is cast: " +
+        condition) {
+      withSourceTable() {
+        // The condition takes the date of each micro-batch, so the scan reads 
both columns.
+        val df = spark.readStream.table(tbl).select(expr(s"CASE WHEN 
$condition THEN data " +
+          "ELSE CAST(extra AS STRING) END AS v"))
+        val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+        assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+          Seq(Set("data", "extra")))
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("data", "extra"),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("read the columns of a condition on a cast whose string is a literal 
only once optimized") {
+    withSourceTable() {
+      // The cast of `cutoff` is only foldable once the optimizer replaces the 
alias with the
+      // string, and then takes the date of the start of the query, so the 
condition would be
+      // folded with it if the cast stayed a cast.
+      val df = spark.readStream.table(tbl).withColumn("cutoff", 
lit("12:00:00"))
+        .select(expr("CASE WHEN CAST(cutoff AS TIMESTAMP) < 
TIMESTAMP'2100-01-01 00:00:00' " +
+          "THEN data ELSE CAST(extra AS STRING) END AS v"))
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+        Seq(Set("data", "extra")))
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        CheckAnswer(Row("a"), Row("b")))
+    }
+  }
+
+  test("push down no filter after a filter that is not pushed down") {
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, qty int, payload 
string) $pushdowns")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), 1, '1'), (2, 'other', 
0, 'abc')")
+      val raise = "CASE WHEN qty <= 0 THEN raise_error('bad qty') ELSE true 
END"
+      def pushed(df: DataFrame): Seq[Set[String]] = {
+        StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, 
None).values.toSeq
+          .flatMap(_.filters).map(_.references.map(_.name).toSet)
+      }
+      val source = spark.readStream.table(tbl)
+      // The filter on the owner has a value of each micro-batch, so it is not 
pushed down, and
+      // neither is any filter written after it: each micro-batch evaluates 
the pushed filters
+      // first, so a filter that fails on the row of the other owner, as the 
cast of its payload
+      // does with ANSI, or the error raised for its quantity 0, would fail 
the query.
+      Seq(raise -> Nil, "CAST(payload AS INT) > 0" -> Nil, "id > 0" -> 
Seq("GreaterThan(id,0)"))
+        .foreach { case (filter, taken) =>
+          val df = source.where(s"owner = current_user() AND $filter")
+          assert(pushed(df) === Nil, filter)
+          // The filters of the V1 builder of the in-memory tables compare 
columns with values,
+          // which cannot fail, so it gets id > 0 wherever it is. It cannot 
take the others.
+          testStream(df.select("id"))(
+            ProcessAllAvailable(),
+            assertPushedFilters(taken: _*),
+            CheckAnswer(Row(1L)))
+        }
+      // The filters written before it are pushed down, as the query evaluates 
them first anyway.
+      assert(pushed(source.where(s"$raise AND id > 0 AND owner = 
current_user() AND qty > 0")) ===
+        Seq(Set("qty"), Set("id")))
+      // With several reads of the relation, the pushed filters are those that 
all of them
+      // evaluate first, in the same order.
+      val read = source.where(s"$raise AND id > 0")
+      assert(pushed(read.union(read)) === Seq(Set("qty"), Set("id")))
+      assert(pushed(read.union(source.where(s"$raise AND qty > 0"))) === 
Seq(Set("qty")))
+      assert(pushed(read.union(source.where(s"id > 0 AND $raise"))) === Nil)
+    }
+  }
+
+  test("drop the rows that fail the pushed filters in the stream of a source") 
{
+    withSourceTable(filtering) {
+      // The stream only returns the rows that pass the pushed filter, so the 
progress of the query
+      // only counts them.
+      def inputRows(query: StreamExecution): Long = 
query.recentProgress.map(_.numInputRows).sum
+      testStream(spark.readStream.table(tbl).where("extra > 
10").select("data"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row("b")),
+        AssertOnQuery(inputRows(_) == 1),
+        insert("(3, 'c', 30), (4, 'd', 5), (5, 'e', 50)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("c"), Row("e")),
+        AssertOnQuery(inputRows(_) == 3))
+    }
+  }
+
+  test("filter the rows of a continuous stream with the filters that its scan 
builder took") {
+    val input = ContinuousMemoryStream.singlePartition[(Long, String, Int)]
+    val table = new PruningContinuousTable(input, takesFilters = true)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"pruning_continuous", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+    PruningContinuousTable.returnedRows.set(0)
+    withTempView("continuous_filtering") {
+      val query = 
df.writeStream.format("memory").queryName("continuous_filtering")
+        .trigger(Trigger.Continuous(100)).start()
+      try {
+        input.addData((1L, "a", 10), (2L, "b", 20), (3L, "c", 30))
+        eventually(timeout(streamingTimeout)) {
+          checkAnswer(spark.table("continuous_filtering"), Seq(Row("b"), 
Row("c")))
+        }
+        // The readers dropped the row that fails the filter, which the plan 
of the query applies
+        // to the rows of the source too.
+        assert(PruningContinuousTable.returnedRows.get() === 2)
+        val filtered = 
query.asInstanceOf[StreamingQueryWrapper].streamingQuery.logicalPlan
+          .collect {
+            case Filter(_, relation: StreamingDataSourceV2ScanRelation) =>
+              relation.pushedFilters.map(_.references.map(_.name).toSet)
+          }
+        assert(filtered === Seq(Seq(Set("_3"))))
+      } finally {
+        query.stop()
+      }
+    }
+  }
+
+  test("plan the micro-batches whose filters fold the pushed filters in part") 
{
+    // The streams drop the rows that fail the pushed filters, so the answers 
depend on them.
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (id bigint, owner string, d date) $filtering")
+      sql(s"INSERT INTO $tbl VALUES (1, current_user(), current_date()), " +
+        "(6, current_user(), current_date()), (7, 'other', current_date())")
+      // A micro-batch folds current_user() into a literal, which it puts in 
place of the column
+      // in the pushed filter owner = '' OR id > 5, which it then folds into 
id > 5.
+      testStream(spark.readStream.table(tbl)
+          .where("(owner = '' OR id > 5) AND owner = 
current_user()").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(EqualTo(owner,),GreaterThan(id,5))"),
+        CheckAnswer(Row(6L)))
+      // So does a date that is not the date of the micro-batch, as on the day 
after the start.
+      // The micro-batch is planned whether the rows have its date or not.
+      testStream(spark.readStream.table(tbl)
+          .where("(d < DATE'2000-01-01' OR id > 5) AND d = 
to_date(now())").select("id"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(LessThan(d,2000-01-01),GreaterThan(id,5))"))
+    }
+    // And an equality that a micro-batch infers through a stream-static join.
+    withTable(tbl) {
+      sql(s"CREATE TABLE $tbl (a bigint, b bigint) $filtering")
+      sql(s"INSERT INTO $tbl VALUES (2, 0), (0, 0), (5, 3)")
+      val source = spark.readStream.table(tbl).where("a > 1 OR b > 2")
+      val static = spark.range(0, 2).toDF("x").where("x = 0")
+      testStream(source.join(static, source("b") === static("x")).select("a"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("Or(GreaterThan(a,1),GreaterThan(b,2))"),
+        CheckAnswer(Row(2L)))
+    }
+  }
+
+  test("read all the columns with no filter from a table without the 
capabilities") {
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = '')") {
+      val df = spark.readStream.table(tbl).where("extra > 10").select("data")
+      testStream(df)(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra"),
+        assertScanOutputs(Seq("id", "data", "extra")),
+        assertPushedFilters(),
+        AssertOnQuery { query =>
+          // The plan of the query only applies the filter of the query.
+          val relations = query.logicalPlan.collect {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }
+          relations.nonEmpty && relations.forall(_.pushedFilters.isEmpty)
+        },
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  test("push down only what the capabilities of the table allow") {
+    val df = () => spark.readStream.table(tbl).where("extra > 
10").select("data")
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 'columns')") {
+      testStream(df())(
+        ProcessAllAvailable(),
+        assertReadSchema("data", "extra"),
+        assertPushedFilters(),
+        CheckAnswer(Row("b")))
+    }
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 'filters')") {
+      testStream(df())(
+        ProcessAllAvailable(),
+        assertReadSchema("id", "data", "extra"),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  Seq("pushFilters", "pruneColumns", "build", "toMicroBatchStream").foreach { 
failingStep =>
+    test(s"read all the columns with no filter when the scan fails with the 
pushdowns: " +
+        failingStep) {
+      val input = MemoryStream[(Long, String, Int)]
+      val table = new FailingPushDownTable(input, failingStep)
+      val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"failing_push_down", table,
+        CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+        None)).toDF("id", "data", "extra").where("extra > 10").select("data")
+      val logs = new LogAppender("the fallback of a streaming scan")
+      withLogAppender(logs,
+          loggerNames = 
Seq(StreamingV2ScanPushDown.getClass.getName.stripSuffix("$")),
+          level = Some(Level.WARN)) {
+        testStream(df)(
+          StartStream(),
+          AddData(input, (1L, "a", 10), (2L, "b", 20)),
+          ProcessAllAvailable(),
+          // A new scan builder builds a scan of all the columns, with no 
filter, as without the
+          // pushdowns.
+          assertReadSchema("_1", "_2", "_3"),
+          AssertOnQuery(_ => table.streams.asScala.toSeq == Seq(Seq("_1", 
"_2", "_3") -> Nil)),
+          CheckAnswer(Row("b")))
+      }
+      val warnings = logs.loggingEvents.map(_.getMessage.getFormattedMessage)
+      assert(warnings.exists { message =>
+        message.contains("could not be built with the pushdowns (columns: _2, 
_3, filters: ") &&
+          message.contains(s"$failingStep failed with the pushdowns")
+      }, warnings)
+    }
+  }
+
+  test("keep the placeholders of the values that depend on when they are 
evaluated apart") {
+    withSourceTable() {
+      val df = spark.readStream.table(tbl)
+        .select(current_user(), current_database(), current_catalog(), 
col("data"))
+      val placeholders = 
StreamingV2ScanPushDown.forStartup(df.queryExecution.analyzed)
+        .collectFirst { case project: Project => project.projectList.take(3) 
}.get
+        .map(_.asInstanceOf[Alias].child)
+      // The placeholders of different expressions differ, although they have 
the same type.
+      assert(placeholders.map(_.dataType).distinct.length === 1)
+      assert(placeholders.map(_.canonicalized).distinct.length === 3)
+    }
+  }
+
+  test("replace the casts of strings to timestamps with a local time zone") {
+    withSourceTable() {
+      // Each cast, and whether it takes the current date for a string with 
only a time: those to
+      // timestamps with a local time zone, of any precision, at any depth of 
their types and
+      // whatever their child is, unlike a cast to a timestamp without time 
zone or to a date,
+      // which rejects such a string.
+      val casts = Seq(
+        "CAST('12:00:00' AS TIMESTAMP)" -> true,
+        "CAST('12:00:00' AS TIMESTAMP_LTZ(9))" -> true,
+        "CAST('2100-01-01 12:00:00' AS TIMESTAMP)" -> true,
+        "CAST(data AS TIMESTAMP)" -> true,
+        "CAST(array('12:00:00') AS ARRAY<TIMESTAMP>)" -> true,
+        "CAST(named_struct('t', '12:00:00') AS STRUCT<t: TIMESTAMP>)" -> true,
+        "CAST(map('k', '12:00:00') AS MAP<STRING, TIMESTAMP>)" -> true,
+        "CAST('12:00:00' AS TIMESTAMP_NTZ)" -> false,
+        "CAST('2100-01-01' AS DATE)" -> false,
+        "CAST(array(1) AS ARRAY<STRING>)" -> false)
+      val df = spark.readStream.table(tbl).select(casts.map(c => expr(c._1)) 
:+ col("data"): _*)
+      val replaced = 
StreamingV2ScanPushDown.forStartup(df.queryExecution.analyzed)
+        .collectFirst { case project: Project => 
project.projectList.take(casts.length) }.get
+        .map(_.asInstanceOf[Alias].child)
+      assert(replaced.map(!_.isInstanceOf[Cast]) === casts.map(_._2))
+    }
+  }
+
+  test("apply the filters that the scan builder got in the plan of each 
micro-batch") {
+    withSourceTable() {
+      // id + extra > 10 cannot be translated to a filter of the source, so 
the scan builder only
+      // gets extra > 15. Each micro-batch applies it to the rows of the 
source, which the query
+      // applies anyway, so the scan cannot drop a row that the plan keeps, 
whatever it does with
+      // the filter. The filter that the scan builder does not get is 
evaluated after it, as for
+      // a batch scan, rather than on every row of the source.
+      testStream(spark.readStream.table(tbl).where("id + extra > 10 AND extra 
> 15")
+          .select("data"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,15)"),
+        AssertOnQuery { query =>
+          val filtered = query.logicalPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition -> relation
+          }
+          assert(filtered.length === 1)
+          val (condition, relation) = filtered.head
+          assert(relation.pushedFilters.map(_.references.map(_.name).toSet) 
=== Seq(Set("extra")))
+          assert(condition === relation.pushedFilters.head)
+          true
+        },
+        CheckAnswer(Row("b")),
+        insert("(3, 'c', 5), (4, 'd', 30)"),
+        ProcessAllAvailable(),
+        CheckAnswer(Row("b"), Row("d")))
+    }
+  }
+
+  test("push down the filters of a query that joins a source with an inline 
table") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val inline = sql("SELECT * FROM VALUES (2, current_date()) AS s(k, d)")
+      val df = source.where("extra > 10").join(inline, source("id") === 
inline("k"))
+        .select("data", "d")
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq ===
+        Seq(Set("id", "data", "extra")))
+      
assert(reads.values.flatMap(_.filters).map(_.references.map(_.name).toSet).toSeq
 ===
+        Seq(Set("extra")))
+    }
+  }
+
+  test("filter the rows of each read of a source with the filters that its 
scan builder got") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      val df = source.where("extra > 10").select("data")
+        .union(source.where("extra > 10 AND id > 1").select("data"))
+      testStream(df)(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          // Both reads of the relation are filtered with the filter that they 
both apply.
+          val filtered = query.logicalPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition -> relation
+          }
+          assert(filtered.length === 2)
+          filtered.foreach { case (condition, relation) =>
+            assert(relation.pushedFilters.map(_.toString) ===
+              Seq(s"(${relation.output(2)} > 10)"))
+            assert(condition === relation.pushedFilters.head)
+          }
+          // So is each read of the relation in the optimized plan of a 
micro-batch.
+          val microBatchFilters = query.lastExecution.optimizedPlan.collect {
+            case Filter(condition, relation: 
StreamingDataSourceV2ScanRelation) =>
+              condition.exists(c => 
relation.pushedFilters.exists(c.semanticEquals))
+          }
+          assert(microBatchFilters === Seq(true, true))
+          true
+        },
+        CheckAnswer(Row("b"), Row("b")))
+    }
+  }
+
+  test("do not build the scans of the batch relations of a streaming query 
when it starts") {
+    withSourceTable() {
+      val table = new CountingBatchTable
+      val static = Dataset.ofRows(spark, DataSourceV2Relation.create(
+        table, None, None, CaseInsensitiveStringMap.empty()))
+      val source = spark.readStream.table(tbl)
+      val df = source.join(static, source("id") === 
static("sid")).select("data")
+      val reads = StreamingV2ScanPushDown.reads(spark, 
df.queryExecution.analyzed, None)
+      assert(reads.values.map(_.requiredColumns.map(_.name).toSet).toSeq === 
Seq(Set("id", "data")))
+      assert(table.scanBuilders.get() === 0)
+    }
+  }
+
+  test("read all the columns of a source whose pruned scan has no read 
schema") {
+    val input = MemoryStream[(Long, String, Int)]
+    val table = new NoPrunedReadSchemaTable(input)
+    val df = Dataset.ofRows(spark, StreamingRelationV2(None, 
"no_pruned_read_schema", table,
+      CaseInsensitiveStringMap.empty(), 
DataTypeUtils.toAttributes(table.schema()), None, None,
+      None)).toDF("id", "data", "extra").select("data")
+    testStream(df)(
+      StartStream(),
+      AddData(input, (1L, "a", 10), (2L, "b", 20)),
+      ProcessAllAvailable(),
+      // The pruned scan is replaced by a scan of all the columns before its 
stream is created.
+      assertReadSchema("_1", "_2", "_3"),
+      AssertOnQuery(_ => table.streams.get() == 1),
+      CheckAnswer(Row("a"), Row("b")))
+  }
+
+  test("read all the columns when the pruning of streaming scans is disabled") 
{
+    withSQLConf(SQLConf.STREAMING_V2_COLUMN_PRUNING_ENABLED.key -> "false") {
+      withSourceTable() {
+        val df = spark.readStream.table(tbl).select("data")
+        testStream(df)(
+          ProcessAllAvailable(),
+          assertReadSchema("id", "data", "extra"),
+          assertScanOutputs(Seq("id", "data", "extra")),
+          CheckAnswer(Row("a"), Row("b")))
+      }
+    }
+  }
+
+  test("canonicalize and show the pushed filters of a streaming scan 
relation") {
+    withSourceTable() {
+      testStream(spark.readStream.table(tbl).where("extra > 
10").select("data"))(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          val relation = query.logicalPlan.collectFirst {
+            case relation: StreamingDataSourceV2ScanRelation => relation
+          }.get
+          assert(relation.pushedFilters.nonEmpty)
+          // A copy of the relation has new attributes, also in its pushed 
filters, and the same
+          // result.
+          val copy = 
relation.newInstance().asInstanceOf[StreamingDataSourceV2ScanRelation]
+          assert(copy.pushedFilters.flatMap(_.references).map(_.exprId).toSet
+            .intersect(relation.output.map(_.exprId).toSet).isEmpty)
+          assert(copy.sameResult(relation))
+          // The plan applies the pushed filters with those of the query, so 
the relation shows
+          // the filters that its scan builder was given.
+          val string = relation.simpleString(100)
+          assert(string.contains("FiltersOfScanBuilder: [") && 
string.contains(" > 10)]"), string)
+          true
+        },
+        CheckAnswer(Row("b")))
+    }
+  }
+
+  test("push down no filter above a projection of a value of each micro-batch 
without " +
+      "constraint propagation") {
+    withSourceTable() {
+      def pushed(df: DataFrame): Seq[Set[String]] = {
+        StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, 
None).values.toSeq
+          .flatMap(_.filters).map(_.references.map(_.name).toSet)
+      }
+      val source = spark.readStream.table(tbl)
+      // The query outputs the value, so that the optimizer keeps its 
projection.
+      val df = source.withColumn("pt", current_timestamp()).where("extra > 10")
+        .select("data", "pt")
+      assert(pushed(df) === Seq(Set("extra")))
+      // The propagation of the constraints prunes the copy of the filter of 
the query that each
+      // micro-batch keeps above the projection, so it evaluates the filter 
once.
+      testStream(df)(
+        ProcessAllAvailable(),
+        AssertOnQuery { query =>
+          def conjuncts(e: Expression): Seq[Expression] = e match {
+            case And(left, right) => conjuncts(left) ++ conjuncts(right)
+            case other => Seq(other)
+          }
+          val conditions = query.lastExecution.optimizedPlan.collect {
+            case filter: Filter => filter.condition
+          }.flatMap(conjuncts).filter(_.references.map(_.name).toSet == 
Set("extra"))
+            .filterNot(_.isInstanceOf[IsNotNull])
+          assert(conditions.length === 1, conditions)
+          true
+        })
+      withSQLConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED.key -> "false") {
+        // Without it, the filter above the projection would be evaluated 
twice: it is not pushed
+        // down, but the filters below the projection still are.
+        assert(pushed(df) === Nil)
+        assert(pushed(source.where("id > 0").withColumn("pt", 
current_timestamp())
+          .where("extra > 10")) === Seq(Set("id")))
+      }
+    }
+  }
+
+  test("do not optimize a query without conditions when no source prunes its 
columns") {
+    def reads(df: DataFrame): Map[StreamingRelationV2, 
StreamingV2ScanPushDown.Reads] =
+      StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, None)
+    withSourceTable(s"TBLPROPERTIES 
('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' = 'filters')") {
+      val source = spark.readStream.table(tbl)
+      // Nothing can be pushed down without a condition, so the query is not 
optimized, and its
+      // scan gets no filter.
+      assert(reads(source.select("data")).isEmpty)
+      assert(reads(source.union(source)).isEmpty)
+      assert(reads(source.where("extra > 10")).nonEmpty)
+      assert(reads(source.join(spark.range(3).toDF("id"), "id")).nonEmpty)
+      // The optimizer turns the conditions of an as-of or a lateral join into 
filters too.
+      val static = spark.range(3).toDF("sid")
+      assert(reads(source.joinAsOf(static, source("id"), static("sid"), 
source("extra") > 10,
+        "inner", null, allowExactMatches = true, "backward")).nonEmpty)
+      withTempView("s") {
+        source.createOrReplaceTempView("s")
+        assert(reads(sql("SELECT * FROM s JOIN LATERAL (SELECT s.id AS x) ON 
s.extra > 10"))
+          .nonEmpty)
+      }
+    }
+    // A source that prunes its columns needs the optimized query either way.
+    withSourceTable() {
+      assert(reads(spark.readStream.table(tbl).select("data")).nonEmpty)
+    }
+  }
+
+  test("push down no filter that the optimizer infers from a generator") {
+    def reads(df: DataFrame): Map[StreamingRelationV2, 
StreamingV2ScanPushDown.Reads] =
+      StreamingV2ScanPushDown.reads(spark, df.queryExecution.analyzed, None)
+    def pushed(df: DataFrame): Seq[Set[String]] =
+      
reads(df).values.toSeq.flatMap(_.filters).map(_.references.map(_.name).toSet)
+    // The optimizer infers isnotnull(arr) and size(arr) > 0 from the explode 
of arr, which the
+    // query does not write: the scan only gets the filters that the query 
applies, whether the
+    // query has another filter or not, and whether the table prunes its 
columns or not.
+    Seq(pushdowns, s"TBLPROPERTIES ('${InMemoryBaseTable.STREAMING_PUSHDOWNS}' 
= 'filters')")
+      .foreach { properties =>
+        withTable(tbl) {
+          sql(s"CREATE TABLE $tbl (id bigint, arr array<int>) $properties")
+          val source = spark.readStream.table(tbl)
+          assert(pushed(source.select(explode(col("arr")))) === Nil, 
properties)
+          assert(pushed(source.where("id > 0").select(explode(col("arr")))) 
=== Seq(Set("id")),
+            properties)
+        }
+      }
+  }
+
+  test("push down only the leading predicates that a V2 filter builder gets") {
+    val table = new V2FilterTable
+    val output = DataTypeUtils.toAttributes(table.schema())
+    val payload = output(1)
+    // RLIKE cannot be translated to a predicate of the source, while the cast 
can, and fails on
+    // a payload that is not a number with ANSI, which the RLIKE written 
before it drops.
+    val matches = RLike(payload, Literal("^[0-9]+$"))
+    val positive = GreaterThan(Cast(payload, IntegerType, evalMode = 
EvalMode.ANSI), Literal(0))
+    assert(DataSourceV2Strategy.translateFilterV2(matches).isEmpty)
+    assert(DataSourceV2Strategy.translateFilterV2(positive).nonEmpty)
+    def pushed(filters: Expression*): Seq[Expression] = {
+      StreamingV2ScanPushDown.buildScan(table, 
CaseInsensitiveStringMap.empty(), output,
+          spark.sessionState.conf,
+          Some(StreamingV2ScanPushDown.Reads(AttributeSet(output), 
Seq(filters))))(_ => null)
+        .pushedFilters
+    }
+    assert(pushed(matches, positive) === Nil)
+    assert(table.pushed.isEmpty)
+    assert(pushed(positive, matches) === Seq(positive))
+    assert(table.pushed.length === 1)
+  }
+
+  test("push down the filters that a V1 filter builder takes wherever they 
are") {
+    withSourceTable() {
+      val source = spark.readStream.table(tbl)
+      // A filter on a value of each micro-batch comes first, as in a window 
over the last hour:
+      // the filter after it is still pushed down to a V1 builder.
+      testStream(source.where("data <> current_user() AND extra > 
10").select("data"))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)"),
+        CheckAnswer(Row("b")))
+      // Both reads apply both filters, in other orders.
+      testStream(source.where("id > 1 AND extra > 10").select("data")
+          .union(source.where("extra > 10 AND id > 1").select("data")))(
+        ProcessAllAvailable(),
+        assertPushedFilters("GreaterThan(extra,10)", "GreaterThan(id,1)"),
+        CheckAnswer(Row("b"), Row("b")))
+    }
+  }
+
+  test("keep the rows of the test table that a pushed filter keeps, or cannot 
tell") {
+    import org.apache.spark.sql.sources.{EqualTo, GreaterThanOrEqual, LessThan}
+    val schema = StructType.fromDDL("d double, s string")
+    // A character outside the BMP, which UTF-16 code units order before 
U+FFFD.
+    val emoji = new String(Character.toChars(0x1F600))
+    val replacement = new String(Character.toChars(0xFFFD))
+    val row = InternalRow(-0.0, UTF8String.fromString(emoji))
+    // -0.0 is equal to 0.0 in Spark.
+    assert(InMemoryBaseTable.passes(EqualTo("d", 0.0), row, schema))
+    assert(InMemoryBaseTable.passes(GreaterThanOrEqual("d", 0.0), row, schema))
+    // Strings are ordered by code point: a character outside the BMP is after 
U+FFFD.
+    assert(InMemoryBaseTable.passes(GreaterThanOrEqual("s", replacement), row, 
schema))
+    assert(!InMemoryBaseTable.passes(LessThan("s", replacement), row, schema))
+    // A filter on a nested or quoted column, or on a column added after the 
row was written,
+    // keeps the row.
+    assert(InMemoryBaseTable.passes(EqualTo("s.x", 1), row, schema))
+    assert(InMemoryBaseTable.passes(EqualTo("added", 1), row,
+      StructType.fromDDL("d double, s string, added int")))
+  }
+}
+
+/**
+ * A table over a continuous memory stream, whose scans only read the columns 
that they are asked
+ * for: their readers return the rows of the stream with only these columns. 
If `takesFilters`,
+ * its scan builders also take the filters, and the readers drop the rows that 
fail them.
+ */
+private class PruningContinuousTable(input: ContinuousMemoryStream[_], 
takesFilters: Boolean)
+  extends Table with SupportsRead {
+
+  override def name(): String = "pruning_continuous"
+
+  override def schema(): StructType = input.fullSchema()
+
+  override def capabilities(): util.Set[TableCapability] = {
+    val capabilities =
+      util.EnumSet.of(TableCapability.CONTINUOUS_READ, 
TableCapability.STREAMING_COLUMN_PRUNING)
+    if (takesFilters) 
capabilities.add(TableCapability.STREAMING_FILTER_PUSHDOWN)
+    capabilities
+  }
+
+  override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder 
= {
+    new ScanBuilder with SupportsPushDownRequiredColumns with 
SupportsPushDownFilters {
+      private var columns = schema()
+      private var filters = Array.empty[SourceFilter]
+
+      override def pruneColumns(requiredSchema: StructType): Unit = {
+        columns = StructType(requiredSchema.fieldNames.map(schema()(_)))
+      }
+
+      override def pushFilters(filters: Array[SourceFilter]): 
Array[SourceFilter] = {
+        if (takesFilters) this.filters = filters
+        filters
+      }
+
+      override def pushedFilters(): Array[SourceFilter] = filters
+
+      override def build(): Scan = new Scan {
+        private val readFilters = filters
+
+        override def readSchema(): StructType = columns
+
+        override def toContinuousStream(checkpointLocation: String): 
ContinuousStream = {
+          new PruningContinuousStream(input, 
columns.fieldNames.map(schema().fieldIndex), columns,
+            readFilters, schema())
+        }
+      }
+    }
+  }
+}
+
+private object PruningContinuousTable {
+  // The rows that the readers of the streams that take filters returned.
+  val returnedRows = new AtomicLong()
+}
+
+/** A table whose scan builder takes V2 predicates, which it records. */
+private class V2FilterTable extends Table with SupportsRead {

Review Comment:
   Thank you, moved. `V2FilterTable` now comes after 
`PruningContinuousReaderFactory`, right before `CountingBatchTable`, so the 
continuous table, its companion object, its stream and its reader factory stay 
together.
   



-- 
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]

Reply via email to