Search in sources :

Example 1 with SqlRowOperator

use of org.apache.calcite.sql.fun.SqlRowOperator in project calcite by apache.

the class SqlToRelConverter method convertInToOr.

/**
 * Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".
 *
 * @param leftKeys   LHS
 * @param valuesList RHS
 * @param op         The operator (IN, NOT IN, > SOME, ...)
 * @return converted expression
 */
private RexNode convertInToOr(final Blackboard bb, final List<RexNode> leftKeys, SqlNodeList valuesList, SqlInOperator op) {
    final List<RexNode> comparisons = new ArrayList<>();
    for (SqlNode rightVals : valuesList) {
        RexNode rexComparison;
        final SqlOperator comparisonOp;
        if (op instanceof SqlQuantifyOperator) {
            comparisonOp = RelOptUtil.op(((SqlQuantifyOperator) op).comparisonKind, SqlStdOperatorTable.EQUALS);
        } else {
            comparisonOp = SqlStdOperatorTable.EQUALS;
        }
        if (leftKeys.size() == 1) {
            rexComparison = rexBuilder.makeCall(comparisonOp, leftKeys.get(0), ensureSqlType(leftKeys.get(0).getType(), bb.convertExpression(rightVals)));
        } else {
            assert rightVals instanceof SqlCall;
            final SqlBasicCall call = (SqlBasicCall) rightVals;
            assert (call.getOperator() instanceof SqlRowOperator) && call.operandCount() == leftKeys.size();
            rexComparison = RexUtil.composeConjunction(rexBuilder, Iterables.transform(Pair.zip(leftKeys, call.getOperandList()), new Function<Pair<RexNode, SqlNode>, RexNode>() {

                public RexNode apply(Pair<RexNode, SqlNode> pair) {
                    return rexBuilder.makeCall(comparisonOp, pair.left, ensureSqlType(pair.left.getType(), bb.convertExpression(pair.right)));
                }
            }), false);
        }
        comparisons.add(rexComparison);
    }
    switch(op.kind) {
        case ALL:
            return RexUtil.composeConjunction(rexBuilder, comparisons, true);
        case NOT_IN:
            return rexBuilder.makeCall(SqlStdOperatorTable.NOT, RexUtil.composeDisjunction(rexBuilder, comparisons, true));
        case IN:
        case SOME:
            return RexUtil.composeDisjunction(rexBuilder, comparisons, true);
        default:
            throw new AssertionError();
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) SqlRowOperator(org.apache.calcite.sql.fun.SqlRowOperator) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlQuantifyOperator(org.apache.calcite.sql.fun.SqlQuantifyOperator) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode) Pair(org.apache.calcite.util.Pair)

Example 2 with SqlRowOperator

use of org.apache.calcite.sql.fun.SqlRowOperator in project hazelcast by hazelcast.

the class HazelcastSqlToRelConverter method constructComparisons.

/**
 * Constructs comparisons between
 * left-hand operand (as a rule, SqlIdentifier) and right-hand list.
 */
