use of org.apache.calcite.rex.RexNode in project hive by apache.
the class TestCBOMaxNumToCNF method testCBOMaxNumToCNF1.
@Test
public void testCBOMaxNumToCNF1() {
// OR(=($0, 1), AND(=($0, 0), =($1, 8)))
// transformation creates 7 nodes AND(OR(=($0, 1), =($0, 0)), OR(=($0, 1), =($1, 8)))
// thus, it is triggered
final RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl();
final RexBuilder rexBuilder = new RexBuilder(typeFactory);
final RexNode cond = rexBuilder.makeCall(SqlStdOperatorTable.OR, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(1, typeFactory.createSqlType(SqlTypeName.INTEGER), false)), rexBuilder.makeCall(SqlStdOperatorTable.AND, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(0, typeFactory.createSqlType(SqlTypeName.INTEGER), false)), rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 1), rexBuilder.makeLiteral(8, typeFactory.createSqlType(SqlTypeName.INTEGER), false))));
final RexNode newCond = RexUtil.toCnf(rexBuilder, maxNumNodesCNF, cond);
assertEquals(newCond.toString(), "AND(OR(=($0, 1), =($0, 0)), OR(=($0, 1), =($1, 8)))");
}
use of org.apache.calcite.rex.RexNode in project hive by apache.
the class TestCBOMaxNumToCNF method testCBOMaxNumToCNF2.
@Test
public void testCBOMaxNumToCNF2() {
// OR(=($0, 1), =($0, 2), AND(=($0, 0), =($1, 8)))
// transformation creates 9 nodes AND(OR(=($0, 1), =($0, 2), =($0, 0)), OR(=($0, 1), =($0, 2), =($1, 8)))
// thus, it is NOT triggered
final RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl();
final RexBuilder rexBuilder = new RexBuilder(typeFactory);
final RexNode cond = rexBuilder.makeCall(SqlStdOperatorTable.OR, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(1, typeFactory.createSqlType(SqlTypeName.INTEGER), false)), rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(2, typeFactory.createSqlType(SqlTypeName.INTEGER), false)), rexBuilder.makeCall(SqlStdOperatorTable.AND, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(0, typeFactory.createSqlType(SqlTypeName.INTEGER), false)), rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 1), rexBuilder.makeLiteral(8, typeFactory.createSqlType(SqlTypeName.INTEGER), false))));
final RexNode newCond = RexUtil.toCnf(rexBuilder, maxNumNodesCNF, cond);
assertEquals(newCond.toString(), "OR(=($0, 1), =($0, 2), AND(=($0, 0), =($1, 8)))");
}
use of org.apache.calcite.rex.RexNode in project hive by apache.
the class HiveSubQRemoveRelBuilder method project.
/** Creates a {@link org.apache.calcite.rel.core.Project} of the given list
* of expressions, using the given names.
*
* <p>Names are deduced as follows:
* <ul>
* <li>If the length of {@code fieldNames} is greater than the index of
* the current entry in {@code nodes}, and the entry in
* {@code fieldNames} is not null, uses it; otherwise
* <li>If an expression projects an input field,
* or is a cast an input field,
* uses the input field name; otherwise
* <li>If an expression is a call to
* {@link org.apache.calcite.sql.fun.SqlStdOperatorTable#AS}
* (see {@link #alias}), removes the call but uses the intended alias.
* </ul>
*
* <p>After the field names have been inferred, makes the
* field names unique by appending numeric suffixes.
*
* @param nodes Expressions
* @param fieldNames Suggested field names
* @param force create project even if it is identity
*/
public HiveSubQRemoveRelBuilder project(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) {
final List<String> names = new ArrayList<>();
final List<RexNode> exprList = Lists.newArrayList(nodes);
final Iterator<String> nameIterator = fieldNames.iterator();
for (RexNode node : nodes) {
final String name = nameIterator.hasNext() ? nameIterator.next() : null;
final String name2 = inferAlias(exprList, node);
names.add(Util.first(name, name2));
}
final RelDataType inputRowType = peek().getRowType();
if (!force && RexUtil.isIdentity(exprList, inputRowType)) {
if (names.equals(inputRowType.getFieldNames())) {
// Do not create an identity project if it does not rename any fields
return this;
} else {
// create "virtual" row type for project only rename fields
final Frame frame = stack.pop();
final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), exprList, names, SqlValidatorUtil.F_SUGGESTER);
stack.push(new Frame(frame.rel, ImmutableList.of(Pair.of(frame.right.get(0).left, rowType))));
return this;
}
}
final RelNode project = projectFactory.createProject(build(), ImmutableList.copyOf(exprList), names);
push(project);
return this;
}
use of org.apache.calcite.rex.RexNode in project hive by apache.
the class HiveSubQRemoveRelBuilder method projects.
private List<RexNode> projects(RelDataType inputRowType) {
final List<RexNode> exprList = new ArrayList<>();
for (RelDataTypeField field : inputRowType.getFieldList()) {
final RexBuilder rexBuilder = cluster.getRexBuilder();
exprList.add(rexBuilder.makeInputRef(field.getType(), field.getIndex()));
}
return exprList;
}
use of org.apache.calcite.rex.RexNode in project hive by apache.
the class HiveSubQRemoveRelBuilder method call.
/** Creates a call to a scalar operator. */
public RexNode call(SqlOperator operator, RexNode... operands) {
final RexBuilder builder = cluster.getRexBuilder();
final List<RexNode> operandList = ImmutableList.copyOf(operands);
final RelDataType type = builder.deriveReturnType(operator, operandList);
if (type == null) {
throw new IllegalArgumentException("cannot derive type: " + operator + "; operands: " + Lists.transform(operandList, FN_TYPE));
}
return builder.makeCall(type, operator, operandList);
}
Aggregations