Search in sources :

Example 21 with SqlFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project druid by druid-io.

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 22 with SqlFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project druid by druid-io.

the class ExpressionsTest method testRound.

@Test
public void testRound() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("a"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "a"))), 10L);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("b"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "b"))), 25L);
    testHelper.testExpressionString(roundFunction, ImmutableList.of(testHelper.makeInputRef("b"), testHelper.makeLiteral(-1)), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "b"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(-1)))), 30L);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("x"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"))), 2.0);
    testHelper.testExpressionString(roundFunction, ImmutableList.of(testHelper.makeInputRef("x"), testHelper.makeLiteral(1)), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(1)))), 2.3);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("y"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "y"))), 3.0);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("z"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "z"))), -2.0);
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 23 with SqlFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project flink by apache.

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 24 with SqlFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project flink by apache.

the class FunctionCatalogOperatorTable method convertToBridgingSqlFunction.

private Optional<SqlFunction> convertToBridgingSqlFunction(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
    final FunctionDefinition definition = resolvedFunction.getDefinition();
    if (!verifyFunctionKind(category, resolvedFunction)) {
        return Optional.empty();
    }
    final TypeInference typeInference;
    try {
        typeInference = definition.getTypeInference(dataTypeFactory);
    } catch (Throwable t) {
        throw new ValidationException(String.format("An error occurred in the type inference logic of function '%s'.", resolvedFunction), t);
    }
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    final SqlFunction function;
    if (definition.getKind() == FunctionKind.AGGREGATE || definition.getKind() == FunctionKind.TABLE_AGGREGATE) {
        function = BridgingSqlAggFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    } else {
        function = BridgingSqlFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    }
    return Optional.of(function);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) ValidationException(org.apache.flink.table.api.ValidationException) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) SqlFunction(org.apache.calcite.sql.SqlFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) HiveTableSqlFunction(org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)

Example 25 with SqlFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project flink by apache.

the class SqlValidatorImpl method deriveConstructorType.

public RelDataType deriveConstructorType(SqlValidatorScope scope, SqlCall call, SqlFunction unresolvedConstructor, SqlFunction resolvedConstructor, List<RelDataType> argTypes) {
    SqlIdentifier sqlIdentifier = unresolvedConstructor.getSqlIdentifier();
    assert sqlIdentifier != null;
    RelDataType type = catalogReader.getNamedType(sqlIdentifier);
    if (type == null) {
        // TODO jvs 12-Feb-2005:  proper type name formatting
        throw newValidationError(sqlIdentifier, RESOURCE.unknownDatatypeName(sqlIdentifier.toString()));
    }
    if (resolvedConstructor == null) {
        if (call.operandCount() > 0) {
            // no user-defined constructor could be found
            throw handleUnresolvedFunction(call, unresolvedConstructor, argTypes, null);
        }
    } else {
        SqlCall testCall = resolvedConstructor.createCall(call.getParserPosition(), call.getOperandList());
        RelDataType returnType = resolvedConstructor.validateOperands(this, scope, testCall);
        assert type == returnType;
    }
    if (config.identifierExpansion()) {
        if (resolvedConstructor != null) {
            ((SqlBasicCall) call).setOperator(resolvedConstructor);
        } else {
            // fake a fully-qualified call to the default constructor
            ((SqlBasicCall) call).setOperator(new SqlFunction(type.getSqlIdentifier(), ReturnTypes.explicit(type), null, null, null, SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR));
        }
    }
    return type;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction)

Aggregations

SqlFunction (org.apache.calcite.sql.SqlFunction)30 SqlOperator (org.apache.calcite.sql.SqlOperator)13 ArrayList (java.util.ArrayList)7 RexNode (org.apache.calcite.rex.RexNode)6 RoundOperatorConversion (org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion)6 Test (org.junit.Test)6 SqlCall (org.apache.calcite.sql.SqlCall)5 ImmutableList (com.google.common.collect.ImmutableList)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 BigDecimal (java.math.BigDecimal)3 RexCall (org.apache.calcite.rex.RexCall)3 RexLiteral (org.apache.calcite.rex.RexLiteral)3 SqlNode (org.apache.calcite.sql.SqlNode)3 List (java.util.List)2 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)2 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)2 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)2