use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project calcite by apache.
the class RexProgramTest method testCnf.
/**
* Unit test for {@link org.apache.calcite.rex.RexUtil#toCnf}.
*/
@Test
public void testCnf() {
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 RexNode eRef = rexBuilder.makeFieldAccess(range, 4);
final RexNode fRef = rexBuilder.makeFieldAccess(range, 5);
final RexNode gRef = rexBuilder.makeFieldAccess(range, 6);
final RexNode hRef = rexBuilder.makeFieldAccess(range, 7);
final RexLiteral sevenLiteral = rexBuilder.makeExactLiteral(BigDecimal.valueOf(7));
final RexNode hEqSeven = eq(hRef, sevenLiteral);
checkCnf(aRef, "?0.a");
checkCnf(trueLiteral, "true");
checkCnf(falseLiteral, "false");
checkCnf(unknownLiteral, "null");
checkCnf(and(aRef, bRef), "AND(?0.a, ?0.b)");
checkCnf(and(aRef, bRef, cRef), "AND(?0.a, ?0.b, ?0.c)");
checkCnf(and(or(aRef, bRef), or(cRef, dRef)), "AND(OR(?0.a, ?0.b), OR(?0.c, ?0.d))");
checkCnf(or(and(aRef, bRef), and(cRef, dRef)), "AND(OR(?0.a, ?0.c), OR(?0.a, ?0.d), OR(?0.b, ?0.c), OR(?0.b, ?0.d))");
// Input has nested ORs, output ORs are flat
checkCnf(or(and(aRef, bRef), or(cRef, dRef)), "AND(OR(?0.a, ?0.c, ?0.d), OR(?0.b, ?0.c, ?0.d))");
checkCnf(or(aRef, not(and(bRef, not(hEqSeven)))), "OR(?0.a, NOT(?0.b), =(?0.h, 7))");
// apply de Morgan's theorem
checkCnf(not(or(aRef, not(bRef))), "AND(NOT(?0.a), ?0.b)");
// apply de Morgan's theorem,
// filter out 'OR ... FALSE' and 'AND ... TRUE'
checkCnf(not(or(and(aRef, trueLiteral), not(bRef), falseLiteral)), "AND(NOT(?0.a), ?0.b)");
checkCnf(and(aRef, or(bRef, and(cRef, dRef))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d))");
checkCnf(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, gRef)))))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d, ?0.e), OR(?0.b, ?0.d, ?0.f, ?0.g))");
checkCnf(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, and(gRef, or(trueLiteral, falseLiteral)))))))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d, ?0.e), OR(?0.b, ?0.d, ?0.f, ?0.g))");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project calcite by apache.
the class RexProgramTest method testThresholdCnf.
/**
* Unit test for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1290">[CALCITE-1290]
* When converting to CNF, fail if the expression exceeds a threshold</a>.
*/
@Test
public void testThresholdCnf() {
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType rowType = typeFactory.builder().add("x", intType).add("y", intType).build();
final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
final RexNode xRef = rexBuilder.makeFieldAccess(range, 0);
final RexNode yRef = rexBuilder.makeFieldAccess(range, 1);
final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(1));
final RexLiteral literal2 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(2));
final RexLiteral literal3 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(3));
final RexLiteral literal4 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(4));
// Expression
// OR(=(?0.x, 1), AND(=(?0.x, 2), =(?0.y, 3)))
// transformation creates 7 nodes
// AND(OR(=(?0.x, 1), =(?0.x, 2)), OR(=(?0.x, 1), =(?0.y, 3)))
// Thus, it is triggered.
checkThresholdCnf(or(eq(xRef, literal1), and(eq(xRef, literal2), eq(yRef, literal3))), 8, "AND(OR(=(?0.x, 1), =(?0.x, 2)), OR(=(?0.x, 1), =(?0.y, 3)))");
// Expression
// OR(=(?0.x, 1), =(?0.x, 2), AND(=(?0.x, 3), =(?0.y, 4)))
// transformation creates 9 nodes
// AND(OR(=(?0.x, 1), =(?0.x, 2), =(?0.x, 3)),
// OR(=(?0.x, 1), =(?0.x, 2), =(?0.y, 8)))
// Thus, it is NOT triggered.
checkThresholdCnf(or(eq(xRef, literal1), eq(xRef, literal2), and(eq(xRef, literal3), eq(yRef, literal4))), 8, "OR(=(?0.x, 1), =(?0.x, 2), AND(=(?0.x, 3), =(?0.y, 4)))");
}
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 bindRel.
private RelNode bindRel(RelNode node, RexBuilder builder, RelDataTypeFactory typeFactory) {
final RexShuttle binder = new RexShuttle() {
@Override
public RexNode visitDynamicParam(RexDynamicParam dynamicParam) {
return bind(dynamicParam, builder, typeFactory);
}
};
node = node.accept(binder);
node.childrenAccept(new RelVisitor() {
@Override
public void visit(RelNode node, int ordinal, RelNode parent) {
super.visit(node, ordinal, parent);
RelNode transformed = node.accept(binder);
if (!node.equals(transformed)) {
parent.replaceInput(ordinal, transformed);
}
}
});
return node;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project herddb by diennea.
the class SQLExpressionCompiler method compileExpression.
public static CompiledSQLExpression compileExpression(RexNode expression) {
if (expression == null) {
return null;
}
if (expression instanceof RexDynamicParam) {
RexDynamicParam p = (RexDynamicParam) expression;
return new TypedJdbcParameterExpression(p.getIndex(), CalcitePlanner.convertToHerdType(p.getType()));
} else if (expression instanceof RexLiteral) {
RexLiteral p = (RexLiteral) expression;
if (p.isNull()) {
return new ConstantExpression(null);
} else {
return new ConstantExpression(safeValue(p.getValue3(), p.getTypeName()));
}
} else if (expression instanceof RexInputRef) {
RexInputRef p = (RexInputRef) expression;
return new AccessCurrentRowExpression(p.getIndex());
} else if (expression instanceof RexCall) {
RexCall p = (RexCall) expression;
SqlOperator op = p.op;
String name = op.getName();
CompiledSQLExpression[] operands = new CompiledSQLExpression[p.operands.size()];
// System.out.println("operator '" + op + "' with " + p.operands.size() + " ops");
int i = 0;
for (RexNode operand : p.operands) {
// System.out.println("operand: " + operand);
operands[i++] = compileExpression(operand);
}
switch(name) {
case "=":
return new CompiledEqualsExpression(false, operands[0], operands[1]);
case "<>":
return new CompiledNotEqualsExpression(false, operands[0], operands[1]);
case ">":
return new CompiledGreaterThenExpression(false, operands[0], operands[1]);
case ">=":
return new CompiledGreaterThenEqualsExpression(false, operands[0], operands[1]);
case "<":
return new CompiledMinorThenExpression(false, operands[0], operands[1]);
case "<=":
return new CompiledMinorThenEqualsExpression(false, operands[0], operands[1]);
case "+":
return new CompiledAddExpression(false, operands[0], operands[1]);
case "-":
if (operands.length == 1) {
return new CompiledSignedExpression('-', operands[0]);
} else if (operands.length == 2) {
return new CompiledSubtractExpression(false, operands[0], operands[1]);
}
break;
case "*":
return new CompiledMultiplyExpression(false, operands[0], operands[1]);
case "/":
return new CompiledDivideExpression(false, operands[0], operands[1]);
case "LIKE":
return new CompiledLikeExpression(false, operands[0], operands[1]);
case "AND":
return new CompiledMultiAndExpression(operands);
case "OR":
return new CompiledMultiOrExpression(operands);
case "NOT":
return new CompiledParenthesisExpression(true, operands[0]);
case "IS NOT NULL":
return new CompiledIsNullExpression(true, operands[0]);
case "IS NOT TRUE":
return new CompiledIsNotTrueExpression(false, operands[0]);
case "IS NULL":
return new CompiledIsNullExpression(false, operands[0]);
case "CAST":
return operands[0].cast(CalcitePlanner.convertToHerdType(p.type));
case "CASE":
List<Map.Entry<CompiledSQLExpression, CompiledSQLExpression>> cases = new ArrayList<>(operands.length / 2);
boolean hasElse = operands.length % 2 == 1;
int numcases = hasElse ? ((operands.length - 1) / 2) : (operands.length / 2);
for (int j = 0; j < numcases; j++) {
cases.add(new AbstractMap.SimpleImmutableEntry<>(operands[j * 2], operands[j * 2 + 1]));
}
CompiledSQLExpression elseExp = hasElse ? operands[operands.length - 1] : null;
return new CompiledCaseExpression(cases, elseExp);
case BuiltinFunctions.NAME_CURRENT_TIMESTAMP:
return new CompiledFunction(BuiltinFunctions.CURRENT_TIMESTAMP, Collections.emptyList());
case BuiltinFunctions.NAME_LOWERCASE:
return new CompiledFunction(BuiltinFunctions.LOWER, Arrays.asList(operands));
case BuiltinFunctions.NAME_UPPER:
return new CompiledFunction(BuiltinFunctions.UPPER, Arrays.asList(operands));
case BuiltinFunctions.NAME_ABS:
return new CompiledFunction(BuiltinFunctions.ABS, Arrays.asList(operands));
case BuiltinFunctions.NAME_ROUND:
return new CompiledFunction(BuiltinFunctions.ROUND, Arrays.asList(operands));
default:
throw new StatementExecutionException("unsupported operator '" + name + "'");
}
} else if (expression instanceof RexFieldAccess) {
RexFieldAccess p = (RexFieldAccess) expression;
CompiledSQLExpression object = compileExpression(p.getReferenceExpr());
return new AccessFieldExpression(object, p.getField().getName());
} else if (expression instanceof RexCorrelVariable) {
RexCorrelVariable p = (RexCorrelVariable) expression;
return new AccessCorrelVariableExpression(p.id.getId(), p.id.getName());
}
throw new StatementExecutionException("not implemented expression type " + expression.getClass() + ": " + expression);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam in project hive by apache.
the class RelFieldTrimmer method trimFields.
/**
* Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
* {@link org.apache.calcite.rel.core.Sort}.
*/
public TrimResult trimFields(Sort sort, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
final RelDataType rowType = sort.getRowType();
final int fieldCount = rowType.getFieldCount();
final RelCollation collation = sort.getCollation();
final RelNode input = sort.getInput();
// We use the fields used by the consumer, plus any fields used as sort
// keys.
final ImmutableBitSet.Builder inputFieldsUsed = fieldsUsed.rebuild();
for (RelFieldCollation field : collation.getFieldCollations()) {
inputFieldsUsed.set(field.getFieldIndex());
}
// Create input with trimmed columns.
final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
TrimResult trimResult = trimChild(sort, input, inputFieldsUsed.build(), inputExtraFields);
RelNode newInput = trimResult.left;
final Mapping inputMapping = trimResult.right;
// there's nothing we can do.
if (newInput == input && inputMapping.isIdentity() && fieldsUsed.cardinality() == fieldCount) {
return result(sort, Mappings.createIdentity(fieldCount));
}
// leave the Sort unchanged in case we have dynamic limits
if (sort.offset instanceof RexDynamicParam || sort.fetch instanceof RexDynamicParam) {
return result(sort, inputMapping);
}
final RelBuilder relBuilder = REL_BUILDER.get();
relBuilder.push(newInput);
final int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset);
final int fetch = sort.fetch == null ? -1 : RexLiteral.intValue(sort.fetch);
final ImmutableList<RexNode> fields = relBuilder.fields(RexUtil.apply(inputMapping, collation));
relBuilder.sortLimit(offset, fetch, fields);
// needs them for its condition.
return result(relBuilder.build(), inputMapping);
}
Aggregations