cloud-fan commented on a change in pull request #34738:
URL: https://github.com/apache/spark/pull/34738#discussion_r765793372
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCV2Suite.scala
##########
@@ -138,33 +140,117 @@ class JDBCV2Suite extends QueryTest with
SharedSparkSession with ExplainSuiteHel
checkPushedLimit(df4, false, 0)
checkAnswer(df4, Seq(Row(1, 19000.00)))
+ val name = udf { (x: String) => x.matches("cat|dav|amy") }
+ val sub = udf { (x: String) => x.substring(0, 3) }
val df5 = spark.read
.table("h2.test.employee")
- .sort("SALARY")
+ .select($"SALARY", $"BONUS", sub($"NAME").as("shortName"))
+ .filter(name($"shortName"))
.limit(1)
+ // LIMIT is pushed down only if all the filters are pushed down
checkPushedLimit(df5, false, 0)
- checkAnswer(df5, Seq(Row(1, "cathy", 9000.00, 1200.0)))
+ checkAnswer(df5, Seq(Row(10000.00, 1000.0, "amy")))
+ }
+
+ private def checkPushedLimit(df: DataFrame, pushed: Boolean, limit: Int):
Unit = {
+ df.queryExecution.optimizedPlan.collect {
+ case relation: DataSourceV2ScanRelation => relation.scan match {
+ case v1: V1ScanWrapper =>
+ if (pushed) {
+ assert(v1.pushedDownOperators.limit === Some(limit))
+ } else {
+ assert(v1.pushedDownOperators.limit.isEmpty)
+ }
+ }
+ }
+ }
+
+ test("simple scan with top N") {
+ val df1 = spark.read.table("h2.test.employee")
+ .where($"dept" === 1).orderBy($"salary").limit(1)
+ val expectedSorts1 =
+ Seq(SortValue(FieldReference("salary"), SortDirection.ASCENDING,
NullOrdering.NULLS_FIRST))
+ checkPushedTopN(df1, true, 1, expectedSorts1)
+ checkAnswer(df1, Seq(Row(1, "cathy", 9000.00, 1200.0)))
+
+ val df2 = spark.read
+ .option("partitionColumn", "dept")
+ .option("lowerBound", "0")
+ .option("upperBound", "2")
+ .option("numPartitions", "2")
+ .table("h2.test.employee")
+ .filter($"dept" > 1)
+ .orderBy($"salary".desc)
+ .limit(1)
+ val expectedSorts2 =
+ Seq(SortValue(FieldReference("salary"), SortDirection.DESCENDING,
NullOrdering.NULLS_LAST))
+ checkPushedTopN(df2, true, 1, expectedSorts2)
+ checkAnswer(df2, Seq(Row(2, "alex", 12000.00, 1200.0)))
+
+ val df3 =
+ sql("SELECT name FROM h2.test.employee WHERE dept > 1 ORDER BY salary
NULLS LAST LIMIT 1")
+ val scan = df3.queryExecution.optimizedPlan.collectFirst {
+ case s: DataSourceV2ScanRelation => s
+ }.get
+ assert(scan.schema.names.sameElements(Seq("NAME")))
+ val expectedSorts3 =
+ Seq(SortValue(FieldReference("salary"), SortDirection.ASCENDING,
NullOrdering.NULLS_LAST))
+ checkPushedTopN(df3, true, 1, expectedSorts3)
+ checkAnswer(df3, Seq(Row("david")))
+
+ val df4 = spark.read.table("h2.test.employee")
+ .where($"dept" === 1).orderBy($"salary")
+ checkPushedTopN(df4, false, 0)
+ checkAnswer(df4, Seq(Row(1, "cathy", 9000.00, 1200.0), Row(1, "amy",
10000.00, 1000.0)))
+
+ val df5 = spark.read.table("h2.test.employee")
+ .where($"dept" === 1).limit(1)
+ checkPushedTopN(df5, false, 1)
+ checkAnswer(df5, Seq(Row(1, "amy", 10000.00, 1000.0)))
+
+ val df6 = spark.read
+ .table("h2.test.employee")
+ .groupBy("DEPT").sum("SALARY")
+ .orderBy("DEPT")
+ .limit(1)
+ checkPushedTopN(df6, false, 0)
+ checkAnswer(df6, Seq(Row(1, 19000.00)))
+
+ val df7 = spark.read
Review comment:
This is probably the simplest case, why it's `df7` not `df1`?
--
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]