Search in sources :

Example 11 with RexDynamicParam

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project beam by apache.

the class LimitOffsetScanToLimitConverter method convert.

@Override
public RelNode convert(ResolvedLimitOffsetScan zetaNode, List<RelNode> inputs) {
    RelNode input = inputs.get(0);
    RelCollation relCollation = RelCollations.of(ImmutableList.of());
    RexNode offset = zetaNode.getOffset() == null ? null : getExpressionConverter().convertRexNodeFromResolvedExpr(zetaNode.getOffset());
    RexNode fetch = getExpressionConverter().convertRexNodeFromResolvedExpr(zetaNode.getLimit(), zetaNode.getColumnList(), input.getRowType().getFieldList(), ImmutableMap.of());
    // offset or fetch being RexDynamicParam means it is NULL (the only param supported currently)
    if (offset instanceof RexDynamicParam || RexLiteral.isNullLiteral(offset) || fetch instanceof RexDynamicParam || RexLiteral.isNullLiteral(fetch)) {
        throw new UnsupportedOperationException("Limit requires non-null count and offset");
    }
    return LogicalSort.create(input, relCollation, offset, fetch);
}
Also used : RelCollation(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode) RexDynamicParam(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)

Example 12 with RexDynamicParam

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project beam by apache.

the class BeamSqlUnparseContext method toSql.

@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind().equals(SqlKind.LITERAL)) {
        final RexLiteral literal = (RexLiteral) rex;
        SqlTypeName name = literal.getTypeName();
        SqlTypeFamily family = name.getFamily();
        if (SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE.equals(name)) {
            TimestampString timestampString = literal.getValueAs(TimestampString.class);
            return new SqlDateTimeLiteral(timestampString, POS);
        } else if (SqlTypeFamily.BINARY.equals(family)) {
            ByteString byteString = literal.getValueAs(ByteString.class);
            BitString bitString = BitString.createFromHexString(byteString.toString(16));
            return new SqlByteStringLiteral(bitString, POS);
        } else if (SqlTypeFamily.CHARACTER.equals(family)) {
            String escaped = ESCAPE_FOR_ZETA_SQL.translate(literal.getValueAs(String.class));
            return SqlLiteral.createCharString(escaped, POS);
        } else if (SqlTypeName.SYMBOL.equals(literal.getTypeName())) {
            Enum symbol = literal.getValueAs(Enum.class);
            if (TimeUnitRange.DOW.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFWEEK");
            } else if (TimeUnitRange.DOY.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFYEAR");
            } else if (TimeUnitRange.WEEK.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "ISOWEEK");
            }
        }
    } else if (rex.getKind().equals(SqlKind.DYNAMIC_PARAM)) {
        final RexDynamicParam param = (RexDynamicParam) rex;
        final int index = param.getIndex();
        final String name = "null_param_" + index;
        nullParams.put(name, param.getType());
        return new NamedDynamicParam(index, POS, name);
    } else if (SqlKind.SEARCH.equals(rex.getKind())) {
        // Workaround CALCITE-4716
        RexCall search = (RexCall) rex;
        RexLocalRef ref = (RexLocalRef) search.operands.get(1);
        RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
        rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
    }
    return super.toSql(program, rex);
}
Also used : RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) SqlTypeName(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) RexLocalRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexDynamicParam(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)

Example 13 with RexDynamicParam

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project calcite by apache.

the class RexProgramTest method checkExponentialCnf.

private void checkExponentialCnf(int n) {
    final RelDataType booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    for (int i = 0; i < n; i++) {
        builder.add("x" + i, booleanType).add("y" + i, booleanType);
    }
    final RelDataType rowType3 = builder.build();
    final RexDynamicParam range3 = rexBuilder.makeDynamicParam(rowType3, 0);
    final List<RexNode> list = Lists.newArrayList();
    for (int i = 0; i < n; i++) {
        list.add(and(rexBuilder.makeFieldAccess(range3, i * 2), rexBuilder.makeFieldAccess(range3, i * 2 + 1)));
    }
    final RexNode cnf = RexUtil.toCnf(rexBuilder, or(list));
    final int nodeCount = nodeCount(cnf);
    assertThat((n + 1) * (int) Math.pow(2, n) + 1, equalTo(nodeCount));
    if (n == 3) {
        assertThat(cnf.toString(), equalTo("AND(OR(?0.x0, ?0.x1, ?0.x2), OR(?0.x0, ?0.x1, ?0.y2)," + " OR(?0.x0, ?0.y1, ?0.x2), OR(?0.x0, ?0.y1, ?0.y2)," + " OR(?0.y0, ?0.x1, ?0.x2), OR(?0.y0, ?0.x1, ?0.y2)," + " OR(?0.y0, ?0.y1, ?0.x2), OR(?0.y0, ?0.y1, ?0.y2))"));
    }
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode)

