protected MutableRel invert(List<Pair<RexNode, String>> namedProjects, MutableRel input, RexShuttle shuttle) { LOGGER.trace("SubstitutionVisitor: invert:\nprojects: {}\ninput: {}\nproject: {}\n", namedProjects, input, shuttle); final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = input.cluster.getRexBuilder(); final List<RexNode> projects = Pair.left(namedProjects); for (RexNode expr : projects) { exprList.add(rexBuilder.makeZeroLiteral(expr.getType())); } for (Ord<RexNode> expr : Ord.zip(projects)) { final RexNode node = expr.e.accept(shuttle); if (node == null) { throw MatchFailed.INSTANCE; } exprList.set(expr.i, node); } return MutableProject.of(input, exprList, Pair.right(namedProjects)); }
protected MutableRel invert(MutableRel model, MutableRel input, MutableProject project) { LOGGER.trace("SubstitutionVisitor: invert:\nmodel: {}\ninput: {}\nproject: {}\n", model, input, project); if (project.getProjects().size() < model.getRowType().getFieldCount()) { throw MatchFailed.INSTANCE; } final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = model.cluster.getRexBuilder(); for (RelDataTypeField field : model.getRowType().getFieldList()) { exprList.add(rexBuilder.makeZeroLiteral(field.getType())); } for (Ord<RexNode> expr : Ord.zip(project.getProjects())) { if (expr.e instanceof RexInputRef) { final int target = ((RexInputRef) expr.e).getIndex(); exprList.set(target, rexBuilder.ensureType(expr.e.getType(), RexInputRef.of(expr.i, input.rowType), false)); } else { throw MatchFailed.INSTANCE; } } return MutableProject.of(model.rowType, input, exprList); } }
SqlStdOperatorTable.COALESCE, refSum, rexBuilder.makeZeroLiteral(refSum.getType())); return rexBuilder.makeCast(oldCall.getType(), coalesce);
private RexNode makeCastExactToBoolean(RelDataType toType, RexNode exp) { return makeCall(toType, SqlStdOperatorTable.NOT_EQUALS, ImmutableList.of(exp, makeZeroLiteral(exp.getType()))); }
private RexNode makeCastExactToBoolean(RelDataType toType, RexNode exp) { return makeCall(toType, SqlStdOperatorTable.NOT_EQUALS, ImmutableList.of(exp, makeZeroLiteral(exp.getType()))); }
protected MutableRel invert(List<Pair<RexNode, String>> namedProjects, MutableRel input, RexShuttle shuttle) { LOGGER.trace("SubstitutionVisitor: invert:\nprojects: {}\ninput: {}\nproject: {}\n", namedProjects, input, shuttle); final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = input.cluster.getRexBuilder(); final List<RexNode> projects = Pair.left(namedProjects); for (RexNode expr : projects) { exprList.add(rexBuilder.makeZeroLiteral(expr.getType())); } for (Ord<RexNode> expr : Ord.zip(projects)) { final RexNode node = expr.e.accept(shuttle); if (node == null) { throw MatchFailed.INSTANCE; } exprList.set(expr.i, node); } return MutableProject.of(input, exprList, Pair.right(namedProjects)); }
protected MutableRel invert(List<Pair<RexNode, String>> namedProjects, MutableRel input, RexShuttle shuttle) { LOGGER.trace("SubstitutionVisitor: invert:\nprojects: {}\ninput: {}\nproject: {}\n", namedProjects, input, shuttle); final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = input.cluster.getRexBuilder(); final List<RexNode> projects = Pair.left(namedProjects); for (RexNode expr : projects) { exprList.add(rexBuilder.makeZeroLiteral(expr.getType())); } for (Ord<RexNode> expr : Ord.zip(projects)) { final RexNode node = expr.e.accept(shuttle); if (node == null) { throw MatchFailed.INSTANCE; } exprList.set(expr.i, node); } return MutableProject.of(input, exprList, Pair.right(namedProjects)); }
private RexNode makeCastBooleanToExact(RelDataType toType, RexNode exp) { final RexNode casted = makeCall(SqlStdOperatorTable.CASE, exp, makeExactLiteral(BigDecimal.ONE, toType), makeZeroLiteral(toType)); if (!exp.getType().isNullable()) { return casted; } return makeCall(toType, SqlStdOperatorTable.CASE, ImmutableList.of(makeCall(SqlStdOperatorTable.IS_NOT_NULL, exp), casted, makeNullLiteral(toType))); }
private RexNode makeCastBooleanToExact(RelDataType toType, RexNode exp) { final RexNode casted = makeCall(SqlStdOperatorTable.CASE, exp, makeExactLiteral(BigDecimal.ONE, toType), makeZeroLiteral(toType)); if (!exp.getType().isNullable()) { return casted; } return makeCall(toType, SqlStdOperatorTable.CASE, ImmutableList.of(makeCall(SqlStdOperatorTable.IS_NOT_NULL, exp), casted, makeNullLiteral(toType))); }
protected MutableRel invert(MutableRel model, MutableRel input, MutableProject project) { LOGGER.trace("SubstitutionVisitor: invert:\nmodel: {}\ninput: {}\nproject: {}\n", model, input, project); if (project.projects.size() < model.rowType.getFieldCount()) { throw MatchFailed.INSTANCE; } final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = model.cluster.getRexBuilder(); for (RelDataTypeField field : model.rowType.getFieldList()) { exprList.add(rexBuilder.makeZeroLiteral(field.getType())); } for (Ord<RexNode> expr : Ord.zip(project.projects)) { if (expr.e instanceof RexInputRef) { final int target = ((RexInputRef) expr.e).getIndex(); exprList.set(target, rexBuilder.ensureType(expr.e.getType(), RexInputRef.of(expr.i, input.rowType), false)); } else { throw MatchFailed.INSTANCE; } } return MutableProject.of(model.rowType, input, exprList); } }
protected MutableRel invert(MutableRel model, MutableRel input, MutableProject project) { LOGGER.trace("SubstitutionVisitor: invert:\nmodel: {}\ninput: {}\nproject: {}\n", model, input, project); if (project.projects.size() < model.rowType.getFieldCount()) { throw MatchFailed.INSTANCE; } final List<RexNode> exprList = new ArrayList<>(); final RexBuilder rexBuilder = model.cluster.getRexBuilder(); for (RelDataTypeField field : model.rowType.getFieldList()) { exprList.add(rexBuilder.makeZeroLiteral(field.getType())); } for (Ord<RexNode> expr : Ord.zip(project.projects)) { if (expr.e instanceof RexInputRef) { final int target = ((RexInputRef) expr.e).getIndex(); exprList.set(target, rexBuilder.ensureType(expr.e.getType(), RexInputRef.of(expr.i, input.rowType), false)); } else { throw MatchFailed.INSTANCE; } } return MutableProject.of(model.rowType, input, exprList); } }
exprList.add( source < 0 ? rexBuilder.makeZeroLiteral(field.getType()) : rexBuilder.makeInputRef(field.getType(), source));
exprList.add( source < 0 ? rexBuilder.makeZeroLiteral(field.getType()) : rexBuilder.makeInputRef(field.getType(), source));
/** Tests {@link org.apache.calcite.rex.RexUtil#isLiteral(RexNode, boolean)}, * which was added to enhance Calcite's public API * <a href="https://issues.apache.org/jira/browse/CALCITE-1219">[CALCITE-1219] * Add a method to SqlOperatorBinding to determine whether operand is a * literal</a>. */ @Test public void testRexNodeLiteral() { final RexNode literal = rexBuilder.makeZeroLiteral( integerDataType); final RexNode castLiteral = rexBuilder.makeCall( integerDataType, SqlStdOperatorTable.CAST, Lists.newArrayList(literal)); final RexNode castCastLiteral = rexBuilder.makeCall( integerDataType, SqlStdOperatorTable.CAST, Lists.newArrayList(castLiteral)); // RexLiteral is considered as a Literal assertSame(true, RexUtil.isLiteral(literal, true)); // CAST(RexLiteral as type) is considered as a Literal assertSame(true, RexUtil.isLiteral(castLiteral, true)); // CAST(CAST(RexLiteral as type) as type) is NOT considered as a Literal assertSame(false, RexUtil.isLiteral(castCastLiteral, true)); } }
/** Tests {@link org.apache.calcite.rex.RexUtil#isLiteral(RexNode, boolean)}, * which was added to enhance Calcite's public API * <a href="https://issues.apache.org/jira/browse/CALCITE-1219">[CALCITE-1219] * Add a method to SqlOperatorBinding to determine whether operand is a * literal</a>. */ @Test public void testRexNodeLiteral() { final RexNode literal = rexBuilder.makeZeroLiteral( integerDataType); final RexNode castLiteral = rexBuilder.makeCall( integerDataType, SqlStdOperatorTable.CAST, Lists.newArrayList(literal)); final RexNode castCastLiteral = rexBuilder.makeCall( integerDataType, SqlStdOperatorTable.CAST, Lists.newArrayList(castLiteral)); // RexLiteral is considered as a Literal assertSame(true, RexUtil.isLiteral(literal, true)); // CAST(RexLiteral as type) is considered as a Literal assertSame(true, RexUtil.isLiteral(castLiteral, true)); // CAST(CAST(RexLiteral as type) as type) is NOT considered as a Literal assertSame(false, RexUtil.isLiteral(castCastLiteral, true)); } }