cloud-fan commented on a change in pull request #33352:
URL: https://github.com/apache/spark/pull/33352#discussion_r673992659



##########
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 + ")"))
+      case min: Min =>
+        val structField = getStructFieldForCol(min.getCol)
+        pushedSchema = pushedSchema.add(structField.copy("min(" + 
structField.name + ")"))
+      case count: Count =>
+        val distinct = if (count.getIsDinstinct) "DISTINCT " else ""
+        val structField = getStructFieldForCol(count.getCol)
+        pushedSchema =
+          pushedSchema.add(StructField(s"count($distinct" + structField.name + 
")", LongType))
+      case _: CountOne =>

Review comment:
       probably `CountStar` is a better name.




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