Example 14 with RexDynamicParam

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project calcite by apache.

the class RexProgramTest method testSimplifyFilter.

@Test
public void testSimplifyFilter() {
    final RelDataType booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
    final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
    final RelDataType rowType = typeFactory.builder().add("a", booleanType).add("b", booleanType).add("c", booleanType).add("d", booleanType).add("e", booleanType).add("f", booleanType).add("g", booleanType).add("h", intType).build();
    final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
    final RexNode aRef = rexBuilder.makeFieldAccess(range, 0);
    final RexNode bRef = rexBuilder.makeFieldAccess(range, 1);
    final RexNode cRef = rexBuilder.makeFieldAccess(range, 2);
    final RexNode dRef = rexBuilder.makeFieldAccess(range, 3);
    final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.ONE);
    final RexLiteral literal5 = rexBuilder.makeExactLiteral(new BigDecimal(5));
    final RexLiteral literal10 = rexBuilder.makeExactLiteral(BigDecimal.TEN);
    // condition, and the inverse
    checkSimplifyFilter(and(le(aRef, literal1), gt(aRef, literal1)), "false");
    checkSimplifyFilter(and(le(aRef, literal1), ge(aRef, literal1)), "AND(<=(?0.a, 1), >=(?0.a, 1))");
    checkSimplifyFilter(and(lt(aRef, literal1), eq(aRef, literal1), ge(aRef, literal1)), "false");
    // simplify equals boolean
    final ImmutableList<RexNode> args = ImmutableList.of(eq(eq(aRef, literal1), trueLiteral), eq(bRef, literal1));
    checkSimplifyFilter(and(args), "AND(=(?0.a, 1), =(?0.b, 1))");
    // as previous, using simplifyFilterPredicates
    assertThat(simplify.withUnknownAsFalse(true).simplifyFilterPredicates(args).toString(), equalTo("AND(=(?0.a, 1), =(?0.b, 1))"));
    // "a = 1 and a = 10" is always false
    final ImmutableList<RexNode> args2 = ImmutableList.of(eq(aRef, literal1), eq(aRef, literal10));
    checkSimplifyFilter(and(args2), "false");
    assertThat(simplify.withUnknownAsFalse(true).simplifyFilterPredicates(args2), nullValue());
    // equality on constants, can remove the equality on the variables
    checkSimplifyFilter(and(eq(aRef, literal1), eq(bRef, literal1), eq(aRef, bRef)), "AND(=(?0.a, 1), =(?0.b, 1))");
    // condition not satisfiable
    checkSimplifyFilter(and(eq(aRef, literal1), eq(bRef, literal10), eq(aRef, bRef)), "false");
    // condition not satisfiable
    checkSimplifyFilter(and(gt(aRef, literal10), ge(bRef, literal1), lt(aRef, literal10)), "false");
    // one "and" containing three "or"s
    checkSimplifyFilter(or(gt(aRef, literal10), gt(bRef, literal1), gt(aRef, literal10)), "OR(>(?0.a, 10), >(?0.b, 1))");
    // case: trailing false and null, remove
    checkSimplifyFilter(case_(aRef, trueLiteral, bRef, trueLiteral, cRef, falseLiteral, dRef, falseLiteral, unknownLiteral), "CAST(OR(?0.a, ?0.b)):BOOLEAN");
    // condition with null value for range
    checkSimplifyFilter(and(gt(aRef, unknownLiteral), ge(bRef, literal1)), "false");
    // condition "1 < a && 5 < x" yields "5 < x"
    checkSimplifyFilter(and(lt(literal1, aRef), lt(literal5, aRef)), RelOptPredicateList.EMPTY, "<(5, ?0.a)");
    // condition "1 < a && a < 5" is unchanged
    checkSimplifyFilter(and(lt(literal1, aRef), lt(aRef, literal5)), RelOptPredicateList.EMPTY, "AND(<(1, ?0.a), <(?0.a, 5))");
    // condition "1 > a && 5 > x" yields "1 > a"
    checkSimplifyFilter(and(gt(literal1, aRef), gt(literal5, aRef)), RelOptPredicateList.EMPTY, ">(1, ?0.a)");
    // condition "1 > a && a > 5" yields false
    checkSimplifyFilter(and(gt(literal1, aRef), gt(aRef, literal5)), RelOptPredicateList.EMPTY, "false");
    // range with no predicates;
    // condition "a > 1 && a < 10 && a < 5" yields "a < 1 && a < 5"
    checkSimplifyFilter(and(gt(aRef, literal1), lt(aRef, literal10), lt(aRef, literal5)), RelOptPredicateList.EMPTY, "AND(>(?0.a, 1), <(?0.a, 5))");
    // condition "a > 1 && a < 10 && a < 5"
    // with pre-condition "a > 5"
    // yields "false"
    checkSimplifyFilter(and(gt(aRef, literal1), lt(aRef, literal10), lt(aRef, literal5)), RelOptPredicateList.of(rexBuilder, ImmutableList.of(gt(aRef, literal5))), "false");
    // condition "a > 1 && a < 10 && a <= 5"
    // with pre-condition "a >= 5"
    // yields "a = 5"
    // "a <= 5" would also be correct, just a little less concise.
    checkSimplifyFilter(and(gt(aRef, literal1), lt(aRef, literal10), le(aRef, literal5)), RelOptPredicateList.of(rexBuilder, ImmutableList.of(ge(aRef, literal5))), "=(?0.a, 5)");
    // condition "a > 1 && a < 10 && a < 5"
    // with pre-condition "b < 10 && a > 5"
    // yields "a > 1 and a < 5"
    checkSimplifyFilter(and(gt(aRef, literal1), lt(aRef, literal10), lt(aRef, literal5)), RelOptPredicateList.of(rexBuilder, ImmutableList.of(lt(bRef, literal10), ge(aRef, literal1))), "AND(>(?0.a, 1), <(?0.a, 5))");
    // condition "a > 1"
    // with pre-condition "b < 10 && a > 5"
    // yields "true"
    checkSimplifyFilter(gt(aRef, literal1), RelOptPredicateList.of(rexBuilder, ImmutableList.of(lt(bRef, literal10), gt(aRef, literal5))), "true");
    // condition "a < 1"
    // with pre-condition "b < 10 && a > 5"
    // yields "false"
    checkSimplifyFilter(lt(aRef, literal1), RelOptPredicateList.of(rexBuilder, ImmutableList.of(lt(bRef, literal10), gt(aRef, literal5))), "false");
    // condition "a > 5"
    // with pre-condition "b < 10 && a >= 5"
    // yields "a > 5"
    checkSimplifyFilter(gt(aRef, literal5), RelOptPredicateList.of(rexBuilder, ImmutableList.of(lt(bRef, literal10), ge(aRef, literal5))), ">(?0.a, 5)");
    // condition "a > 5"
    // with pre-condition "a <= 5"
    // yields "false"
    checkSimplifyFilter(gt(aRef, literal5), RelOptPredicateList.of(rexBuilder, ImmutableList.of(le(aRef, literal5))), "false");
    // condition "a > 5"
    // with pre-condition "a <= 5 and b <= 5"
    // yields "false"
    checkSimplifyFilter(gt(aRef, literal5), RelOptPredicateList.of(rexBuilder, ImmutableList.of(le(aRef, literal5), le(bRef, literal5))), "false");
    // condition "a > 5 or b > 5"
    // with pre-condition "a <= 5 and b <= 5"
    // should yield "false" but yields "a = 5 or b = 5"
    checkSimplifyFilter(or(gt(aRef, literal5), gt(bRef, literal5)), RelOptPredicateList.of(rexBuilder, ImmutableList.of(le(aRef, literal5), le(bRef, literal5))), "false");
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 15 with RexDynamicParam

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project druid by druid-io.

