flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] sunjincheng121 commented on a change in pull request #8057: [FLINK-12028][table] Add `addColumns`,`renameColumns`, `dropColumns` …
Date Thu, 28 Mar 2019 06:45:57 GMT
sunjincheng121 commented on a change in pull request #8057: [FLINK-12028][table] Add `addColumns`,`renameColumns`,
`dropColumns` …
URL: https://github.com/apache/flink/pull/8057#discussion_r269873295
 
 

 ##########
 File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/api/tableImpl.scala
 ##########
 @@ -451,6 +450,117 @@ class TableImpl(
     }
     tableName
   }
+
+  override def addColumns(fields: String): Table = {
+    addColumns(ExpressionParser.parseExpressionList(fields): _*);
+  }
+
+  override def addColumns(fields: Expression*): Table = {
+    addColumns(false, fields: _*)
+  }
+
+  override def addColumns(replaceIfExist: Boolean, fields: String): Table = {
+    addColumns(replaceIfExist, ExpressionParser.parseExpressionList(fields): _*)
+  }
+
+  override def addColumns(replaceIfExist: Boolean, fields: Expression*): Table = {
+
+    val aggNames = extractAggregationsAndProperties(
+      fields.map(expressionBridge.bridge), tableEnv)._1
+
+    if(aggNames.nonEmpty){
+      throw new TableException(
+        s"The added field expression cannot be an aggregations, find [${aggNames.head}].")
+    }
+
+    val childFields = logicalPlan.output.map(a => UnresolvedFieldReference(a.name))
+
+    if (replaceIfExist) {
+
+      val finalFields = new ListBuffer[Expression]()
+      val addFields = fields.map(expressionBridge.bridge)
+      childFields.foreach(e => finalFields.append(e))
+
+      // replace field if exist.
+      addFields.foreach {
+        case e@Alias(_, name, _) =>
+          val index = finalFields.indexWhere(p => p match {
+            case u: UnresolvedFieldReference => u.name.equalsIgnoreCase(name)
+            case a: Alias => a.name.equalsIgnoreCase(name)
+            case _ => false
+          })
+          if (index >= 0) {
+            finalFields(index) = e
 
 Review comment:
   I think is better to overwrite the field which has the same name. what do you think?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message