flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] hequn8128 commented on a change in pull request #7235: [FLINK-10976] [table] Add support for aggregate to table API
Date Wed, 12 Dec 2018 04:19:15 GMT
hequn8128 commented on a change in pull request #7235: [FLINK-10976] [table] Add support for
aggregate to table API
URL: https://github.com/apache/flink/pull/7235#discussion_r240877602
 
 

 ##########
 File path: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/table.scala
 ##########
 @@ -1226,3 +1308,76 @@ class WindowGroupedTable(
     select(withResolvedAggFunctionCall: _*)
   }
 }
+
+class AggregatedTable(
+    private[flink] val table: Table,
+    private[flink] val groupKeys: Seq[Expression],
+    private[flink] val aggregateFunction: Expression) {
+
+  /**
+    * Performs a selection operation after an aggregate operation. The field expressions
+    * cannot contain table functions and aggregations.
+    *
+    * Example:
+    *
+    * {{{
+    *   val aggFunc: AggregateFunction[_, _] = new MyAggregateFunction
+    *   tableEnv.registerFunction("aggFunc", aggFunc);
+    *   table.groupBy('key).aggregate("aggFunc(a, b) as (f0, f1, f2)").select('key, 'f0,
'f1)
+    * }}}
+    */
+  def select(fields: Expression*): Table = {
+    val tableEnv = table.tableEnv
+    val (aggNames, propNames) = extractAggregationsAndProperties(Seq(aggregateFunction),
tableEnv)
+    val projectsOnAgg = replaceAggregationsAndProperties(
+      groupKeys ++ Seq(aggregateFunction), tableEnv, aggNames, propNames)
+    val projectFields = extractFieldReferences(groupKeys ++ Seq(aggregateFunction))
+
+    val aggTable = new Table(tableEnv,
+      Project(projectsOnAgg,
+        Aggregate(groupKeys, aggNames.map(a => Alias(a._1, a._2)).toSeq,
+                  Project(projectFields, table.logicalPlan).validate(tableEnv)
+        ).validate(tableEnv)
+      ).validate(tableEnv))
+
+    // expand the aggregate results
+    val projectsOnAggTable =
+      aggTable.logicalPlan.output.take(groupKeys.length) ++
+      expandProjectList(
+        Seq(Flattening(aggTable.logicalPlan.output.last)),
+        aggTable.logicalPlan,
+        tableEnv).zip(extractFieldNames(aggregateFunction)).map(a => Alias(a._1, a._2))
+
+    val expandedFields = expandProjectList(fields, aggTable.logicalPlan, tableEnv)
 
 Review comment:
   This `expandedFields` is used by the last `Project` bellow, so we can't expand fields from
the agg Table. Instead, we should expand fields from the flattened Table after aggregate.

   The error can be reproduced by `select("*")` after aggregate.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message