You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by rm...@apache.org on 2016/01/14 17:16:34 UTC
[37/50] flink git commit: [FLINK-1712] Remove "flink-staging" module
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/Table.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/Table.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/Table.scala
new file mode 100644
index 0000000..641f2fa
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/Table.scala
@@ -0,0 +1,294 @@
+/*
+ * 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.api.table
+
+import org.apache.flink.api.java.io.DiscardingOutputFormat
+import org.apache.flink.api.table.explain.PlanJsonParser
+import org.apache.flink.api.table.expressions.analysis.{GroupByAnalyzer, PredicateAnalyzer, SelectionAnalyzer}
+import org.apache.flink.api.table.expressions.{Expression, ResolvedFieldReference, UnresolvedFieldReference}
+import org.apache.flink.api.table.parser.ExpressionParser
+import org.apache.flink.api.table.plan._
+import org.apache.flink.api.scala._
+import org.apache.flink.api.scala.table._
+
+/**
+ * The abstraction for writing Table API programs. Similar to how the batch and streaming APIs
+ * have [[org.apache.flink.api.scala.DataSet]] and
+ * [[org.apache.flink.streaming.api.scala.DataStream]].
+ *
+ * Use the methods of [[Table]] to transform data. Use
+ * [[org.apache.flink.api.java.table.TableEnvironment]] to convert a [[Table]] back to a DataSet
+ * or DataStream.
+ *
+ * When using Scala a [[Table]] can also be converted using implicit conversions.
+ *
+ * Example:
+ *
+ * {{{
+ * val table = set.toTable('a, 'b)
+ * ...
+ * val table2 = ...
+ * val set = table2.toDataSet[MyType]
+ * }}}
+ *
+ * Operations such as [[join]], [[select]], [[where]] and [[groupBy]] either take arguments
+ * in a Scala DSL or as an expression String. Please refer to the documentation for the expression
+ * syntax.
+ */
+case class Table(private[flink] val operation: PlanNode) {
+
+ /**
+ * Performs a selection operation. Similar to an SQL SELECT statement. The field expressions
+ * can contain complex expressions and aggregations.
+ *
+ * Example:
+ *
+ * {{{
+ * in.select('key, 'value.avg + " The average" as 'average, 'other.substring(0, 10))
+ * }}}
+ */
+ def select(fields: Expression*): Table = {
+ val analyzer = new SelectionAnalyzer(operation.outputFields)
+ val analyzedFields = fields.map(analyzer.analyze)
+ val fieldNames = analyzedFields map(_.name)
+ if (fieldNames.toSet.size != fieldNames.size) {
+ throw new ExpressionException(s"Resulting fields names are not unique in expression" +
+ s""" "${fields.mkString(", ")}".""")
+ }
+ this.copy(operation = Select(operation, analyzedFields))
+ }
+
+ /**
+ * Performs a selection operation. Similar to an SQL SELECT statement. The field expressions
+ * can contain complex expressions and aggregations.
+ *
+ * Example:
+ *
+ * {{{
+ * in.select("key, value.avg + " The average" as average, other.substring(0, 10)")
+ * }}}
+ */
+ def select(fields: String): Table = {
+ val fieldExprs = ExpressionParser.parseExpressionList(fields)
+ select(fieldExprs: _*)
+ }
+
+ /**
+ * Renames the fields of the expression result. Use this to disambiguate fields before
+ * joining to operations.
+ *
+ * Example:
+ *
+ * {{{
+ * in.as('a, 'b)
+ * }}}
+ */
+ def as(fields: Expression*): Table = {
+ fields forall {
+ f => f.isInstanceOf[UnresolvedFieldReference]
+ } match {
+ case true =>
+ case false => throw new ExpressionException("Only field expression allowed in as().")
+ }
+ this.copy(operation = As(operation, fields.toArray map { _.name }))
+ }
+
+ /**
+ * Renames the fields of the expression result. Use this to disambiguate fields before
+ * joining to operations.
+ *
+ * Example:
+ *
+ * {{{
+ * in.as("a, b")
+ * }}}
+ */
+ def as(fields: String): Table = {
+ val fieldExprs = ExpressionParser.parseExpressionList(fields)
+ as(fieldExprs: _*)
+ }
+
+ /**
+ * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE
+ * clause.
+ *
+ * Example:
+ *
+ * {{{
+ * in.filter('name === "Fred")
+ * }}}
+ */
+ def filter(predicate: Expression): Table = {
+ val analyzer = new PredicateAnalyzer(operation.outputFields)
+ val analyzedPredicate = analyzer.analyze(predicate)
+ this.copy(operation = Filter(operation, analyzedPredicate))
+ }
+
+ /**
+ * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE
+ * clause.
+ *
+ * Example:
+ *
+ * {{{
+ * in.filter("name = 'Fred'")
+ * }}}
+ */
+ def filter(predicate: String): Table = {
+ val predicateExpr = ExpressionParser.parseExpression(predicate)
+ filter(predicateExpr)
+ }
+
+ /**
+ * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE
+ * clause.
+ *
+ * Example:
+ *
+ * {{{
+ * in.where('name === "Fred")
+ * }}}
+ */
+ def where(predicate: Expression): Table = {
+ filter(predicate)
+ }
+
+ /**
+ * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE
+ * clause.
+ *
+ * Example:
+ *
+ * {{{
+ * in.where("name = 'Fred'")
+ * }}}
+ */
+ def where(predicate: String): Table = {
+ filter(predicate)
+ }
+
+ /**
+ * Groups the elements on some grouping keys. Use this before a selection with aggregations
+ * to perform the aggregation on a per-group basis. Similar to a SQL GROUP BY statement.
+ *
+ * Example:
+ *
+ * {{{
+ * in.groupBy('key).select('key, 'value.avg)
+ * }}}
+ */
+ def groupBy(fields: Expression*): Table = {
+ val analyzer = new GroupByAnalyzer(operation.outputFields)
+ val analyzedFields = fields.map(analyzer.analyze)
+
+ val illegalKeys = analyzedFields filter {
+ case fe: ResolvedFieldReference => false // OK
+ case e => true
+ }
+
+ if (illegalKeys.nonEmpty) {
+ throw new ExpressionException("Illegal key expressions: " + illegalKeys.mkString(", "))
+ }
+
+ this.copy(operation = GroupBy(operation, analyzedFields))
+ }
+
+ /**
+ * Groups the elements on some grouping keys. Use this before a selection with aggregations
+ * to perform the aggregation on a per-group basis. Similar to a SQL GROUP BY statement.
+ *
+ * Example:
+ *
+ * {{{
+ * in.groupBy("key").select("key, value.avg")
+ * }}}
+ */
+ def groupBy(fields: String): Table = {
+ val fieldsExpr = ExpressionParser.parseExpressionList(fields)
+ groupBy(fieldsExpr: _*)
+ }
+
+ /**
+ * Joins two [[Table]]s. Similar to an SQL join. The fields of the two joined
+ * operations must not overlap, use [[as]] to rename fields if necessary. You can use
+ * where and select clauses after a join to further specify the behaviour of the join.
+ *
+ * Example:
+ *
+ * {{{
+ * left.join(right).where('a === 'b && 'c > 3).select('a, 'b, 'd)
+ * }}}
+ */
+ def join(right: Table): Table = {
+ val leftInputNames = operation.outputFields.map(_._1).toSet
+ val rightInputNames = right.operation.outputFields.map(_._1).toSet
+ if (leftInputNames.intersect(rightInputNames).nonEmpty) {
+ throw new ExpressionException(
+ "Overlapping fields names on join input, result would be ambiguous: " +
+ operation.outputFields.mkString(", ") +
+ " and " +
+ right.operation.outputFields.mkString(", ") )
+ }
+ this.copy(operation = Join(operation, right.operation))
+ }
+
+ /**
+ * Union two[[Table]]s. Similar to an SQL UNION ALL. The fields of the two union operations
+ * must fully overlap.
+ *
+ * Example:
+ *
+ * {{{
+ * left.unionAll(right)
+ * }}}
+ */
+ def unionAll(right: Table): Table = {
+ val leftInputFields = operation.outputFields
+ val rightInputFields = right.operation.outputFields
+ if (!leftInputFields.equals(rightInputFields)) {
+ throw new ExpressionException(
+ "The fields names of join inputs should be fully overlapped, left inputs fields:" +
+ operation.outputFields.mkString(", ") +
+ " and right inputs fields" +
+ right.operation.outputFields.mkString(", ")
+ )
+ }
+ this.copy(operation = UnionAll(operation, right.operation))
+ }
+
+ /**
+ * Get the process of the sql parsing, print AST and physical execution plan.The AST
+ * show the structure of the supplied statement. The execution plan shows how the table
+ * referenced by the statement will be scanned.
+ */
+ def explain(extended: Boolean): String = {
+ val ast = operation
+ val dataSet = this.toDataSet[Row]
+ val env = dataSet.getExecutionEnvironment
+ dataSet.output(new DiscardingOutputFormat[Row])
+ val jasonSqlPlan = env.getExecutionPlan()
+ val sqlPlan = PlanJsonParser.getSqlExecutionPlan(jasonSqlPlan, extended)
+ val result = "== Abstract Syntax Tree ==\n" + ast + "\n\n" + "== Physical Execution Plan ==" +
+ "\n" + sqlPlan
+ return result
+ }
+
+ def explain(): String = explain(false)
+
+ override def toString: String = s"Expression($operation)"
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/TableConfig.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/TableConfig.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/TableConfig.scala
new file mode 100644
index 0000000..ffa2bec
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/TableConfig.scala
@@ -0,0 +1,66 @@
+/*
+ * 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.api.table
+
+import java.util.TimeZone
+
+/**
+ * A config to define the runtime behavior of the Table API.
+ */
+class TableConfig extends Serializable {
+
+ /**
+ * Defines the timezone for date/time/timestamp conversions.
+ */
+ private var timeZone: TimeZone = TimeZone.getTimeZone("UTC")
+
+ /**
+ * Defines if all fields need to be checked for NULL first.
+ */
+ private var nullCheck: Boolean = false
+
+ /**
+ * Sets the timezone for date/time/timestamp conversions.
+ */
+ def setTimeZone(timeZone: TimeZone) = {
+ require(timeZone != null, "timeZone must not be null.")
+ this.timeZone = timeZone
+ }
+
+ /**
+ * Returns the timezone for date/time/timestamp conversions.
+ */
+ def getTimeZone = timeZone
+
+ /**
+ * Returns the NULL check. If enabled, all fields need to be checked for NULL first.
+ */
+ def getNullCheck = nullCheck
+
+ /**
+ * Sets the NULL check. If enabled, all fields need to be checked for NULL first.
+ */
+ def setNullCheck(nullCheck: Boolean) = {
+ this.nullCheck = nullCheck
+ }
+
+}
+
+object TableConfig {
+ val DEFAULT = new TableConfig()
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/ExpressionCodeGenerator.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/ExpressionCodeGenerator.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/ExpressionCodeGenerator.scala
new file mode 100644
index 0000000..a03ba61
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/ExpressionCodeGenerator.scala
@@ -0,0 +1,829 @@
+/*
+ * 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.api.table.codegen
+
+import java.util.Date
+import java.util.concurrent.atomic.AtomicInteger
+
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo._
+import org.apache.flink.api.common.typeinfo.{BasicTypeInfo, PrimitiveArrayTypeInfo, TypeInformation}
+import org.apache.flink.api.common.typeutils.CompositeType
+import org.apache.flink.api.java.typeutils.{PojoTypeInfo, TupleTypeInfo}
+import org.apache.flink.api.scala.typeutils.CaseClassTypeInfo
+import org.apache.flink.api.table.expressions._
+import org.apache.flink.api.table.typeinfo.{RenamingProxyTypeInfo, RowTypeInfo}
+import org.apache.flink.api.table.{ExpressionException, TableConfig, expressions}
+import org.codehaus.janino.SimpleCompiler
+import org.slf4j.LoggerFactory
+
+import scala.collection.mutable
+
+/** Base class for all code generation classes. This provides the functionality for generating
+ * code from an [[Expression]] tree. Derived classes must embed this in a lambda function
+ * to form an executable code block.
+ *
+ * @param inputs List of input variable names with corresponding [[TypeInformation]].
+ * @param cl The ClassLoader that is used to create the Scala reflection ToolBox
+ * @param config General configuration specifying runtime behaviour.
+ * @tparam R The type of the generated code block. In most cases a lambda function such
+ * as "(IN1, IN2) => OUT".
+ */
+abstract class ExpressionCodeGenerator[R](
+ inputs: Seq[(String, CompositeType[_])],
+ cl: ClassLoader,
+ config: TableConfig) {
+ protected val log = LoggerFactory.getLogger(classOf[ExpressionCodeGenerator[_]])
+
+ import scala.reflect.runtime.universe._
+ import scala.reflect.runtime.{universe => ru}
+
+ if (cl == null) {
+ throw new IllegalArgumentException("ClassLoader must not be null.")
+ }
+
+ val compiler = new SimpleCompiler()
+ compiler.setParentClassLoader(cl)
+
+ protected val reusableMemberStatements = mutable.Set[String]()
+
+ protected val reusableInitStatements = mutable.Set[String]()
+
+ protected def reuseMemberCode(): String = {
+ reusableMemberStatements.mkString("", "\n", "\n")
+ }
+
+ protected def reuseInitCode(): String = {
+ reusableInitStatements.mkString("", "\n", "\n")
+ }
+
+ protected def nullCheck: Boolean = config.getNullCheck
+
+ // This is to be implemented by subclasses, we have it like this
+ // so that we only call it from here with the Scala Reflection Lock.
+ protected def generateInternal(): R
+
+ final def generate(): R = {
+ generateInternal()
+ }
+
+ protected def generateExpression(expr: Expression): GeneratedExpression = {
+ generateExpressionInternal(expr)
+ }
+
+ protected def generateExpressionInternal(expr: Expression): GeneratedExpression = {
+ // protected def generateExpression(expr: Expression): GeneratedExpression = {
+ val nullTerm = freshName("isNull")
+ val resultTerm = freshName("result")
+
+ // For binary predicates that must only be evaluated when both operands are non-null.
+ // This will write to nullTerm and resultTerm, so don't use those term names
+ // after using this function
+ def generateIfNonNull(left: Expression, right: Expression, resultType: TypeInformation[_])
+ (expr: (String, String) => String): String = {
+ val leftCode = generateExpression(left)
+ val rightCode = generateExpression(right)
+
+ val leftTpe = typeTermForTypeInfo(left.typeInfo)
+ val rightTpe = typeTermForTypeInfo(right.typeInfo)
+ val resultTpe = typeTermForTypeInfo(resultType)
+
+ if (nullCheck) {
+ leftCode.code + "\n" +
+ rightCode.code + "\n" +
+ s"""
+ |boolean $nullTerm = ${leftCode.nullTerm} || ${rightCode.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = ${defaultPrimitive(resultType)};
+ |} else {
+ | $resultTerm = ${expr(leftCode.resultTerm, rightCode.resultTerm)};
+ |}
+ """.stripMargin
+ } else {
+ leftCode.code + "\n" +
+ rightCode.code + "\n" +
+ s"""
+ |$resultTpe $resultTerm = ${expr(leftCode.resultTerm, rightCode.resultTerm)};
+ """.stripMargin
+ }
+ }
+
+ def cleanedExpr(e: Expression): Expression = {
+ e match {
+ case expressions.Naming(namedExpr, _) => cleanedExpr(namedExpr)
+ case _ => e
+ }
+ }
+
+ val cleanedExpression = cleanedExpr(expr)
+ val resultTpe = typeTermForTypeInfo(cleanedExpression.typeInfo)
+
+ val code: String = cleanedExpression match {
+
+ case expressions.Literal(null, typeInfo) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = true;
+ |$resultTpe resultTerm = null;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe resultTerm = null;
+ """.stripMargin
+ }
+
+ case expressions.Literal(intValue: Int, INT_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = $intValue;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = $intValue;
+ """.stripMargin
+ }
+
+ case expressions.Literal(longValue: Long, LONG_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = ${longValue}L;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = ${longValue}L;
+ """.stripMargin
+ }
+
+
+ case expressions.Literal(doubleValue: Double, DOUBLE_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = $doubleValue;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = $doubleValue;
+ """.stripMargin
+ }
+
+ case expressions.Literal(floatValue: Float, FLOAT_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = ${floatValue}f;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = ${floatValue}f;
+ """.stripMargin
+ }
+
+ case expressions.Literal(strValue: String, STRING_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = "$strValue";
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = "$strValue";
+ """.stripMargin
+ }
+
+ case expressions.Literal(boolValue: Boolean, BOOLEAN_TYPE_INFO) =>
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = $boolValue;
+ """.stripMargin
+ } else {
+ s"""
+ $resultTpe $resultTerm = $boolValue;
+ """.stripMargin
+ }
+
+ case expressions.Literal(dateValue: Date, DATE_TYPE_INFO) =>
+ val dateName = s"""date_${dateValue.getTime}"""
+ val dateStmt = s"""static java.util.Date $dateName
+ |= new java.util.Date(${dateValue.getTime});""".stripMargin
+ reusableMemberStatements.add(dateStmt)
+
+ if (nullCheck) {
+ s"""
+ |boolean $nullTerm = false;
+ |$resultTpe $resultTerm = $dateName;
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = $dateName;
+ """.stripMargin
+ }
+
+ case Substring(str, beginIndex, endIndex) =>
+ val strCode = generateExpression(str)
+ val beginIndexCode = generateExpression(beginIndex)
+ val endIndexCode = generateExpression(endIndex)
+ if (nullCheck) {
+ strCode.code +
+ beginIndexCode.code +
+ endIndexCode.code +
+ s"""
+ boolean $nullTerm =
+ ${strCode.nullTerm} || ${beginIndexCode.nullTerm} || ${endIndexCode.nullTerm};
+ $resultTpe $resultTerm;
+ if ($nullTerm) {
+ $resultTerm = ${defaultPrimitive(str.typeInfo)};
+ } else {
+ if (${endIndexCode.resultTerm} == Int.MaxValue) {
+ $resultTerm = (${strCode.resultTerm}).substring(${beginIndexCode.resultTerm});
+ } else {
+ $resultTerm = (${strCode.resultTerm}).substring(
+ ${beginIndexCode.resultTerm},
+ ${endIndexCode.resultTerm});
+ }
+ }
+ """.stripMargin
+ } else {
+ strCode.code +
+ beginIndexCode.code +
+ endIndexCode.code +
+ s"""
+ $resultTpe $resultTerm;
+
+ if (${endIndexCode.resultTerm} == Integer.MAX_VALUE) {
+ $resultTerm = (${strCode.resultTerm}).substring(${beginIndexCode.resultTerm});
+ } else {
+ $resultTerm = (${strCode.resultTerm}).substring(
+ ${beginIndexCode.resultTerm},
+ ${endIndexCode.resultTerm});
+ }
+ """
+ }
+
+ case expressions.Cast(child: Expression, STRING_TYPE_INFO)
+ if child.typeInfo == BasicTypeInfo.DATE_TYPE_INFO =>
+ val childGen = generateExpression(child)
+
+ addTimestampFormatter()
+
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $resultTerm = timestampFormatter.format(${childGen.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = timestampFormatter.format(${childGen.resultTerm});
+ """.stripMargin
+ }
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, STRING_TYPE_INFO) =>
+ val childGen = generateExpression(child)
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $resultTerm = "" + ${childGen.resultTerm};
+ |}
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = "" + ${childGen.resultTerm};
+ """.stripMargin
+ }
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, DATE_TYPE_INFO)
+ if child.typeInfo == BasicTypeInfo.LONG_TYPE_INFO =>
+ val childGen = generateExpression(child)
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $resultTerm = new java.util.Date(${childGen.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = new java.util.Date(${childGen.resultTerm});
+ """.stripMargin
+ }
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, DATE_TYPE_INFO)
+ if child.typeInfo == BasicTypeInfo.STRING_TYPE_INFO =>
+ val childGen = generateExpression(child)
+
+ addDateFormatter()
+ addTimeFormatter()
+ addTimestampFormatter()
+
+ // tries to parse
+ // "2011-05-03 15:51:36.234"
+ // then "2011-05-03"
+ // then "15:51:36"
+ // then "1446473775"
+ val parsedName = freshName("parsed")
+ val parsingCode =
+ s"""
+ |java.util.Date $parsedName = null;
+ |try {
+ | $parsedName = timestampFormatter.parse(${childGen.resultTerm});
+ |} catch (java.text.ParseException e1) {
+ | try {
+ | $parsedName = dateFormatter.parse(${childGen.resultTerm});
+ | } catch (java.text.ParseException e2) {
+ | try {
+ | $parsedName = timeFormatter.parse(${childGen.resultTerm});
+ | } catch (java.text.ParseException e3) {
+ | $parsedName = new java.util.Date(Long.valueOf(${childGen.resultTerm}));
+ | }
+ | }
+ |}
+ """.stripMargin
+
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $parsingCode
+ | $resultTerm = $parsedName;
+ |}
+ """.stripMargin
+ } else {
+ s"""
+ |$parsingCode
+ |$resultTpe $resultTerm = $parsedName;
+ """.stripMargin
+ }
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, DATE_TYPE_INFO) =>
+ throw new ExpressionException("Only Long and String can be casted to Date.")
+
+ case expressions.Cast(child: Expression, LONG_TYPE_INFO)
+ if child.typeInfo == BasicTypeInfo.DATE_TYPE_INFO =>
+ val childGen = generateExpression(child)
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $resultTerm = ${childGen.resultTerm}.getTime();
+ |}
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm = ${childGen.resultTerm}.getTime();
+ """.stripMargin
+ }
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, tpe: BasicTypeInfo[_])
+ if child.typeInfo == BasicTypeInfo.DATE_TYPE_INFO =>
+ throw new ExpressionException("Date can only be casted to Long or String.")
+
+ case expressions.Cast(child: Expression, tpe: BasicTypeInfo[_])
+ if child.typeInfo == BasicTypeInfo.STRING_TYPE_INFO =>
+ val childGen = generateExpression(child)
+ val fromTpe = typeTermForTypeInfoForCast(child.typeInfo)
+ val toTpe = typeTermForTypeInfoForCast(tpe)
+
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm =
+ | ${tpe.getTypeClass.getCanonicalName}.valueOf(${childGen.resultTerm});
+ """.stripMargin
+ } else {
+ s"""
+ |$resultTpe $resultTerm =
+ | ${tpe.getTypeClass.getCanonicalName}.valueOf(${childGen.resultTerm});
+ """.stripMargin
+ }
+
+ childGen.code + castCode
+
+ case expressions.Cast(child: Expression, tpe: BasicTypeInfo[_])
+ if child.typeInfo.isBasicType =>
+ val childGen = generateExpression(child)
+ val fromTpe = typeTermForTypeInfoForCast(child.typeInfo)
+ val toTpe = typeTermForTypeInfoForCast(tpe)
+ val castCode = if (nullCheck) {
+ s"""
+ |boolean $nullTerm = ${childGen.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = null;
+ |} else {
+ | $resultTerm = ($toTpe)($fromTpe) ${childGen.resultTerm};
+ |}
+ """.stripMargin
+ } else {
+ s"$resultTpe $resultTerm = ($toTpe)($fromTpe) ${childGen.resultTerm};\n"
+ }
+ childGen.code + castCode
+
+ case ResolvedFieldReference(fieldName, fieldTpe: TypeInformation[_]) =>
+ inputs find { i => i._2.hasField(fieldName)} match {
+ case Some((inputName, inputTpe)) =>
+ val fieldCode = getField(newTermName(inputName), inputTpe, fieldName, fieldTpe)
+ if (nullCheck) {
+ s"""
+ |$resultTpe $resultTerm = $fieldCode;
+ |boolean $nullTerm = $resultTerm == null;
+ """.stripMargin
+ } else {
+ s"""$resultTpe $resultTerm = $fieldCode;"""
+ }
+
+ case None => throw new ExpressionException("Could not get accessor for " + fieldName
+ + " in inputs " + inputs.mkString(", ") + ".")
+ }
+
+ case GreaterThan(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm > $rightTerm"
+ }
+
+ case GreaterThanOrEqual(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm >= $rightTerm"
+ }
+
+ case LessThan(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm < $rightTerm"
+ }
+
+ case LessThanOrEqual(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm <= $rightTerm"
+ }
+
+ case EqualTo(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm.equals($rightTerm)"
+ }
+
+ case NotEqualTo(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"!($leftTerm.equals($rightTerm))"
+ }
+
+ case And(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm && $rightTerm"
+ }
+
+ case Or(left, right) =>
+ generateIfNonNull(left, right, BOOLEAN_TYPE_INFO) {
+ (leftTerm, rightTerm) => s"$leftTerm || $rightTerm"
+ }
+
+ case Plus(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"$leftTerm + $rightTerm"
+ }
+
+ case Minus(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"$leftTerm - $rightTerm"
+ }
+
+ case Div(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"$leftTerm / $rightTerm"
+ }
+
+ case Mul(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"$leftTerm * $rightTerm"
+ }
+
+ case Mod(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"$leftTerm % $rightTerm"
+ }
+
+ case UnaryMinus(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |boolean $nullTerm = ${childCode.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = ${defaultPrimitive(child.typeInfo)};
+ |} else {
+ | $resultTerm = -(${childCode.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = -(${childCode.resultTerm});
+ """.stripMargin
+ }
+
+ case BitwiseAnd(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"(int) $leftTerm & (int) $rightTerm"
+ }
+
+ case BitwiseOr(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"(int) $leftTerm | (int) $rightTerm"
+ }
+
+ case BitwiseXor(left, right) =>
+ generateIfNonNull(left, right, expr.typeInfo) {
+ (leftTerm, rightTerm) => s"(int) $leftTerm ^ (int) $rightTerm"
+ }
+
+ case BitwiseNot(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |boolean $nullTerm = ${childCode.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = ${defaultPrimitive(child.typeInfo)};
+ |} else {
+ | $resultTerm = ~((int) ${childCode.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = ~((int) ${childCode.resultTerm});
+ """.stripMargin
+ }
+
+ case Not(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |boolean $nullTerm = ${childCode.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = ${defaultPrimitive(child.typeInfo)};
+ |} else {
+ | $resultTerm = !(${childCode.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = !(${childCode.resultTerm});
+ """.stripMargin
+ }
+
+ case IsNull(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = ${childCode.nullTerm};
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = (${childCode.resultTerm}) == null;
+ """.stripMargin
+ }
+
+ case IsNotNull(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = !${childCode.nullTerm};
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = (${childCode.resultTerm}) != null;
+ """.stripMargin
+ }
+
+ case Abs(child) =>
+ val childCode = generateExpression(child)
+ if (nullCheck) {
+ childCode.code +
+ s"""
+ |boolean $nullTerm = ${childCode.nullTerm};
+ |$resultTpe $resultTerm;
+ |if ($nullTerm) {
+ | $resultTerm = ${defaultPrimitive(child.typeInfo)};
+ |} else {
+ | $resultTerm = Math.abs(${childCode.resultTerm});
+ |}
+ """.stripMargin
+ } else {
+ childCode.code +
+ s"""
+ |$resultTpe $resultTerm = Math.abs(${childCode.resultTerm});
+ """.stripMargin
+ }
+
+ case _ => throw new ExpressionException("Could not generate code for expression " + expr)
+ }
+
+ GeneratedExpression(code, resultTerm, nullTerm)
+ }
+
+ case class GeneratedExpression(code: String, resultTerm: String, nullTerm: String)
+
+ def freshName(name: String): String = {
+ s"$name$$${freshNameCounter.getAndIncrement}"
+ }
+
+ val freshNameCounter = new AtomicInteger
+
+ protected def getField(
+ inputTerm: TermName,
+ inputType: CompositeType[_],
+ fieldName: String,
+ fieldType: TypeInformation[_]): String = {
+ val accessor = fieldAccessorFor(inputType, fieldName)
+ val fieldTpe = typeTermForTypeInfo(fieldType)
+
+ accessor match {
+ case ObjectFieldAccessor(fieldName) =>
+ val fieldTerm = newTermName(fieldName)
+ s"($fieldTpe) $inputTerm.$fieldTerm"
+
+ case ObjectMethodAccessor(methodName) =>
+ val methodTerm = newTermName(methodName)
+ s"($fieldTpe) $inputTerm.$methodTerm()"
+
+ case ProductAccessor(i) =>
+ s"($fieldTpe) $inputTerm.productElement($i)"
+
+ }
+ }
+
+ sealed abstract class FieldAccessor
+
+ case class ObjectFieldAccessor(fieldName: String) extends FieldAccessor
+
+ case class ObjectMethodAccessor(methodName: String) extends FieldAccessor
+
+ case class ProductAccessor(i: Int) extends FieldAccessor
+
+ def fieldAccessorFor(elementType: CompositeType[_], fieldName: String): FieldAccessor = {
+ elementType match {
+ case ri: RowTypeInfo =>
+ ProductAccessor(elementType.getFieldIndex(fieldName))
+
+ case cc: CaseClassTypeInfo[_] =>
+ ObjectMethodAccessor(fieldName)
+
+ case javaTup: TupleTypeInfo[_] =>
+ ObjectFieldAccessor(fieldName)
+
+ case pj: PojoTypeInfo[_] =>
+ ObjectFieldAccessor(fieldName)
+
+ case proxy: RenamingProxyTypeInfo[_] =>
+ val underlying = proxy.getUnderlyingType
+ val fieldIndex = proxy.getFieldIndex(fieldName)
+ fieldAccessorFor(underlying, underlying.getFieldNames()(fieldIndex))
+ }
+ }
+
+ protected def defaultPrimitive(tpe: TypeInformation[_]): String = tpe match {
+ case BasicTypeInfo.INT_TYPE_INFO => "-1"
+ case BasicTypeInfo.LONG_TYPE_INFO => "-1"
+ case BasicTypeInfo.SHORT_TYPE_INFO => "-1"
+ case BasicTypeInfo.BYTE_TYPE_INFO => "-1"
+ case BasicTypeInfo.FLOAT_TYPE_INFO => "-1.0f"
+ case BasicTypeInfo.DOUBLE_TYPE_INFO => "-1.0d"
+ case BasicTypeInfo.BOOLEAN_TYPE_INFO => "false"
+ case BasicTypeInfo.STRING_TYPE_INFO => "\"<empty>\""
+ case BasicTypeInfo.CHAR_TYPE_INFO => "'\\0'"
+ case _ => "null"
+ }
+
+ protected def typeTermForTypeInfo(tpe: TypeInformation[_]): String = tpe match {
+
+ // From PrimitiveArrayTypeInfo we would get class "int[]", scala reflections
+ // does not seem to like this, so we manually give the correct type here.
+ case PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO => "int[]"
+ case PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO => "long[]"
+ case PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO => "short[]"
+ case PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO => "byte[]"
+ case PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO => "float[]"
+ case PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO => "double[]"
+ case PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO => "boolean[]"
+ case PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO => "char[]"
+
+ case _ =>
+ tpe.getTypeClass.getCanonicalName
+
+ }
+
+ // when casting we first need to unbox Primitives, for example,
+ // float a = 1.0f;
+ // byte b = (byte) a;
+ // works, but for boxed types we need this:
+ // Float a = 1.0f;
+ // Byte b = (byte)(float) a;
+ protected def typeTermForTypeInfoForCast(tpe: TypeInformation[_]): String = tpe match {
+
+ case BasicTypeInfo.INT_TYPE_INFO => "int"
+ case BasicTypeInfo.LONG_TYPE_INFO => "long"
+ case BasicTypeInfo.SHORT_TYPE_INFO => "short"
+ case BasicTypeInfo.BYTE_TYPE_INFO => "byte"
+ case BasicTypeInfo.FLOAT_TYPE_INFO => "float"
+ case BasicTypeInfo.DOUBLE_TYPE_INFO => "double"
+ case BasicTypeInfo.BOOLEAN_TYPE_INFO => "boolean"
+ case BasicTypeInfo.CHAR_TYPE_INFO => "char"
+
+ // From PrimitiveArrayTypeInfo we would get class "int[]", scala reflections
+ // does not seem to like this, so we manually give the correct type here.
+ case PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO => "int[]"
+ case PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO => "long[]"
+ case PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO => "short[]"
+ case PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO => "byte[]"
+ case PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO => "float[]"
+ case PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO => "double[]"
+ case PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO => "boolean[]"
+ case PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO => "char[]"
+
+ case _ =>
+ tpe.getTypeClass.getCanonicalName
+
+ }
+
+ def addDateFormatter(): Unit = {
+ reusableMemberStatements.add(s"""
+ |java.text.SimpleDateFormat dateFormatter =
+ | new java.text.SimpleDateFormat("yyyy-MM-dd");
+ |""".stripMargin)
+
+ reusableInitStatements.add(s"""
+ |dateFormatter.setTimeZone(config.getTimeZone());
+ |""".stripMargin)
+ }
+
+ def addTimeFormatter(): Unit = {
+ reusableMemberStatements.add(s"""
+ |java.text.SimpleDateFormat timeFormatter =
+ | new java.text.SimpleDateFormat("HH:mm:ss");
+ |""".stripMargin)
+
+ reusableInitStatements.add(s"""
+ |timeFormatter.setTimeZone(config.getTimeZone());
+ |""".stripMargin)
+ }
+
+ def addTimestampFormatter(): Unit = {
+ reusableMemberStatements.add(s"""
+ |java.text.SimpleDateFormat timestampFormatter =
+ | new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+ |""".stripMargin)
+
+ reusableInitStatements.add(s"""
+ |timestampFormatter.setTimeZone(config.getTimeZone());
+ |""".stripMargin)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateFilter.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateFilter.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateFilter.scala
new file mode 100644
index 0000000..50b8c69
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateFilter.scala
@@ -0,0 +1,99 @@
+/*
+ * 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.api.table.codegen
+
+import java.io.StringReader
+
+import org.apache.flink.api.common.functions.FilterFunction
+import org.apache.flink.api.common.typeutils.CompositeType
+import org.apache.flink.api.table.TableConfig
+import org.apache.flink.api.table.codegen.Indenter._
+import org.apache.flink.api.table.expressions.Expression
+import org.slf4j.LoggerFactory
+
+/**
+ * Code generator for a unary predicate, i.e. a Filter.
+ */
+class GenerateFilter[T](
+ inputType: CompositeType[T],
+ predicate: Expression,
+ cl: ClassLoader,
+ config: TableConfig) extends ExpressionCodeGenerator[FilterFunction[T]](
+ Seq(("in0", inputType)),
+ cl = cl,
+ config) {
+
+ val LOG = LoggerFactory.getLogger(this.getClass)
+
+ override protected def generateInternal(): FilterFunction[T] = {
+ val pred = generateExpression(predicate)
+
+ val tpe = typeTermForTypeInfo(inputType)
+
+ val generatedName = freshName("GeneratedFilter")
+
+ // Janino does not support generics, so we need to cast by hand
+ val code = if (nullCheck) {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FilterFunction<$tpe> {
+
+ org.apache.flink.api.table.TableConfig config = null;
+
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ }
+
+ public boolean filter(Object _in0) {
+ $tpe in0 = ($tpe) _in0;
+ ${pred.code}
+ if (${pred.nullTerm}) {
+ return false;
+ } else {
+ return ${pred.resultTerm};
+ }
+ }
+ }
+ """
+ } else {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FilterFunction<$tpe> {
+
+ org.apache.flink.api.table.TableConfig config = null;
+
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ }
+
+ public boolean filter(Object _in0) {
+ $tpe in0 = ($tpe) _in0;
+ ${pred.code}
+ return ${pred.resultTerm};
+ }
+ }
+ """
+ }
+
+ LOG.debug(s"""Generated unary predicate "$predicate":\n$code""")
+ compiler.cook(new StringReader(code))
+ val clazz = compiler.getClassLoader().loadClass(generatedName)
+ val constructor = clazz.getConstructor(classOf[TableConfig])
+ constructor.newInstance(config).asInstanceOf[FilterFunction[T]]
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateJoin.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateJoin.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateJoin.scala
new file mode 100644
index 0000000..b706e6d
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateJoin.scala
@@ -0,0 +1,171 @@
+/*
+ * 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.api.table.codegen
+
+import java.io.StringReader
+
+import org.apache.flink.api.common.functions.FlatJoinFunction
+import org.apache.flink.api.common.typeutils.CompositeType
+import org.apache.flink.api.table.TableConfig
+import org.apache.flink.api.table.codegen.Indenter._
+import org.apache.flink.api.table.expressions.{Expression, NopExpression}
+import org.slf4j.LoggerFactory
+
+/**
+ * Code generator for assembling the result of a binary operation.
+ */
+class GenerateJoin[L, R, O](
+ leftTypeInfo: CompositeType[L],
+ rightTypeInfo: CompositeType[R],
+ resultTypeInfo: CompositeType[O],
+ predicate: Expression,
+ outputFields: Seq[Expression],
+ cl: ClassLoader,
+ config: TableConfig)
+ extends GenerateResultAssembler[FlatJoinFunction[L, R, O]](
+ Seq(("in0", leftTypeInfo), ("in1", rightTypeInfo)),
+ cl = cl,
+ config) {
+
+ val LOG = LoggerFactory.getLogger(this.getClass)
+
+
+ override protected def generateInternal(): FlatJoinFunction[L, R, O] = {
+
+ val leftTpe = typeTermForTypeInfo(leftTypeInfo)
+ val rightTpe = typeTermForTypeInfo(rightTypeInfo)
+ val resultTpe = typeTermForTypeInfo(resultTypeInfo)
+
+
+ val resultCode = createResult(resultTypeInfo, outputFields, o => s"coll.collect($o);")
+
+ val generatedName = freshName("GeneratedJoin")
+
+
+ val code = predicate match {
+ case n: NopExpression =>
+ // Janino does not support generics, that's why we need
+ // manual casting here
+ if (nullCheck) {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FlatFlatJoinFunction {
+
+ ${reuseCode(resultTypeInfo)}
+
+ public org.apache.flink.api.table.TableConfig config = null;
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ }
+
+ public void join(Object _in0, Object _in1, org.apache.flink.util.Collector coll) {
+ $leftTpe in0 = ($leftTpe) _in0;
+ $rightTpe in1 = ($rightTpe) _in1;
+
+ $resultCode
+ }
+ }
+ """
+ } else {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FlatJoinFunction {
+
+ ${reuseCode(resultTypeInfo)}
+
+ public org.apache.flink.api.table.TableConfig config = null;
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ }
+
+ public void join(Object _in0, Object _in1, org.apache.flink.util.Collector coll) {
+ $leftTpe in0 = ($leftTpe) _in0;
+ $rightTpe in1 = ($rightTpe) _in1;
+
+ $resultCode
+ }
+ }
+ """
+ }
+
+ case _ =>
+ val pred = generateExpression(predicate)
+ // Janino does not support generics, that's why we need
+ // manual casting here
+ if (nullCheck) {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FlatFlatJoinFunction {
+
+ ${reuseCode(resultTypeInfo)}
+
+ org.apache.flink.api.table.TableConfig config = null;
+
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ ${reuseInitCode()}
+ }
+
+ public void join(Object _in0, Object _in1, org.apache.flink.util.Collector coll) {
+ $leftTpe in0 = ($leftTpe) _in0;
+ $rightTpe in1 = ($rightTpe) _in1;
+
+ ${pred.code}
+
+ if (${pred.nullTerm} && ${pred.resultTerm}) {
+ $resultCode
+ }
+ }
+ }
+ """
+ } else {
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.FlatJoinFunction {
+
+ ${reuseCode(resultTypeInfo)}
+
+ org.apache.flink.api.table.TableConfig config = null;
+
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ ${reuseInitCode()}
+ }
+
+ public void join(Object _in0, Object _in1, org.apache.flink.util.Collector coll) {
+ $leftTpe in0 = ($leftTpe) _in0;
+ $rightTpe in1 = ($rightTpe) _in1;
+
+ ${pred.code}
+
+ if (${pred.resultTerm}) {
+ $resultCode
+ }
+ }
+ }
+ """
+ }
+ }
+
+ LOG.debug(s"""Generated join:\n$code""")
+ compiler.cook(new StringReader(code))
+ val clazz = compiler.getClassLoader().loadClass(generatedName)
+ val constructor = clazz.getConstructor(classOf[TableConfig])
+ constructor.newInstance(config).asInstanceOf[FlatJoinFunction[L, R, O]]
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateResultAssembler.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateResultAssembler.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateResultAssembler.scala
new file mode 100644
index 0000000..3916410
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateResultAssembler.scala
@@ -0,0 +1,119 @@
+/*
+ * 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.api.table.codegen
+
+import org.apache.flink.api.common.typeutils.CompositeType
+import org.apache.flink.api.java.typeutils.{PojoTypeInfo, TupleTypeInfo}
+import org.apache.flink.api.scala.typeutils.CaseClassTypeInfo
+import org.apache.flink.api.table.TableConfig
+import org.apache.flink.api.table.expressions.Expression
+import org.apache.flink.api.table.typeinfo.RowTypeInfo
+
+/**
+ * Base class for unary and binary result assembler code generators.
+ */
+abstract class GenerateResultAssembler[R](
+ inputs: Seq[(String, CompositeType[_])],
+ cl: ClassLoader,
+ config: TableConfig)
+ extends ExpressionCodeGenerator[R](inputs, cl = cl, config) {
+
+ def reuseCode[A](resultTypeInfo: CompositeType[A]) = {
+ val resultTpe = typeTermForTypeInfo(resultTypeInfo)
+ resultTypeInfo match {
+ case pj: PojoTypeInfo[_] =>
+ super.reuseMemberCode() +
+ s"$resultTpe out = new ${pj.getTypeClass.getCanonicalName}();"
+
+ case row: RowTypeInfo =>
+ super.reuseMemberCode() +
+ s"org.apache.flink.api.table.Row out =" +
+ s" new org.apache.flink.api.table.Row(${row.getArity});"
+
+ case _ => ""
+ }
+ }
+
+ def createResult[T](
+ resultTypeInfo: CompositeType[T],
+ outputFields: Seq[Expression],
+ result: String => String): String = {
+
+ val resultType = typeTermForTypeInfo(resultTypeInfo)
+
+ val fieldsCode = outputFields.map(generateExpression)
+
+ val block = resultTypeInfo match {
+ case ri: RowTypeInfo =>
+ val resultSetters: String = fieldsCode.zipWithIndex map {
+ case (fieldCode, i) =>
+ s"""
+ |${fieldCode.code}
+ |out.setField($i, ${fieldCode.resultTerm});
+ """.stripMargin
+ } mkString("\n")
+
+ s"""
+ |$resultSetters
+ |${result("out")}
+ """.stripMargin
+
+ case pj: PojoTypeInfo[_] =>
+ val resultSetters: String = fieldsCode.zip(outputFields) map {
+ case (fieldCode, expr) =>
+ val fieldName = expr.name
+ s"""
+ |${fieldCode.code}
+ |out.$fieldName = ${fieldCode.resultTerm};
+ """.stripMargin
+ } mkString("\n")
+
+ s"""
+ |$resultSetters
+ |${result("out")}
+ """.stripMargin
+
+ case tup: TupleTypeInfo[_] =>
+ val resultSetters: String = fieldsCode.zip(outputFields) map {
+ case (fieldCode, expr) =>
+ val fieldName = expr.name
+ s"""
+ |${fieldCode.code}
+ |out.$fieldName = ${fieldCode.resultTerm};
+ """.stripMargin
+ } mkString("\n")
+
+ s"""
+ |$resultSetters
+ |${result("out")}
+ """.stripMargin
+
+ case cc: CaseClassTypeInfo[_] =>
+ val fields: String = fieldsCode.map(_.code).mkString("\n")
+ val ctorParams: String = fieldsCode.map(_.resultTerm).mkString(",")
+
+ s"""
+ |$fields
+ |return new $resultType($ctorParams);
+ """.stripMargin
+ }
+
+ block
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateSelect.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateSelect.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateSelect.scala
new file mode 100644
index 0000000..a75d15b
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/GenerateSelect.scala
@@ -0,0 +1,84 @@
+/*
+ * 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.api.table.codegen
+
+import java.io.StringReader
+
+import org.apache.flink.api.common.functions.MapFunction
+import org.apache.flink.api.common.typeutils.CompositeType
+import org.apache.flink.api.table.TableConfig
+import org.apache.flink.api.table.codegen.Indenter._
+import org.apache.flink.api.table.expressions.Expression
+import org.slf4j.LoggerFactory
+
+/**
+ * Code generator for assembling the result of a select operation.
+ */
+class GenerateSelect[I, O](
+ inputTypeInfo: CompositeType[I],
+ resultTypeInfo: CompositeType[O],
+ outputFields: Seq[Expression],
+ cl: ClassLoader,
+ config: TableConfig)
+ extends GenerateResultAssembler[MapFunction[I, O]](
+ Seq(("in0", inputTypeInfo)),
+ cl = cl,
+ config) {
+
+ val LOG = LoggerFactory.getLogger(this.getClass)
+
+ override protected def generateInternal(): MapFunction[I, O] = {
+
+ val inputTpe = typeTermForTypeInfo(inputTypeInfo)
+ val resultTpe = typeTermForTypeInfo(resultTypeInfo)
+
+ val resultCode = createResult(resultTypeInfo, outputFields, o => s"return $o;")
+
+ val generatedName = freshName("GeneratedSelect")
+
+ // Janino does not support generics, that's why we need
+ // manual casting here
+ val code =
+ j"""
+ public class $generatedName
+ implements org.apache.flink.api.common.functions.MapFunction<$inputTpe, $resultTpe> {
+
+ ${reuseCode(resultTypeInfo)}
+
+ org.apache.flink.api.table.TableConfig config = null;
+
+ public $generatedName(org.apache.flink.api.table.TableConfig config) {
+ this.config = config;
+ ${reuseInitCode()}
+ }
+
+ @Override
+ public Object map(Object _in0) {
+ $inputTpe in0 = ($inputTpe) _in0;
+ $resultCode
+ }
+ }
+ """
+
+ LOG.debug(s"""Generated select:\n$code""")
+ compiler.cook(new StringReader(code))
+ val clazz = compiler.getClassLoader().loadClass(generatedName)
+ val constructor = clazz.getConstructor(classOf[TableConfig])
+ constructor.newInstance(config).asInstanceOf[MapFunction[I, O]]
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/Indenter.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/Indenter.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/Indenter.scala
new file mode 100644
index 0000000..1319f21
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/Indenter.scala
@@ -0,0 +1,54 @@
+/*
+ * 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.api.table.codegen
+
+class IndentStringContext(sc: StringContext) {
+ def j(args: Any*):String = {
+ val sb = new StringBuilder()
+ for ((s, a) <- sc.parts zip args) {
+ sb append s
+
+ val ind = getindent(s)
+ if (ind.size > 0) {
+ sb append a.toString().replaceAll("\n", "\n" + ind)
+ } else {
+ sb append a.toString()
+ }
+ }
+ if (sc.parts.size > args.size) {
+ sb append sc.parts.last
+ }
+
+ sb.toString()
+ }
+
+ // get white indent after the last new line, if any
+ def getindent(str: String): String = {
+ val lastnl = str.lastIndexOf("\n")
+ if (lastnl == -1) ""
+ else {
+ val ind = str.substring(lastnl + 1)
+ if (ind.trim.isEmpty) ind // ind is all whitespace. Use this
+ else ""
+ }
+ }
+}
+
+object Indenter {
+ implicit def toISC(sc: StringContext) = new IndentStringContext(sc)
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/package.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/package.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/package.scala
new file mode 100644
index 0000000..b69ac1c
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/package.scala
@@ -0,0 +1,25 @@
+/*
+ * 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.api.table
+
+package object codegen {
+ // Used in ExpressionCodeGenerator because Scala 2.10 reflection is not thread safe. We might
+ // have several parallel expression operators in one TaskManager, therefore we need to guard
+ // these operations.
+ object ReflectionLock
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/Expression.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/Expression.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/Expression.scala
new file mode 100644
index 0000000..900ed8a
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/Expression.scala
@@ -0,0 +1,60 @@
+/*
+ * 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.api.table.expressions
+
+import java.util.concurrent.atomic.AtomicInteger
+
+import scala.language.postfixOps
+
+import org.apache.flink.api.common.typeinfo.{NothingTypeInfo, TypeInformation}
+import org.apache.flink.api.table.trees.TreeNode
+
+
+abstract class Expression extends TreeNode[Expression] { self: Product =>
+ def name: String = Expression.freshName("expression")
+ def typeInfo: TypeInformation[_]
+}
+
+abstract class BinaryExpression() extends Expression { self: Product =>
+ def left: Expression
+ def right: Expression
+ def children = Seq(left, right)
+}
+
+abstract class UnaryExpression() extends Expression { self: Product =>
+ def child: Expression
+ def children = Seq(child)
+}
+
+abstract class LeafExpression() extends Expression { self: Product =>
+ val children = Nil
+}
+
+case class NopExpression() extends LeafExpression {
+ val typeInfo = new NothingTypeInfo()
+ override val name = Expression.freshName("nop")
+
+}
+
+object Expression {
+ def freshName(prefix: String): String = {
+ s"$prefix-${freshNameCounter.getAndIncrement}"
+ }
+
+ val freshNameCounter = new AtomicInteger
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/aggregations.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/aggregations.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/aggregations.scala
new file mode 100644
index 0000000..08e319d
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/aggregations.scala
@@ -0,0 +1,99 @@
+/*
+ * 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.api.table.expressions
+
+import org.apache.flink.api.table.ExpressionException
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo
+import org.apache.flink.api.java.aggregation.Aggregations
+
+
+abstract sealed class Aggregation extends UnaryExpression { self: Product =>
+ def typeInfo = {
+ child.typeInfo match {
+ case BasicTypeInfo.LONG_TYPE_INFO => // ok
+ case BasicTypeInfo.INT_TYPE_INFO =>
+ case BasicTypeInfo.DOUBLE_TYPE_INFO =>
+ case BasicTypeInfo.FLOAT_TYPE_INFO =>
+ case BasicTypeInfo.BYTE_TYPE_INFO =>
+ case BasicTypeInfo.SHORT_TYPE_INFO =>
+ case _ =>
+ throw new ExpressionException(s"Unsupported type ${child.typeInfo} for " +
+ s"aggregation $this. Only numeric data types supported.")
+ }
+ child.typeInfo
+ }
+
+ override def toString = s"Aggregate($child)"
+
+ def getIntermediateFields: Seq[Expression]
+ def getFinalField(inputs: Seq[Expression]): Expression
+ def getAggregations: Seq[Aggregations]
+}
+
+case class Sum(child: Expression) extends Aggregation {
+ override def toString = s"($child).sum"
+
+ override def getIntermediateFields: Seq[Expression] = Seq(child)
+ override def getFinalField(inputs: Seq[Expression]): Expression = inputs(0)
+ override def getAggregations = Seq(Aggregations.SUM)
+}
+
+case class Min(child: Expression) extends Aggregation {
+ override def toString = s"($child).min"
+
+ override def getIntermediateFields: Seq[Expression] = Seq(child)
+ override def getFinalField(inputs: Seq[Expression]): Expression = inputs(0)
+ override def getAggregations = Seq(Aggregations.MIN)
+
+}
+
+case class Max(child: Expression) extends Aggregation {
+ override def toString = s"($child).max"
+
+ override def getIntermediateFields: Seq[Expression] = Seq(child)
+ override def getFinalField(inputs: Seq[Expression]): Expression = inputs(0)
+ override def getAggregations = Seq(Aggregations.MAX)
+}
+
+case class Count(child: Expression) extends Aggregation {
+ override def typeInfo = {
+ child.typeInfo match {
+ case _ => // we can count anything... :D
+ }
+ BasicTypeInfo.INT_TYPE_INFO
+ }
+
+ override def toString = s"($child).count"
+
+ override def getIntermediateFields: Seq[Expression] = Seq(Literal(Integer.valueOf(1)))
+ override def getFinalField(inputs: Seq[Expression]): Expression = inputs(0)
+ override def getAggregations = Seq(Aggregations.SUM)
+
+}
+
+case class Avg(child: Expression) extends Aggregation {
+ override def toString = s"($child).avg"
+
+ override def getIntermediateFields: Seq[Expression] = Seq(child, Literal(1))
+ // This is just sweet. Use our own AST representation and let the code generator do
+ // our dirty work.
+ override def getFinalField(inputs: Seq[Expression]): Expression =
+ Div(inputs(0), inputs(1))
+ override def getAggregations = Seq(Aggregations.SUM, Aggregations.SUM)
+
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ExtractEquiJoinFields.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ExtractEquiJoinFields.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ExtractEquiJoinFields.scala
new file mode 100644
index 0000000..797de55
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ExtractEquiJoinFields.scala
@@ -0,0 +1,70 @@
+/*
+ * 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.api.table.expressions.analysis
+
+import org.apache.flink.api.table.expressions._
+import org.apache.flink.api.common.typeutils.CompositeType
+
+import scala.collection.mutable
+
+/**
+ * Equi-join field extractor for Join Predicates and CoGroup predicates. The result is a modified
+ * expression without the equi-join predicates together with indices of the join fields
+ * from both the left and right input.
+ */
+object ExtractEquiJoinFields {
+ def apply(leftType: CompositeType[_], rightType: CompositeType[_], predicate: Expression) = {
+
+ val joinFieldsLeft = mutable.MutableList[Int]()
+ val joinFieldsRight = mutable.MutableList[Int]()
+
+ val equiJoinExprs = mutable.MutableList[EqualTo]()
+ // First get all `===` expressions that are not below an `Or`
+ predicate.transformPre {
+ case or@Or(_, _) => NopExpression()
+ case eq@EqualTo(le: ResolvedFieldReference, re: ResolvedFieldReference) =>
+ if (leftType.hasField(le.name) && rightType.hasField(re.name)) {
+ joinFieldsLeft += leftType.getFieldIndex(le.name)
+ joinFieldsRight += rightType.getFieldIndex(re.name)
+ } else if (leftType.hasField(re.name) && rightType.hasField(le.name)) {
+ joinFieldsLeft += leftType.getFieldIndex(re.name)
+ joinFieldsRight += rightType.getFieldIndex(le.name)
+ } else {
+ // not an equi-join predicate
+ }
+ equiJoinExprs += eq
+ eq
+ }
+
+ // then remove the equi join expressions from the predicate
+ val resultExpr = predicate.transformPost {
+ // For OR, we can eliminate the OR since the equi join
+ // predicate is evaluated before the expression is evaluated
+ case or@Or(NopExpression(), _) => NopExpression()
+ case or@Or(_, NopExpression()) => NopExpression()
+ // For AND we replace it with the other expression, since the
+ // equi join predicate will always be true
+ case and@And(NopExpression(), other) => other
+ case and@And(other, NopExpression()) => other
+ case eq : EqualTo if equiJoinExprs.contains(eq) =>
+ NopExpression()
+ }
+
+ (resultExpr, joinFieldsLeft.toArray, joinFieldsRight.toArray)
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/GroupByAnalyzer.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/GroupByAnalyzer.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/GroupByAnalyzer.scala
new file mode 100644
index 0000000..6c7ecb2
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/GroupByAnalyzer.scala
@@ -0,0 +1,51 @@
+/*
+ * 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.api.table.expressions.analysis
+
+import org.apache.flink.api.table._
+import org.apache.flink.api.table.expressions.{ResolvedFieldReference, Expression}
+import org.apache.flink.api.common.typeinfo.TypeInformation
+
+import scala.collection.mutable
+
+import org.apache.flink.api.table.trees.{Rule, Analyzer}
+
+
+/**
+ * Analyzer for grouping expressions. Only field expressions are allowed as grouping expressions.
+ */
+class GroupByAnalyzer(inputFields: Seq[(String, TypeInformation[_])])
+ extends Analyzer[Expression] {
+
+ def rules = Seq(new ResolveFieldReferences(inputFields), CheckGroupExpression)
+
+ object CheckGroupExpression extends Rule[Expression] {
+
+ def apply(expr: Expression) = {
+ val errors = mutable.MutableList[String]()
+
+ expr match {
+ case f: ResolvedFieldReference => // this is OK
+ case other =>
+ throw new ExpressionException(
+ s"""Invalid grouping expression "$expr". Only field references are allowed.""")
+ }
+ expr
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/InsertAutoCasts.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/InsertAutoCasts.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/InsertAutoCasts.scala
new file mode 100644
index 0000000..0fdcab6
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/InsertAutoCasts.scala
@@ -0,0 +1,92 @@
+/*
+ * 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.api.table.expressions.analysis
+
+import org.apache.flink.api.table.expressions._
+import org.apache.flink.api.common.typeinfo.{IntegerTypeInfo, BasicTypeInfo}
+import org.apache.flink.api.table.trees.Rule
+
+/**
+ * [[Rule]] that adds casts in arithmetic operations.
+ */
+class InsertAutoCasts extends Rule[Expression] {
+
+ def apply(expr: Expression) = {
+ val result = expr.transformPost {
+
+ case plus@Plus(o1, o2) =>
+ // Plus is special case since we can cast anything to String for String concat
+ if (o1.typeInfo != o2.typeInfo && o1.typeInfo.isBasicType && o2.typeInfo.isBasicType) {
+ if (o1.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o2.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ Plus(Cast(o1, o2.typeInfo), o2)
+ } else if (o2.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o1.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ Plus(o1, Cast(o2, o1.typeInfo))
+ } else if (o1.typeInfo == BasicTypeInfo.STRING_TYPE_INFO) {
+ Plus(o1, Cast(o2, BasicTypeInfo.STRING_TYPE_INFO))
+ } else if (o2.typeInfo == BasicTypeInfo.STRING_TYPE_INFO) {
+ Plus(Cast(o1, BasicTypeInfo.STRING_TYPE_INFO), o2)
+ } else {
+ plus
+ }
+ } else {
+ plus
+ }
+
+ case ba: BinaryExpression if ba.isInstanceOf[BinaryArithmetic] ||
+ ba.isInstanceOf[BinaryComparison] =>
+ val o1 = ba.left
+ val o2 = ba.right
+ if (o1.typeInfo != o2.typeInfo && o1.typeInfo.isBasicType && o2.typeInfo.isBasicType) {
+ if (o1.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o2.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ ba.makeCopy(Seq(Cast(o1, o2.typeInfo), o2))
+ } else if (o2.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o1.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ ba.makeCopy(Seq(o1, Cast(o2, o1.typeInfo)))
+ } else {
+ ba
+ }
+ } else {
+ ba
+ }
+
+ case ba: BinaryExpression if ba.isInstanceOf[BitwiseBinaryArithmetic] =>
+ val o1 = ba.left
+ val o2 = ba.right
+ if (o1.typeInfo != o2.typeInfo && o1.typeInfo.isInstanceOf[IntegerTypeInfo[_]] &&
+ o2.typeInfo.isInstanceOf[IntegerTypeInfo[_]]) {
+ if (o1.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o2.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ ba.makeCopy(Seq(Cast(o1, o2.typeInfo), o2))
+ } else if (o2.typeInfo.asInstanceOf[BasicTypeInfo[_]].shouldAutocastTo(
+ o1.typeInfo.asInstanceOf[BasicTypeInfo[_]])) {
+ ba.makeCopy(Seq(o1, Cast(o2, o1.typeInfo)))
+ } else {
+ ba
+ }
+ } else {
+ ba
+ }
+ }
+
+ result
+ }
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/PredicateAnalyzer.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/PredicateAnalyzer.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/PredicateAnalyzer.scala
new file mode 100644
index 0000000..e9236f7
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/PredicateAnalyzer.scala
@@ -0,0 +1,35 @@
+/*
+ * 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.api.table.expressions.analysis
+
+import org.apache.flink.api.common.typeinfo.TypeInformation
+import org.apache.flink.api.table.expressions.Expression
+import org.apache.flink.api.table.trees.Analyzer
+
+/**
+ * Analyzer for predicates, i.e. filter operations and where clauses of joins.
+ */
+class PredicateAnalyzer(inputFields: Seq[(String, TypeInformation[_])])
+ extends Analyzer[Expression] {
+ def rules = Seq(
+ new ResolveFieldReferences(inputFields),
+ new InsertAutoCasts,
+ new TypeCheck,
+ new VerifyNoAggregates,
+ new VerifyBoolean)
+}
http://git-wip-us.apache.org/repos/asf/flink/blob/e9bf13d8/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ResolveFieldReferences.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ResolveFieldReferences.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ResolveFieldReferences.scala
new file mode 100644
index 0000000..db7ea6c
--- /dev/null
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/analysis/ResolveFieldReferences.scala
@@ -0,0 +1,60 @@
+/*
+ * 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.api.table.expressions.analysis
+
+import org.apache.flink.api.table.expressions.{ResolvedFieldReference,
+UnresolvedFieldReference, Expression}
+import org.apache.flink.api.common.typeinfo.TypeInformation
+import org.apache.flink.api.table._
+
+import scala.collection.mutable
+
+import org.apache.flink.api.table.trees.Rule
+
+/**
+ * Rule that resolved field references. This rule verifies that field references point to existing
+ * fields of the input operation and creates [[ResolvedFieldReference]]s that hold the field
+ * [[TypeInformation]] in addition to the field name.
+ */
+class ResolveFieldReferences(inputFields: Seq[(String, TypeInformation[_])])
+ extends Rule[Expression] {
+
+ def apply(expr: Expression) = {
+ val errors = mutable.MutableList[String]()
+
+ val result = expr.transformPost {
+ case fe@UnresolvedFieldReference(fieldName) =>
+ inputFields.find { _._1 == fieldName } match {
+ case Some((_, tpe)) => ResolvedFieldReference(fieldName, tpe)
+
+ case None =>
+ errors +=
+ s"Field '$fieldName' is not valid for input fields ${inputFields.mkString(",")}"
+ fe
+ }
+ }
+
+ if (errors.length > 0) {
+ throw new ExpressionException(
+ s"""Invalid expression "$expr": ${errors.mkString(" ")}""")
+ }
+
+ result
+
+ }
+}