use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class AggregateOperationFactory method createResolvedWindow.
/**
* Converts an API class to a resolved window for planning with expressions already resolved. It
* performs following validations:
*
* <ul>
* <li>The alias is represented with an unresolved reference
* <li>The time attribute is a single field reference of a {@link
* TimeIndicatorTypeInfo}(stream), {@link SqlTimeTypeInfo}(batch), or {@link
* BasicTypeInfo#LONG_TYPE_INFO}(batch) type
* <li>The size & slide are value literals of either {@link BasicTypeInfo#LONG_TYPE_INFO}, or
* {@link TimeIntervalTypeInfo} type
* <li>The size & slide are of the same type
* <li>The gap is a value literal of a {@link TimeIntervalTypeInfo} type
* </ul>
*
* @param window window to resolve
* @param resolver resolver to resolve potential unresolved field references
* @return window with expressions resolved
*/
ResolvedGroupWindow createResolvedWindow(GroupWindow window, ExpressionResolver resolver) {
Expression alias = window.getAlias();
if (!(alias instanceof UnresolvedReferenceExpression)) {
throw new ValidationException("Only unresolved reference supported for alias of a group window.");
}
final String windowName = ((UnresolvedReferenceExpression) alias).getName();
FieldReferenceExpression timeField = getValidatedTimeAttribute(window, resolver);
if (window instanceof TumbleWithSizeOnTimeWithAlias) {
return validateAndCreateTumbleWindow((TumbleWithSizeOnTimeWithAlias) window, windowName, timeField);
} else if (window instanceof SlideWithSizeAndSlideOnTimeWithAlias) {
return validateAndCreateSlideWindow((SlideWithSizeAndSlideOnTimeWithAlias) window, windowName, timeField);
} else if (window instanceof SessionWithGapOnTimeWithAlias) {
return validateAndCreateSessionWindow((SessionWithGapOnTimeWithAlias) window, windowName, timeField);
} else {
throw new TableException("Unknown window type: " + window);
}
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class FieldInfoUtils method extractFieldInfoFromAtomicType.
private static List<FieldInfo> extractFieldInfoFromAtomicType(TypeInformation<?> atomicType, Expression[] exprs) {
List<FieldInfo> fields = new ArrayList<>(exprs.length);
boolean alreadyReferenced = false;
for (int i = 0; i < exprs.length; i++) {
Expression expr = exprs[i];
if (expr instanceof UnresolvedReferenceExpression) {
if (alreadyReferenced) {
throw new ValidationException("Too many fields referenced from an atomic type.");
}
alreadyReferenced = true;
String name = ((UnresolvedReferenceExpression) expr).getName();
fields.add(new FieldInfo(name, i, fromLegacyInfoToDataType(atomicType)));
} else if (isRowTimeExpression(expr)) {
UnresolvedReferenceExpression reference = getChildAsReference(expr);
fields.add(createTimeAttributeField(reference, TimestampKind.ROWTIME, null));
} else if (isProcTimeExpression(expr)) {
UnresolvedReferenceExpression reference = getChildAsReference(expr);
fields.add(createTimeAttributeField(reference, TimestampKind.PROCTIME, null));
} else {
throw new ValidationException("Field reference expression expected.");
}
}
return fields;
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class AliasOperationUtils method createAliasList.
/**
* Creates a list of valid alias expressions. Resulting expression might still contain {@link
* UnresolvedReferenceExpression}.
*
* @param aliases aliases to validate
* @param child relational operation on top of which to apply the aliases
* @return validated list of aliases
*/
static List<Expression> createAliasList(List<Expression> aliases, QueryOperation child) {
ResolvedSchema childSchema = child.getResolvedSchema();
if (aliases.size() > childSchema.getColumnCount()) {
throw new ValidationException("Aliasing more fields than we actually have.");
}
List<ValueLiteralExpression> fieldAliases = aliases.stream().map(f -> f.accept(aliasLiteralValidator)).collect(Collectors.toList());
List<String> childNames = childSchema.getColumnNames();
return IntStream.range(0, childNames.size()).mapToObj(idx -> {
UnresolvedReferenceExpression oldField = unresolvedRef(childNames.get(idx));
if (idx < fieldAliases.size()) {
ValueLiteralExpression alias = fieldAliases.get(idx);
return unresolvedCall(BuiltInFunctionDefinitions.AS, oldField, alias);
} else {
return oldField;
}
}).collect(Collectors.toList());
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class AvgAggFunction method getValueExpression.
/**
* If all input are nulls, count will be 0 and we will get null after the division.
*/
@Override
public Expression getValueExpression() {
Expression ifTrue = nullOf(getResultType());
Expression ifFalse = cast(div(sum, count), typeLiteral(getResultType()));
return ifThenElse(equalTo(count, literal(0L)), ifTrue, ifFalse);
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class MapConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
List<Expression> children = call.getChildren();
checkArgument(call, !children.isEmpty() && children.size() % 2 == 0);
List<RexNode> childrenRexNode = toRexNodes(context, children);
RelDataType mapType = context.getTypeFactory().createFieldTypeFromLogicalType(call.getOutputDataType().getLogicalType());
return context.getRelBuilder().getRexBuilder().makeCall(mapType, FlinkSqlOperatorTable.MAP_VALUE_CONSTRUCTOR, childrenRexNode);
}
Aggregations