You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by dw...@apache.org on 2019/04/15 10:44:29 UTC

[flink] branch master updated: [FLINK-11884][table] Ported alias validation on top of Expressions

This is an automated email from the ASF dual-hosted git repository.

dwysakowicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 807bce2  [FLINK-11884][table] Ported alias validation on top of Expressions
807bce2 is described below

commit 807bce2ff7be7f2c3ca09aab9a01363b8eb72a3b
Author: Dawid Wysakowicz <dw...@apache.org>
AuthorDate: Mon Apr 1 15:06:44 2019 +0200

    [FLINK-11884][table] Ported alias validation on top of Expressions
---
 .../table/operations/AliasOperationUtils.java      | 110 +++++++++++++++++++++
 .../table/operations/OperationTreeBuilder.scala    |   6 +-
 .../flink/table/plan/logical/operators.scala       |  30 +-----
 3 files changed, 114 insertions(+), 32 deletions(-)

diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/operations/AliasOperationUtils.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/operations/AliasOperationUtils.java
new file mode 100644
index 0000000..ba8e87a
--- /dev/null
+++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/operations/AliasOperationUtils.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.operations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.api.Types;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.expressions.ApiExpressionDefaultVisitor;
+import org.apache.flink.table.expressions.ApiExpressionUtils;
+import org.apache.flink.table.expressions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.UnresolvedReferenceExpression;
+import org.apache.flink.table.expressions.ValueLiteralExpression;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * Utility class for creating valid alias expressions that can be later used as a projection.
+ */
+@Internal
+public final class AliasOperationUtils {
+
+	private static final AliasLiteralValidator aliasLiteralValidator = new AliasLiteralValidator();
+	private static final String ALL_REFERENCE = "*";
+
+	/**
+	 * Creates a list of valid alias expressions. Resulting expression might still contain
+	 * {@link UnresolvedReferenceExpression}.
+	 *
+	 * @param aliases aliases to validate
+	 * @param child relational operation on top of which to apply the aliases
+	 * @return validated list of aliases
+	 */
+	public static List<Expression> createAliasList(List<Expression> aliases, TableOperation child) {
+		TableSchema childSchema = child.getTableSchema();
+
+		if (aliases.size() > childSchema.getFieldCount()) {
+			throw new ValidationException("Aliasing more fields than we actually have.");
+		}
+
+		List<ValueLiteralExpression> fieldAliases = aliases.stream()
+			.map(f -> f.accept(aliasLiteralValidator))
+			.collect(Collectors.toList());
+
+		String[] childNames = childSchema.getFieldNames();
+		return IntStream.range(0, childNames.length)
+			.mapToObj(idx -> {
+				UnresolvedReferenceExpression oldField = new UnresolvedReferenceExpression(childNames[idx]);
+				if (idx < fieldAliases.size()) {
+					ValueLiteralExpression alias = fieldAliases.get(idx);
+					return new CallExpression(BuiltInFunctionDefinitions.AS, Arrays.asList(oldField, alias));
+				} else {
+					return oldField;
+				}
+			}).collect(Collectors.toList());
+	}
+
+	private static class AliasLiteralValidator extends ApiExpressionDefaultVisitor<ValueLiteralExpression> {
+
+		@Override
+		public ValueLiteralExpression visitValueLiteral(ValueLiteralExpression valueLiteralExpression) {
+			String name = ApiExpressionUtils.extractValue(valueLiteralExpression, Types.STRING())
+				.orElseThrow(() -> new ValidationException(
+					"Alias accepts only names that are not '*' reference."));
+
+			if (name.equals(ALL_REFERENCE)) {
+				throw new ValidationException("Alias can not accept '*' as name.");
+			}
+			return valueLiteralExpression;
+		}
+
+		@Override
+		protected ValueLiteralExpression defaultMethod(Expression expression) {
+			throw new ValidationException("Alias accepts only names that are not '*' reference.");
+		}
+
+		@Override
+		public ValueLiteralExpression visitUnresolvedReference(UnresolvedReferenceExpression unresolvedReference) {
+
+			if (unresolvedReference.getName().equals(ALL_REFERENCE)) {
+				throw new ValidationException("Alias can not accept '*' as name.");
+			}
+			return new ValueLiteralExpression(unresolvedReference.getName());
+		}
+	}
+
+	private AliasOperationUtils() {
+	}
+}
diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/operations/OperationTreeBuilder.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/operations/OperationTreeBuilder.scala
index 38d0c41..7321cb8 100644
--- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/operations/OperationTreeBuilder.scala
+++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/operations/OperationTreeBuilder.scala
@@ -28,6 +28,7 @@ import org.apache.flink.table.expressions._
 import org.apache.flink.table.expressions.catalog.FunctionDefinitionCatalog
 import org.apache.flink.table.expressions.lookups.TableReferenceLookup
 import org.apache.flink.table.functions.utils.UserDefinedFunctionUtils
