use of org.apache.flink.table.expressions.resolver.ExpressionResolver in project flink by apache.
the class OperationTreeBuilder method renameColumns.
public QueryOperation renameColumns(List<Expression> aliases, QueryOperation child) {
ExpressionResolver resolver = getResolver(child);
final List<String> inputFieldNames = child.getResolvedSchema().getColumnNames();
List<Expression> validateAliases = ColumnOperationUtils.renameColumns(inputFieldNames, resolver.resolveExpanding(aliases));
return project(validateAliases, child, false);
}
use of org.apache.flink.table.expressions.resolver.ExpressionResolver in project flink by apache.
the class OperationTreeBuilder method aggregate.
public QueryOperation aggregate(List<Expression> groupingExpressions, Expression aggregate, QueryOperation child) {
Expression resolvedAggregate = aggregate.accept(lookupResolver);
ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
AggregateWithAlias aggregateWithAlias = resolvedAggregate.accept(new ExtractAliasAndAggregate(true, resolver));
List<Expression> groupsAndAggregate = new ArrayList<>(groupingExpressions);
groupsAndAggregate.add(aggregateWithAlias.aggregate);
List<Expression> namedGroupsAndAggregate = addAliasToTheCallInAggregate(child.getResolvedSchema().getColumnNames(), groupsAndAggregate);
// Step1: add a default name to the call in the grouping expressions, e.g., groupBy(a % 5)
// to
// groupBy(a % 5 as TMP_0). We need a name for every column so that to perform alias for the
// aggregate function in Step5.
List<Expression> newGroupingExpressions = namedGroupsAndAggregate.subList(0, groupingExpressions.size());
// Step2: turn agg to a named agg, because it will be verified later.
Expression aggregateRenamed = namedGroupsAndAggregate.get(groupingExpressions.size());
// Step3: get agg table
QueryOperation aggregateOperation = this.aggregate(newGroupingExpressions, Collections.singletonList(aggregateRenamed), child);
// Step4: flatten the aggregate function
List<String> aggNames = aggregateOperation.getResolvedSchema().getColumnNames();
List<Expression> flattenedExpressions = aggNames.subList(0, groupingExpressions.size()).stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toCollection(ArrayList::new));
flattenedExpressions.add(unresolvedCall(BuiltInFunctionDefinitions.FLATTEN, unresolvedRef(aggNames.get(aggNames.size() - 1))));
QueryOperation flattenedProjection = this.project(flattenedExpressions, aggregateOperation);
// Step5: add alias
return aliasBackwardFields(flattenedProjection, aggregateWithAlias.aliases, groupingExpressions.size());
}
use of org.apache.flink.table.expressions.resolver.ExpressionResolver in project flink by apache.
the class OperationTreeBuilder method joinLateral.
public QueryOperation joinLateral(QueryOperation left, Expression tableFunction, JoinType joinType, Optional<Expression> condition) {
ExpressionResolver resolver = getResolver(left);
ResolvedExpression resolvedFunction = resolveSingleExpression(tableFunction, resolver);
QueryOperation temporalTable = calculatedTableFactory.create(resolvedFunction, left.getResolvedSchema().getColumnNames());
return join(left, temporalTable, joinType, condition, true);
}
use of org.apache.flink.table.expressions.resolver.ExpressionResolver 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.resolver.ExpressionResolver in project flink by apache.
the class OperationTreeBuilder method windowAggregate.
public QueryOperation windowAggregate(List<Expression> groupingExpressions, GroupWindow window, List<Expression> windowProperties, Expression aggregateFunction, QueryOperation child) {
ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
Expression resolvedAggregate = aggregateFunction.accept(lookupResolver);
AggregateWithAlias aggregateWithAlias = resolvedAggregate.accept(new ExtractAliasAndAggregate(true, resolver));
List<Expression> groupsAndAggregate = new ArrayList<>(groupingExpressions);
groupsAndAggregate.add(aggregateWithAlias.aggregate);
List<Expression> namedGroupsAndAggregate = addAliasToTheCallInAggregate(child.getResolvedSchema().getColumnNames(), groupsAndAggregate);
// Step1: add a default name to the call in the grouping expressions, e.g., groupBy(a % 5)
// to
// groupBy(a % 5 as TMP_0). We need a name for every column so that to perform alias for the
// table aggregate function in Step6.
List<Expression> newGroupingExpressions = namedGroupsAndAggregate.subList(0, groupingExpressions.size());
// Step2: turn agg to a named agg, because it will be verified later.
Expression aggregateRenamed = namedGroupsAndAggregate.get(groupingExpressions.size());
// Step3: resolve expressions, including grouping, aggregates and window properties.
ResolvedGroupWindow resolvedWindow = aggregateOperationFactory.createResolvedWindow(window, resolver);
ExpressionResolver resolverWithWindowReferences = getResolverBuilder(child).withLocalReferences(localRef(resolvedWindow.getAlias(), resolvedWindow.getTimeAttribute().getOutputDataType())).build();
List<ResolvedExpression> convertedGroupings = resolverWithWindowReferences.resolve(newGroupingExpressions);
List<ResolvedExpression> convertedAggregates = resolverWithWindowReferences.resolve(Collections.singletonList(aggregateRenamed));
List<ResolvedExpression> convertedProperties = resolverWithWindowReferences.resolve(windowProperties);
// Step4: create window agg operation
QueryOperation aggregateOperation = aggregateOperationFactory.createWindowAggregate(convertedGroupings, Collections.singletonList(convertedAggregates.get(0)), convertedProperties, resolvedWindow, child);
// Step5: flatten the aggregate function
List<String> aggNames = aggregateOperation.getResolvedSchema().getColumnNames();
List<Expression> flattenedExpressions = aggNames.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toCollection(ArrayList::new));
flattenedExpressions.set(groupingExpressions.size(), unresolvedCall(BuiltInFunctionDefinitions.FLATTEN, unresolvedRef(aggNames.get(groupingExpressions.size()))));
QueryOperation flattenedProjection = this.project(flattenedExpressions, aggregateOperation);
// window attribute.
return aliasBackwardFields(flattenedProjection, aggregateWithAlias.aliases, groupingExpressions.size());
}
Aggregations