twalthr commented on a change in pull request #8764: [FLINK-12798][table-planner][table-api-java] Port TableEnvironment to table-api-java module URL: https://github.com/apache/flink/pull/8764#discussion_r296155164
########## File path: flink-table/flink-table-api-scala-bridge/src/main/scala/org/apache/flink/table/api/scala/internal/StreamTableEnvironmentImpl.scala ########## @@ -0,0 +1,300 @@ +/* + * 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.api.scala.internal + +import java.util +import java.util.{Collections, List => JList} + +import org.apache.flink.annotation.Internal +import org.apache.flink.api.common.typeinfo.TypeInformation +import org.apache.flink.api.scala._ +import org.apache.flink.streaming.api.TimeCharacteristic +import org.apache.flink.streaming.api.datastream.{DataStream => JDataStream} +import org.apache.flink.streaming.api.environment.{StreamExecutionEnvironment => JStreamExecutionEnvironment} +import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment} +import org.apache.flink.streaming.api.transformations.StreamTransformation +import org.apache.flink.table.api._ +import org.apache.flink.table.api.internal.{PlannerFactory, TableEnvironmentImpl} +import org.apache.flink.table.api.scala.StreamTableEnvironment +import org.apache.flink.table.catalog.{CatalogManager, FunctionCatalog, GenericInMemoryCatalog} +import org.apache.flink.table.descriptors.{ConnectorDescriptor, StreamTableDescriptor} +import org.apache.flink.table.executor.Executor +import org.apache.flink.table.expressions.Expression +import org.apache.flink.table.functions.{AggregateFunction, TableAggregateFunction, TableFunction, UserFunctionsTypeHelper} +import org.apache.flink.table.operations.OutputConversionModifyOperation +import org.apache.flink.table.operations.scala.DataStreamQueryOperation +import org.apache.flink.table.planner.Planner +import org.apache.flink.table.sources.{TableSource, TableSourceValidation} +import org.apache.flink.table.types.utils.TypeConversions +import org.apache.flink.table.typeutils.FieldInfoUtils + +import _root_.scala.collection.JavaConverters._ + +/** + * The implementation for a Scala [[StreamTableEnvironment]]. This enables conversions from/to + * [[DataStream]]. It is bound to a given [[StreamExecutionEnvironment]]. + */ +@Internal +class StreamTableEnvironmentImpl ( + catalogManager: CatalogManager, + functionCatalog: FunctionCatalog, + config: TableConfig, + scalaExecutionEnvironment: StreamExecutionEnvironment, + planner: Planner, + executor: Executor) + extends TableEnvironmentImpl( + catalogManager, + config, + executor, + functionCatalog, + planner) + with org.apache.flink.table.api.scala.StreamTableEnvironment { + + override def fromDataStream[T](dataStream: DataStream[T]): Table = { + val queryOperation = asQueryOperation(dataStream, None) + createTable(queryOperation) + } + + override def fromDataStream[T](dataStream: DataStream[T], fields: Expression*): Table = { + val queryOperation = asQueryOperation(dataStream, Some(fields.toList.asJava)) + createTable(queryOperation) + } + + override def registerDataStream[T](name: String, dataStream: DataStream[T]): Unit = { + registerTable(name, fromDataStream(dataStream)) + } + + override def registerDataStream[T]( + name: String, + dataStream: DataStream[T], + fields: Expression*) + : Unit = { + registerTable(name, fromDataStream(dataStream, fields: _*)) + } + + override def toAppendStream[T: TypeInformation](table: Table): DataStream[T] = { + toAppendStream(table, new StreamQueryConfig) + } + + override def toAppendStream[T: TypeInformation]( + table: Table, + queryConfig: StreamQueryConfig) + : DataStream[T] = { + val returnType = createTypeInformation[T] + + val modifyOperation = new OutputConversionModifyOperation( + table.getQueryOperation, + TypeConversions.fromLegacyInfoToDataType(returnType), + OutputConversionModifyOperation.UpdateMode.APPEND) + queryConfigProvider.setConfig(queryConfig) + toDataStream(table, modifyOperation) + } + + override def toRetractStream[T: TypeInformation](table: Table): DataStream[(Boolean, T)] = { + toRetractStream(table, new StreamQueryConfig) + } + + override def toRetractStream[T: TypeInformation]( + table: Table, + queryConfig: StreamQueryConfig) + : DataStream[(Boolean, T)] = { + val returnType = createTypeInformation[(Boolean, T)] + + val modifyOperation = new OutputConversionModifyOperation( + table.getQueryOperation, + TypeConversions.fromLegacyInfoToDataType(returnType), + OutputConversionModifyOperation.UpdateMode.RETRACT) + + queryConfigProvider.setConfig(queryConfig) + toDataStream(table, modifyOperation) + } + + override def registerFunction[T: TypeInformation](name: String, tf: TableFunction[T]): Unit = { + val typeInfo = UserFunctionsTypeHelper + .getReturnTypeOfTableFunction(tf, implicitly[TypeInformation[T]]) + functionCatalog.registerTableFunction( + name, + tf, + typeInfo + ) + } + + override def registerFunction[T: TypeInformation, ACC: TypeInformation]( + name: String, + f: AggregateFunction[T, ACC]) + : Unit = { + val typeInfo = UserFunctionsTypeHelper + .getReturnTypeOfAggregateFunction(f, implicitly[TypeInformation[T]]) + val accTypeInfo = UserFunctionsTypeHelper + .getAccumulatorTypeOfAggregateFunction(f, implicitly[TypeInformation[ACC]]) + functionCatalog.registerAggregateFunction( + name, + f, + typeInfo, + accTypeInfo + ) + } + + override def registerFunction[T: TypeInformation, ACC: TypeInformation]( + name: String, + f: TableAggregateFunction[T, ACC]) + : Unit = { + val typeInfo = UserFunctionsTypeHelper + .getReturnTypeOfAggregateFunction(f, implicitly[TypeInformation[T]]) + val accTypeInfo = UserFunctionsTypeHelper + .getAccumulatorTypeOfAggregateFunction(f, implicitly[TypeInformation[ACC]]) + functionCatalog.registerAggregateFunction( + name, + f, + typeInfo, + accTypeInfo + ) + } + + override def connect(connectorDescriptor: ConnectorDescriptor): StreamTableDescriptor = super + .connect(connectorDescriptor).asInstanceOf[StreamTableDescriptor] + + + override protected def validateTableSource(tableSource: TableSource[_]): Unit = { + super.validateTableSource(tableSource) + // check that event-time is enabled if table source includes rowtime attributes + if (TableSourceValidation.hasRowtimeAttribute(tableSource) && + scalaExecutionEnvironment.getStreamTimeCharacteristic != TimeCharacteristic.EventTime) { + throw new TableException(String.format( Review comment: same comments as above ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