+import org.apache.flink.table.operations.AliasOperationUtils.createAliasList
 import org.apache.flink.table.plan.logical.{Minus => LMinus, _}
 import org.apache.flink.table.util.JavaScalaConversionUtil
 import org.apache.flink.table.util.JavaScalaConversionUtil.toScala
@@ -297,10 +298,9 @@ class OperationTreeBuilder(private val tableEnv: TableEnvironment) {
       child: TableOperation)
     : TableOperation = {
 
-    val childNode = child.asInstanceOf[LogicalNode]
-    val convertedFields = fields.asScala.map(expressionBridge.bridge)
+    val newFields = createAliasList(fields, child)
 
-    AliasNode(convertedFields, childNode).validate(tableEnv)
+    project(newFields, child, explicitAlias = true)
   }
 
   def filter(
diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/logical/operators.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/logical/operators.scala
index 8a92474..0ee5ca5 100644
--- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/logical/operators.scala
+++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/logical/operators.scala
@@ -29,7 +29,7 @@ import org.apache.calcite.tools.RelBuilder
 import org.apache.flink.api.common.typeinfo.BasicTypeInfo._
 import org.apache.flink.api.common.typeinfo.TypeInformation
 import org.apache.flink.api.java.operators.join.JoinType
-import org.apache.flink.table.api.{StreamTableEnvironment, TableEnvironment, Types, UnresolvedException}
+import org.apache.flink.table.api.{StreamTableEnvironment, TableEnvironment, Types}
 import org.apache.flink.table.calcite.{FlinkRelBuilder, FlinkTypeFactory}
 import org.apache.flink.table.expressions.PlannerExpressionUtils.isRowCountLiteral
 import org.apache.flink.table.expressions._
@@ -72,34 +72,6 @@ case class Project(
   }
 }
 
-case class AliasNode(aliasList: Seq[PlannerExpression], child: LogicalNode) extends UnaryNode {
-  override def output: Seq[Attribute] =
-    throw UnresolvedException("Invalid call to output on AliasNode")
-
-  override protected[logical] def construct(relBuilder: RelBuilder): RelBuilder =
-    throw UnresolvedException("Invalid call to toRelNode on AliasNode")
-
-  override def validate(tableEnv: TableEnvironment): LogicalNode = {
-    if (aliasList.length > child.output.length) {
-      failValidation("Aliasing more fields than we actually have")
-    } else if (!aliasList.forall(_.isInstanceOf[UnresolvedFieldReference])) {
-      failValidation("Alias only accept name expressions as arguments")
-    } else if (!aliasList.forall(_.asInstanceOf[UnresolvedFieldReference].name != "*")) {
-      failValidation("Alias can not accept '*' as name")
-    } else {
-      val names = aliasList.map(_.asInstanceOf[UnresolvedFieldReference].name)
-      val input = child.output
-      val expressions = names.zip(input).map { case (name, attr) =>
-        Alias(attr, name)
-      } ++ input.drop(names.length)
-      Project(
-        expressions.toList.map(_.asInstanceOf[PlannerExpression]).asJava,
-        child,
-        explicitAlias = true)
-    }
-  }
-}
-
 case class Distinct(child: LogicalNode) extends UnaryNode {
   override def output: Seq[Attribute] = child.output