Search in sources :

Example 11 with CallExpression

use of org.apache.flink.table.expressions.CallExpression 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);
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlJsonConstructorNullClause(org.apache.calcite.sql.SqlJsonConstructorNullClause) LinkedList(java.util.LinkedList) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with CallExpression

use of org.apache.flink.table.expressions.CallExpression 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;
}
Also used : TableException(org.apache.flink.table.api.TableException) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression)

Example 13 with CallExpression

use of org.apache.flink.table.expressions.CallExpression in project flink by apache.

the class FilterUtils method binaryFilterApplies.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static boolean binaryFilterApplies(CallExpression binExpr, Function<String, Comparable<?>> getter) {
    List<Expression> children = binExpr.getChildren();
    Preconditions.checkArgument(children.size() == 2);
    Comparable lhsValue = getValue(children.get(0), getter);
    Comparable rhsValue = getValue(children.get(1), getter);
    FunctionDefinition functionDefinition = binExpr.getFunctionDefinition();
    if (BuiltInFunctionDefinitions.GREATER_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) > 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) < 0;
    } else if (BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) >= 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) <= 0;
    } else if (BuiltInFunctionDefinitions.EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) == 0;
    } else if (BuiltInFunctionDefinitions.NOT_EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) != 0;
    } else {
        throw new UnsupportedOperationException("Unsupported operator: " + functionDefinition);
    }
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition)

Aggregations

CallExpression (org.apache.flink.table.expressions.CallExpression)13 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)11 Expression (org.apache.flink.table.expressions.Expression)9 RexNode (org.apache.calcite.rex.RexNode)7 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)5 ArrayList (java.util.ArrayList)4 TableException (org.apache.flink.table.api.TableException)4 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)4 RelDataType (org.apache.calcite.rel.type.RelDataType)3 ImmutableList (com.google.common.collect.ImmutableList)2 BigDecimal (java.math.BigDecimal)2 List (java.util.List)2 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)2 SqlNode (org.apache.calcite.sql.SqlNode)2 SqlOperator (org.apache.calcite.sql.SqlOperator)2 SqlPostfixOperator (org.apache.calcite.sql.SqlPostfixOperator)2 OrdinalReturnTypeInference (org.apache.calcite.sql.type.OrdinalReturnTypeInference)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1