the class RelParameterizerShuttle method bind.

private RexNode bind(RexNode node, RexBuilder builder, RelDataTypeFactory typeFactory) {
    if (node instanceof RexDynamicParam) {
        RexDynamicParam dynamicParam = (RexDynamicParam) node;
        // if we have a value for dynamic parameter, replace with a literal, else add to list of unbound parameters
        if (plannerContext.getParameters().size() > dynamicParam.getIndex()) {
            TypedValue param = plannerContext.getParameters().get(dynamicParam.getIndex());
            if (param == null) {
                throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
            }
            if (param.value == null) {
                return builder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.NULL));
            }
            SqlTypeName typeName = SqlTypeName.getNameForJdbcType(param.type.typeId);
            return builder.makeLiteral(param.value, typeFactory.createSqlType(typeName), true);
        } else {
            throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
        }
    }
    return node;
}
Also used : SqlPlanningException(org.apache.druid.sql.SqlPlanningException) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Aggregations

RexDynamicParam (org.apache.calcite.rex.RexDynamicParam)13 RexNode (org.apache.calcite.rex.RexNode)11 RelDataType (org.apache.calcite.rel.type.RelDataType)10 RexLiteral (org.apache.calcite.rex.RexLiteral)8 Test (org.junit.Test)7 RelNode (org.apache.calcite.rel.RelNode)3 RexDynamicParam (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)2 RelCollation (org.apache.calcite.rel.RelCollation)2 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)2 Mapping (org.apache.calcite.util.mapping.Mapping)2 StatementExecutionException (herddb.model.StatementExecutionException)1 RawString (herddb.utils.RawString)1 BigDecimal (java.math.BigDecimal)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ByteString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString)1