Search in sources :

Example 51 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink-mirror by flink-ci.

the class SqlValidatorImpl method handleUnresolvedFunction.

public CalciteException handleUnresolvedFunction(SqlCall call, SqlFunction unresolvedFunction, List<RelDataType> argTypes, List<String> argNames) {
    // For builtins, we can give a better error message
    final List<SqlOperator> overloads = new ArrayList<>();
    opTab.lookupOperatorOverloads(unresolvedFunction.getNameAsId(), null, SqlSyntax.FUNCTION, overloads, catalogReader.nameMatcher());
    if (overloads.size() == 1) {
        SqlFunction fun = (SqlFunction) overloads.get(0);
        if ((fun.getSqlIdentifier() == null) && (fun.getSyntax() != SqlSyntax.FUNCTION_ID)) {
            final int expectedArgCount = fun.getOperandCountRange().getMin();
            throw newValidationError(call, RESOURCE.invalidArgCount(call.getOperator().getName(), expectedArgCount));
        }
    }
    AssignableOperandTypeChecker typeChecking = new AssignableOperandTypeChecker(argTypes, argNames);
    String signature = typeChecking.getAllowedSignatures(unresolvedFunction, unresolvedFunction.getName());
    throw newValidationError(call, RESOURCE.validatorUnknownFunction(signature));
}
Also used : AssignableOperandTypeChecker(org.apache.calcite.sql.type.AssignableOperandTypeChecker) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) BitString(org.apache.calcite.util.BitString) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 52 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project druid by apache.

the class ExpressionsTest method testRoundWithNanShouldRoundTo0.

@Test
public void testRoundWithNanShouldRoundTo0() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("nan"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "nan"))), 0D);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("fnan"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "fnan"))), 0D);
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 53 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project druid by apache.

the class ExpressionsTest method testRoundWithInfinityShouldRoundTo0.

@Test
public void testRoundWithInfinityShouldRoundTo0() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    // CHECKSTYLE.OFF: Regexp
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "inf"))), Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("-inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "-inf"))), -1 * Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "finf"))), Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("-finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "-finf"))), -1 * Double.MAX_VALUE);
// CHECKSTYLE.ON: Regexp
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 54 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project druid by apache.

the class ExpressionsTest method testRoundWithInvalidArgument.

@Test
public void testRoundWithInvalidArgument() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    if (!NullHandling.sqlCompatible()) {
        expectException(IAE.class, "The first argument to the function[round] should be integer or double type but got the type: STRING");
    }
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("s"), DruidExpression.ofExpression(ColumnType.STRING, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.STRING, "s"))), NullHandling.sqlCompatible() ? null : "IAE Exception");
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 55 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project Mycat2 by MyCATApache.

the class RelJson method toJson.

