huaxingao commented on a change in pull request #33352:
URL: https://github.com/apache/spark/pull/33352#discussion_r674408330
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala
##########
@@ -49,6 +51,58 @@ case class JDBCScanBuilder(
override def pushedFilters(): Array[Filter] = pushedFilter
+ private var pushedAggregations = Option.empty[Aggregation]
+
+ private var pushedAggregateColumn: Array[String] = Array()
+
+ private def getStructFieldForCol(col: FieldReference): StructField =
+ schema.fields(schema.fieldNames.toList.indexOf(col.fieldNames.head))
+
+ override def pushAggregation(aggregation: Aggregation): Boolean = {
+ if (!jdbcOptions.pushDownAggregate) return false
+
+ val dialect = JdbcDialects.get(jdbcOptions.url)
+ val compiledAgg =
JDBCRDD.compileAggregates(aggregation.getAggregateExpressions, dialect)
+
+ var pushedSchema = new StructType()
+ aggregation.getGroupByColumns.foreach { col =>
+ val structField = getStructFieldForCol(col)
+ pushedSchema = pushedSchema.add(StructField(structField.name,
structField.dataType))
+ pushedAggregateColumn = pushedAggregateColumn :+
dialect.quoteIdentifier(structField.name)
+ }
+
+ // The column names here are already quoted and can be used to build sql
string directly.
+ // e.g. "DEPT","NAME",MAX("SALARY"),MIN("BONUS") =>
+ // SELECT "DEPT","NAME",MAX("SALARY"),MIN("BONUS") FROM "test"."employee"
+ // GROUP BY "DEPT", "NAME"
+ pushedAggregateColumn = pushedAggregateColumn ++ compiledAgg
+
+ aggregation.getAggregateExpressions.foreach {
+ case max: Max =>
+ val structField = getStructFieldForCol(max.getCol)
+ pushedSchema = pushedSchema.add(structField.copy("max(" +
structField.name + ")"))
Review comment:
I guess no. We only use quoted identifier in the sql text. We don't use
quoted identifier in the schema. For example, in the original plan as the
follows, we don't quote column name `SALARY` and `BONUS`.
```
Aggregate [DEPT#0], [max(SALARY#2) AS max(SALARY)#6, min(BONUS#3) AS
min(BONUS)#7]
```
Similarly, after push down aggregate, we don't want to quote the column name
`SALARY` and `BONUS` like `[max(max("SALARY")#29)` and `min(min("BONUS")#30)`.
We want to have the following
```
Aggregate [DEPT#0], [max(max(SALARY)#29) AS max(SALARY)#6,
min(min(BONUS)#30) AS min(BONUS)#7]
```
--
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]