Search in sources :

Example 31 with SqlOperator

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

the class RexSimplify method simplifyIs2.

private RexNode simplifyIs2(SqlKind kind, RexNode a) {
    switch(kind) {
        case IS_NULL:
            // x IS NULL ==> FALSE (if x is not nullable)
            if (!a.getType().isNullable()) {
                return rexBuilder.makeLiteral(false);
            }
            break;
        case IS_NOT_NULL:
            // x IS NOT NULL ==> TRUE (if x is not nullable)
            RexNode simplified = simplifyIsNotNull(a);
            if (simplified != null) {
                return simplified;
            }
            break;
        case IS_TRUE:
        case IS_NOT_FALSE:
            // x IS NOT FALSE ==> x (if x is not nullable)
            if (!a.getType().isNullable()) {
                return simplify(a);
            }
            break;
        case IS_FALSE:
        case IS_NOT_TRUE:
            // x IS FALSE ==> NOT x (if x is not nullable)
            if (!a.getType().isNullable()) {
                return simplify(rexBuilder.makeCall(SqlStdOperatorTable.NOT, a));
            }
            break;
    }
    switch(a.getKind()) {
        case NOT:
            // (NOT x) IS TRUE ==> x IS FALSE
            // Similarly for IS NOT TRUE, IS FALSE, etc.
            // 
            // Note that
            // (NOT x) IS TRUE !=> x IS FALSE
            // because of null values.
            final SqlOperator notKind = RexUtil.op(kind.negateNullSafe());
            final RexNode arg = ((RexCall) a).operands.get(0);
            return simplify(rexBuilder.makeCall(notKind, arg));
    }
    RexNode a2 = simplify(a);
    if (a != a2) {
        return rexBuilder.makeCall(RexUtil.op(kind), ImmutableList.of(a2));
    }
    // cannot be simplified
    return null;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator)

Example 32 with SqlOperator

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

the class RexSqlReflectiveConvertletTable method get.

// ~ Methods ----------------------------------------------------------------
public RexSqlConvertlet get(RexCall call) {
    RexSqlConvertlet convertlet;
    final SqlOperator op = call.getOperator();
    // Is there a convertlet for this operator
    // (e.g. SqlStdOperatorTable.plusOperator)?
    convertlet = (RexSqlConvertlet) map.get(op);
    if (convertlet != null) {
        return convertlet;
    }
    // Is there a convertlet for this class of operator
    // (e.g. SqlBinaryOperator)?
    Class<? extends Object> clazz = op.getClass();
    while (clazz != null) {
        convertlet = (RexSqlConvertlet) map.get(clazz);
        if (convertlet != null) {
            return convertlet;
        }
        clazz = clazz.getSuperclass();
    }
    // Is there a convertlet for this class of expression
    // (e.g. SqlCall)?
    clazz = call.getClass();
    while (clazz != null) {
        convertlet = (RexSqlConvertlet) map.get(clazz);
        if (convertlet != null) {
            return convertlet;
        }
        clazz = clazz.getSuperclass();
    }
    return null;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator)

Example 33 with SqlOperator

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

the class RexSqlStandardConvertletTable method convertCall.

// ~ Methods ----------------------------------------------------------------
/**
 * Converts a call to an operator into a {@link SqlCall} to the same
 * operator.
 *
 * <p>Called automatically via reflection.
 *
 * @param converter Converter
 * @param call      Call
 * @return Sql call
 */
public SqlNode convertCall(RexToSqlNodeConverter converter, RexCall call) {
    if (get(call) == null) {
        return null;
    }
    final SqlOperator op = call.getOperator();
    final List<RexNode> operands = call.getOperands();
    final SqlNode[] exprs = convertExpressionList(converter, operands);
    if (exprs == null) {
        return null;
    }
    return new SqlBasicCall(op, exprs, SqlParserPos.ZERO);
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlNode(org.apache.calcite.sql.SqlNode)

Example 34 with SqlOperator

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

the class LookupOperatorOverloadsTest method test.

@Test
public void test() throws SQLException {
    final String schemaName = "MySchema";
    final String funcName = "MyFUNC";
    final String anotherName = "AnotherFunc";
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
        final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
        schema.add(funcName, table);
        schema.add(anotherName, table);
        final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
        schema.add(funcName, table2);
        final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
        final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
        final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
        CalciteCatalogReader reader = new CalciteCatalogReader(prepareContext.getRootSchema(), ImmutableList.<String>of(), typeFactory, prepareContext.config());
        final List<SqlOperator> operatorList = new ArrayList<>();
        SqlIdentifier myFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, funcName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(2, funcName, operatorList);
        operatorList.clear();
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(0, null, operatorList);
        operatorList.clear();
        SqlIdentifier anotherFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, anotherName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(anotherFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(1, anotherName, operatorList);
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 35 with SqlOperator

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

the class LookupOperatorOverloadsTest method checkFunctionType.

private void checkFunctionType(int size, String name, List<SqlOperator> operatorList) {
    assertThat(size, is(operatorList.size()));
    for (SqlOperator op : operatorList) {
        assertThat(op, instanceOf(SqlUserDefinedTableFunction.class));
        assertThat(name, is(op.getName()));
    }
}
Also used : SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) SqlOperator(org.apache.calcite.sql.SqlOperator)

Aggregations

SqlOperator (org.apache.calcite.sql.SqlOperator)129 ArrayList (java.util.ArrayList)44 RexNode (org.apache.calcite.rex.RexNode)41 RelDataType (org.apache.calcite.rel.type.RelDataType)25 SqlCall (org.apache.calcite.sql.SqlCall)24 RexCall (org.apache.calcite.rex.RexCall)21 SqlNode (org.apache.calcite.sql.SqlNode)21 SqlKind (org.apache.calcite.sql.SqlKind)15 List (java.util.List)13 SqlFunction (org.apache.calcite.sql.SqlFunction)13 RelNode (org.apache.calcite.rel.RelNode)12 RexBuilder (org.apache.calcite.rex.RexBuilder)11 RexInputRef (org.apache.calcite.rex.RexInputRef)11 NlsString (org.apache.calcite.util.NlsString)11 Test (org.junit.Test)11 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)10 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)10 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)9 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9