use of org.apache.flink.table.expressions.ApiExpressionUtils in project flink by apache.
the class OperationTreeBuilder method flatMap.
public QueryOperation flatMap(Expression tableFunctionCall, QueryOperation child) {
final ExpressionResolver resolver = getResolverBuilder(child).build();
final ResolvedExpression resolvedCall = resolveSingleExpression(tableFunctionCall, resolver);
if (!isFunctionOfKind(resolvedCall, FunctionKind.TABLE)) {
throw new ValidationException("Only a table function can be used in the flatMap operator.");
}
final List<String> originFieldNames = DataTypeUtils.flattenToNames(resolvedCall.getOutputDataType());
List<String> childFields = child.getResolvedSchema().getColumnNames();
Set<String> usedFieldNames = new HashSet<>(childFields);
List<Expression> args = new ArrayList<>();
for (String originFieldName : originFieldNames) {
String resultName = getUniqueName(originFieldName, usedFieldNames);
usedFieldNames.add(resultName);
args.add(valueLiteral(resultName));
}
args.add(0, tableFunctionCall);
Expression renamedTableFunction = unresolvedCall(BuiltInFunctionDefinitions.AS, args.toArray(new Expression[0]));
QueryOperation joinNode = joinLateral(child, renamedTableFunction, JoinType.INNER, Optional.empty());
QueryOperation rightNode = dropColumns(childFields.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), joinNode);
return alias(originFieldNames.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), rightNode);
}
use of org.apache.flink.table.expressions.ApiExpressionUtils in project flink by apache.
the class AbstractStreamTableEnvironmentImpl method toStreamInternal.
protected <T> DataStream<T> toStreamInternal(Table table, SchemaTranslator.ProducingResult schemaTranslationResult, @Nullable ChangelogMode changelogMode) {
final CatalogManager catalogManager = getCatalogManager();
final OperationTreeBuilder operationTreeBuilder = getOperationTreeBuilder();
final QueryOperation projectOperation = schemaTranslationResult.getProjections().map(projections -> operationTreeBuilder.project(projections.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), table.getQueryOperation())).orElseGet(table::getQueryOperation);
final ResolvedCatalogTable resolvedCatalogTable = catalogManager.resolveCatalogTable(new ExternalCatalogTable(schemaTranslationResult.getSchema()));
final ExternalModifyOperation modifyOperation = new ExternalModifyOperation(ContextResolvedTable.anonymous("datastream_sink", resolvedCatalogTable), projectOperation, changelogMode, schemaTranslationResult.getPhysicalDataType().orElseGet(() -> resolvedCatalogTable.getResolvedSchema().toPhysicalRowDataType()));
return toStreamInternal(table, modifyOperation);
}
use of org.apache.flink.table.expressions.ApiExpressionUtils in project flink by apache.
the class AbstractStreamTableEnvironmentImpl method fromStreamInternal.
protected <T> Table fromStreamInternal(DataStream<T> dataStream, @Nullable Schema schema, @Nullable String viewPath, ChangelogMode changelogMode) {
Preconditions.checkNotNull(dataStream, "Data stream must not be null.");
Preconditions.checkNotNull(changelogMode, "Changelog mode must not be null.");
if (dataStream.getExecutionEnvironment() != executionEnvironment) {
throw new ValidationException("The DataStream's StreamExecutionEnvironment must be identical to the one that " + "has been passed to the StreamTableEnvironment during instantiation.");
}
final CatalogManager catalogManager = getCatalogManager();
final OperationTreeBuilder operationTreeBuilder = getOperationTreeBuilder();
final SchemaTranslator.ConsumingResult schemaTranslationResult = SchemaTranslator.createConsumingResult(catalogManager.getDataTypeFactory(), dataStream.getType(), schema);
final ResolvedCatalogTable resolvedCatalogTable = catalogManager.resolveCatalogTable(new ExternalCatalogTable(schemaTranslationResult.getSchema()));
final ContextResolvedTable contextResolvedTable;
if (viewPath != null) {
UnresolvedIdentifier unresolvedIdentifier = getParser().parseIdentifier(viewPath);
final ObjectIdentifier objectIdentifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
contextResolvedTable = ContextResolvedTable.temporary(objectIdentifier, resolvedCatalogTable);
} else {
contextResolvedTable = ContextResolvedTable.anonymous("datastream_source", resolvedCatalogTable);
}
final QueryOperation scanOperation = new ExternalQueryOperation<>(contextResolvedTable, dataStream, schemaTranslationResult.getPhysicalDataType(), schemaTranslationResult.isTopLevelRecord(), changelogMode);
final List<String> projections = schemaTranslationResult.getProjections();
if (projections == null) {
return createTable(scanOperation);
}
final QueryOperation projectOperation = operationTreeBuilder.project(projections.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), scanOperation);
return createTable(projectOperation);
}
Aggregations