private Object toJson(RexNode node) {
    final Map<String, Object> map;
    switch(node.getKind()) {
        case FIELD_ACCESS:
            map = jsonBuilder.map();
            final RexFieldAccess fieldAccess = (RexFieldAccess) node;
            map.put("field", fieldAccess.getField().getName());
            map.put("expr", toJson(fieldAccess.getReferenceExpr()));
            return map;
        case LITERAL:
            final RexLiteral literal = (RexLiteral) node;
            Object value = literal.getValue3();
            map = jsonBuilder.map();
            if (value instanceof ByteString || value instanceof Duration || value instanceof Period || value instanceof Temporal) {
                if (value instanceof Period) {
                    Period period = (Period) value;
                    switch(literal.getTypeName()) {
                        case INTERVAL_DAY:
                            value = period.getDays();
                            break;
                        case INTERVAL_WEEK:
                            value = period.getDays() / 7;
                            break;
                        case INTERVAL_MONTH:
                            value = period.getMonths();
                            break;
                        case INTERVAL_YEAR_MONTH:
                            value = period.getMonths();
                            break;
                        case INTERVAL_QUARTER:
                            value = period.getMonths() / 3;
                            break;
                        case INTERVAL_YEAR:
                            value = period.getYears();
                            break;
                        default:
                    }
                    map.put("literal", value);
                    map.put("class", value.getClass().getName());
                } else // else if (value instanceof Duration) {
                // switch (literal.getTypeName()){
                // case  INTERVAL_DAY_HOUR:
                // case INTERVAL_DAY_MINUTE:
                // case INTERVAL_DAY_SECOND:
                // case INTERVAL_HOUR:
                // case INTERVAL_HOUR_MINUTE:
                // case INTERVAL_HOUR_SECOND:
                // case INTERVAL_MINUTE:
                // case INTERVAL_MINUTE_SECOND:
                // case INTERVAL_SECOND:
                // case INTERVAL_MICROSECOND:
                // case INTERVAL_SECOND_MICROSECOND:
                // }
                // }
                {
                    map.put("literal", value.toString());
                    map.put("class", value.getClass().getName());
                }
            } else {
                map.put("literal", RelEnumTypes.fromEnum(value));
            }
            map.put("type", toJson(node.getType()));
            return map;
        case INPUT_REF:
            map = jsonBuilder.map();
            map.put("input", ((RexSlot) node).getIndex());
            map.put("name", ((RexSlot) node).getName());
            return map;
        case LOCAL_REF:
            map = jsonBuilder.map();
            map.put("input", ((RexSlot) node).getIndex());
            map.put("name", ((RexSlot) node).getName());
            map.put("type", toJson(node.getType()));
            return map;
        case CORREL_VARIABLE:
            map = jsonBuilder.map();
            map.put("correl", ((RexCorrelVariable) node).getName());
            map.put("type", toJson(node.getType()));
            return map;
        case DYNAMIC_PARAM:
            map = jsonBuilder.map();
            map.put("name", ((RexDynamicParam) node).getName());
            map.put("index", ((RexDynamicParam) node).getIndex());
            map.put("type", toJson(node.getType()));
            return map;
        default:
            if (node instanceof RexCall) {
                final RexCall call = (RexCall) node;
                map = jsonBuilder.map();
                map.put("op", toJson(call.getOperator()));
                final List<Object> list = jsonBuilder.list();
                for (RexNode operand : call.getOperands()) {
                    list.add(toJson(operand));
                }
                map.put("operands", list);
                switch(node.getKind()) {
                    case CAST:
                        map.put("type", toJson(node.getType()));
                }
                if (call.getOperator() instanceof SqlFunction) {
                    if (((SqlFunction) call.getOperator()).getFunctionType().isUserDefined()) {
                        SqlOperator op = call.getOperator();
                        map.put("class", op.getClass().getName());
                        map.put("type", toJson(node.getType()));
                        map.put("deterministic", op.isDeterministic());
                        map.put("dynamic", op.isDynamicFunction());
                    }
                }
                if (call instanceof RexOver) {
                    RexOver over = (RexOver) call;
                    map.put("distinct", over.isDistinct());
                    map.put("type", toJson(node.getType()));
                    map.put("window", toJson(over.getWindow()));
                }
                if (call instanceof Window.RexWinAggCall) {
                    Window.RexWinAggCall rexWinAggCall = (Window.RexWinAggCall) call;
                    final int ordinal = rexWinAggCall.ordinal;
                    final boolean distinct = rexWinAggCall.distinct;
                    final boolean ignoreNulls = rexWinAggCall.ignoreNulls;
                    map.put("ordinal", toJson(ordinal));
                    map.put("distinct", toJson(distinct));
                    map.put("window", toJson(ignoreNulls));
                }
                return map;
            }
            throw new UnsupportedOperationException("unknown rex " + node);
    }
}
Also used : Window(org.apache.calcite.rel.core.Window) ByteString(org.apache.calcite.avatica.util.ByteString) SqlOperator(org.apache.calcite.sql.SqlOperator) Period(java.time.Period) Duration(java.time.Duration) ByteString(org.apache.calcite.avatica.util.ByteString) Temporal(java.time.temporal.Temporal) SqlFunction(org.apache.calcite.sql.SqlFunction)

Aggregations

SqlFunction (org.apache.calcite.sql.SqlFunction)57 SqlOperator (org.apache.calcite.sql.SqlOperator)26 ArrayList (java.util.ArrayList)13 RoundOperatorConversion (org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion)12 Test (org.junit.Test)12 RexNode (org.apache.calcite.rex.RexNode)8 SqlCall (org.apache.calcite.sql.SqlCall)8 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)8 AssignableOperandTypeChecker (org.apache.calcite.sql.type.AssignableOperandTypeChecker)6 BitString (org.apache.calcite.util.BitString)6 ImmutableList (com.google.common.collect.ImmutableList)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 RexCall (org.apache.calcite.rex.RexCall)4 RexLiteral (org.apache.calcite.rex.RexLiteral)4 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)4 ScalarFunctionDefinition (org.apache.flink.table.functions.ScalarFunctionDefinition)4 BigDecimal (java.math.BigDecimal)3 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3