private List<RexNode> constructComparisons(Blackboard bb, List<RexNode> leftKeys, SqlNodeList valuesList) {
    final List<RexNode> comparisons = new ArrayList<>();
    for (SqlNode rightValues : valuesList) {
        RexNode rexComparison;
        final SqlOperator comparisonOp = SqlStdOperatorTable.EQUALS;
        if (leftKeys.size() == 1) {
            rexComparison = rexBuilder.makeCall(comparisonOp, leftKeys.get(0), ensureSqlType(leftKeys.get(0).getType(), bb.convertExpression(rightValues)));
        } else {
            assert rightValues instanceof SqlCall;
            final SqlBasicCall basicCall = (SqlBasicCall) rightValues;
            assert basicCall.getOperator() instanceof SqlRowOperator && basicCall.operandCount() == leftKeys.size();
            rexComparison = RexUtil.composeConjunction(rexBuilder, Pair.zip(leftKeys, basicCall.getOperandList()).stream().map(pair -> rexBuilder.makeCall(comparisonOp, pair.left, ensureSqlType(pair.left.getType(), bb.convertExpression(pair.right)))).collect(Collectors.toList()));
        }
        comparisons.add(rexComparison);
    }
    return comparisons;
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) LiteralUtils(com.hazelcast.jet.sql.impl.validate.literal.LiteralUtils) RelDataTypeFamily(org.apache.calcite.rel.type.RelDataTypeFamily) HazelcastResources(com.hazelcast.jet.sql.impl.validate.HazelcastResources) SqlCall(org.apache.calcite.sql.SqlCall) SqlJsonEmptyOrError(org.apache.calcite.sql.SqlJsonEmptyOrError) BigDecimal(java.math.BigDecimal) RexUtil(org.apache.calcite.rex.RexUtil) SqlNode(org.apache.calcite.sql.SqlNode) SqlUtil(org.apache.calcite.sql.SqlUtil) RexNode(org.apache.calcite.rex.RexNode) Arrays.asList(java.util.Arrays.asList) LocalTime(java.time.LocalTime) TableModify(org.apache.calcite.rel.core.TableModify) DAY(org.apache.calcite.avatica.util.TimeUnit.DAY) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) YEAR(org.apache.calcite.avatica.util.TimeUnit.YEAR) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlKind(org.apache.calcite.sql.SqlKind) IdentityHashMap(java.util.IdentityHashMap) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) HazelcastJsonParseFunction(com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonParseFunction) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) RexLiteral(org.apache.calcite.rex.RexLiteral) Set(java.util.Set) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) HazelcastJsonValueFunction(com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonValueFunction) List(java.util.List) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) SqlRowOperator(org.apache.calcite.sql.fun.SqlRowOperator) SqlErrorCode(com.hazelcast.sql.impl.SqlErrorCode) MONTH(org.apache.calcite.avatica.util.TimeUnit.MONTH) SqlInsert(org.apache.calcite.sql.SqlInsert) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) SqlJsonValueReturning(org.apache.calcite.sql.SqlJsonValueReturning) TimeString(org.apache.calcite.util.TimeString) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) Lists(com.google.common.collect.Lists) SqlLiteral(org.apache.calcite.sql.SqlLiteral) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.calcite.util.Pair) Converter(com.hazelcast.sql.impl.type.converter.Converter) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) QueryException(com.hazelcast.sql.impl.QueryException) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlExtendedInsert(com.hazelcast.jet.sql.impl.parse.SqlExtendedInsert) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HazelcastReturnTypeInference(com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference) RexBuilder(org.apache.calcite.rex.RexBuilder) Literal(com.hazelcast.jet.sql.impl.validate.literal.Literal) SqlRexConvertletTable(org.apache.calcite.sql2rel.SqlRexConvertletTable) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException) RelNode(org.apache.calcite.rel.RelNode) LogicalTableInsert(com.hazelcast.jet.sql.impl.opt.logical.LogicalTableInsert) Prepare(org.apache.calcite.prepare.Prepare) SECOND(org.apache.calcite.avatica.util.TimeUnit.SECOND) HazelcastBetweenOperator(com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator) SqlJsonValueEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior) SqlInOperator(org.apache.calcite.sql.fun.SqlInOperator) Converters(com.hazelcast.sql.impl.type.converter.Converters) Util(org.apache.calcite.util.Util) LogicalTableSink(com.hazelcast.jet.sql.impl.opt.logical.LogicalTableSink) Resources(org.apache.calcite.runtime.Resources) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Collections(java.util.Collections) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) SqlRowOperator(org.apache.calcite.sql.fun.SqlRowOperator) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 LogicalTableInsert (com.hazelcast.jet.sql.impl.opt.logical.LogicalTableInsert)1 LogicalTableSink (com.hazelcast.jet.sql.impl.opt.logical.LogicalTableSink)1 SqlExtendedInsert (com.hazelcast.jet.sql.impl.parse.SqlExtendedInsert)1 HazelcastResources (com.hazelcast.jet.sql.impl.validate.HazelcastResources)1 Literal (com.hazelcast.jet.sql.impl.validate.literal.Literal)1 LiteralUtils (com.hazelcast.jet.sql.impl.validate.literal.LiteralUtils)1 HazelcastJsonParseFunction (com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonParseFunction)1 HazelcastJsonValueFunction (com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonValueFunction)1 HazelcastBetweenOperator (com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator)1 HazelcastReturnTypeInference (com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference)1 HazelcastTypeUtils (com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils)1 QueryException (com.hazelcast.sql.impl.QueryException)1 SqlErrorCode (com.hazelcast.sql.impl.SqlErrorCode)1 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)1 Converter (com.hazelcast.sql.impl.type.converter.Converter)1 Converters (com.hazelcast.sql.impl.type.converter.Converters)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1