use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class RankLikeAggFunctionBase method orderKeyEqualsExpression.
protected Expression orderKeyEqualsExpression() {
Expression[] orderKeyEquals = new Expression[orderKeyTypes.length];
for (int i = 0; i < orderKeyTypes.length; ++i) {
// pseudo code:
// if (lastValue_i is null) {
// if (operand(i) is null) true else false
// } else {
// lastValue_i equalTo orderKey(i)
// }
Expression lasValue = lastValues[i];
orderKeyEquals[i] = ifThenElse(isNull(lasValue), ifThenElse(isNull(operand(i)), literal(true), literal(false)), equalTo(lasValue, operand(i)));
}
Optional<Expression> ret = Arrays.stream(orderKeyEquals).reduce(ExpressionBuilder::and);
return ret.orElseGet(() -> literal(true));
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class OverConvertRule method convert.
@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
List<Expression> children = call.getChildren();
if (call.getFunctionDefinition() == BuiltInFunctionDefinitions.OVER) {
FlinkTypeFactory typeFactory = context.getTypeFactory();
Expression agg = children.get(0);
FunctionDefinition def = ((CallExpression) agg).getFunctionDefinition();
boolean isDistinct = BuiltInFunctionDefinitions.DISTINCT == def;
SqlAggFunction aggFunc = agg.accept(new SqlAggFunctionVisitor(context.getRelBuilder()));
RelDataType aggResultType = typeFactory.createFieldTypeFromLogicalType(fromDataTypeToLogicalType(((ResolvedExpression) agg).getOutputDataType()));
// assemble exprs by agg children
List<RexNode> aggExprs = agg.getChildren().stream().map(child -> {
if (isDistinct) {
return context.toRexNode(child.getChildren().get(0));
} else {
return context.toRexNode(child);
}
}).collect(Collectors.toList());
// assemble order by key
Expression orderKeyExpr = children.get(1);
Set<SqlKind> kinds = new HashSet<>();
RexNode collationRexNode = createCollation(context.toRexNode(orderKeyExpr), RelFieldCollation.Direction.ASCENDING, null, kinds);
ImmutableList<RexFieldCollation> orderKey = ImmutableList.of(new RexFieldCollation(collationRexNode, kinds));
// assemble partition by keys
List<RexNode> partitionKeys = children.subList(4, children.size()).stream().map(context::toRexNode).collect(Collectors.toList());
// assemble bounds
Expression preceding = children.get(2);
boolean isPhysical = fromDataTypeToLogicalType(((ResolvedExpression) preceding).getOutputDataType()).is(LogicalTypeRoot.BIGINT);
Expression following = children.get(3);
RexWindowBound lowerBound = createBound(context, preceding, SqlKind.PRECEDING);
RexWindowBound upperBound = createBound(context, following, SqlKind.FOLLOWING);
// build RexOver
return Optional.of(context.getRelBuilder().getRexBuilder().makeOver(aggResultType, aggFunc, aggExprs, partitionKeys, orderKey, lowerBound, upperBound, isPhysical, true, false, isDistinct));
}
return Optional.empty();
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class InConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgument(call, call.getChildren().size() > 1);
Expression headExpr = call.getChildren().get(1);
if (headExpr instanceof TableReferenceExpression) {
QueryOperation tableOperation = ((TableReferenceExpression) headExpr).getQueryOperation();
RexNode child = context.toRexNode(call.getChildren().get(0));
return RexSubQuery.in(((FlinkRelBuilder) context.getRelBuilder()).queryOperation(tableOperation).build(), ImmutableList.of(child));
} else {
List<RexNode> child = toRexNodes(context, call.getChildren());
return context.getRelBuilder().getRexBuilder().makeIn(child.get(0), child.subList(1, child.size()));
}
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class JsonObjectConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgument(call, (call.getChildren().size() - 1) % 2 == 0);
final List<RexNode> operands = new LinkedList<>();
final SqlJsonConstructorNullClause onNull = JsonConverterUtil.getOnNullArgument(call, 0);
operands.add(context.getRelBuilder().getRexBuilder().makeFlag(onNull));
for (int i = 1; i < call.getChildren().size(); i++) {
final Expression operand = call.getChildren().get(i);
if (i % 2 == 1 && !(operand instanceof ValueLiteralExpression)) {
throw new TableException(String.format("Argument at position %s must be a string literal.", i));
}
operands.add(context.toRexNode(operand));
}
return context.getRelBuilder().getRexBuilder().makeCall(FlinkSqlOperatorTable.JSON_OBJECT, operands);
}
use of org.apache.flink.table.expressions.Expression in project flink by apache.
the class FilterUtils method isRetainedAfterApplyingFilterPredicates.
public static boolean isRetainedAfterApplyingFilterPredicates(List<ResolvedExpression> predicates, Function<String, Comparable<?>> getter) {
for (ResolvedExpression predicate : predicates) {
if (predicate instanceof CallExpression) {
FunctionDefinition definition = ((CallExpression) predicate).getFunctionDefinition();
boolean result = false;
if (definition.equals(BuiltInFunctionDefinitions.OR)) {
// nested filter, such as (key1 > 2 or key2 > 3)
for (Expression expr : predicate.getChildren()) {
if (!(expr instanceof CallExpression && expr.getChildren().size() == 2)) {
throw new TableException(expr + " not supported!");
}
result = binaryFilterApplies((CallExpression) expr, getter);
if (result) {
break;
}
}
} else if (predicate.getChildren().size() == 2) {
result = binaryFilterApplies((CallExpression) predicate, getter);
} else {
throw new UnsupportedOperationException(String.format("Unsupported expr: %s.", predicate));
}
if (!result) {
return false;
}
} else {
throw new UnsupportedOperationException(String.format("Unsupported expr: %s.", predicate));
}
}
return true;
}
Aggregations