@Override public String visitLiteral(RexLiteral rexLiteral) { Object v = rexLiteral.getValue(); RelDataType ty = rexLiteral.getType(); switch (rexLiteral.getTypeName()) { case BOOLEAN: return v.toString(); case CHAR: return CompilerUtil.escapeJavaString(((NlsString) v).getValue(), true); case NULL: return "((" + ((Class<?>) typeFactory.getJavaClass(ty)).getCanonicalName() + ")null)"; case DOUBLE: case BIGINT: case DECIMAL: switch (ty.getSqlTypeName()) { case TINYINT: case SMALLINT: case INTEGER: return Long.toString(((BigDecimal) v).longValueExact()); case BIGINT: return Long.toString(((BigDecimal) v).longValueExact()) + 'L'; case DECIMAL: case FLOAT: case REAL: case DOUBLE: return Util.toScientificNotation((BigDecimal) v); } break; default: throw new UnsupportedOperationException(); } return null; }
public Double getMaxRowCount(Sort rel, RelMetadataQuery mq) { Double rowCount = mq.getMaxRowCount(rel.getInput()); if (rowCount == null) { rowCount = Double.POSITIVE_INFINITY; } final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset); rowCount = Math.max(rowCount - offset, 0D); if (rel.fetch != null) { final int limit = RexLiteral.intValue(rel.fetch); if (limit < rowCount) { return (double) limit; } } return rowCount; }
/** * Infers predicates for a project. * * <ol> * <li>create a mapping from input to projection. Map only positions that directly reference an * input column. * <li>Expressions that only contain above columns are retained in the Project's pullExpressions * list. * <li>For e.g. expression 'a + e = 9' below will not be pulled up because 'e' is not in the * projection list. * <pre> * childPullUpExprs: {a > 7, b + c < 10, a + e = 9} * projectionExprs: {a, b, c, e / 2} * projectionPullupExprs: {a > 7, b + c < 10} * </pre> * </ol> */ public RelOptPredicateList getPredicates(Project project) { RelNode child = project.getInput(); final RexBuilder rexBuilder = project.getCluster().getRexBuilder(); RelOptPredicateList childInfo = RelMetadataQuery.getPulledUpPredicates(child); List<RexNode> projectPullUpPredicates = new ArrayList<RexNode>(); ImmutableBitSet.Builder columnsMappedBuilder = ImmutableBitSet.builder(); Mapping m = Mappings.create( MappingType.PARTIAL_FUNCTION, child.getRowType().getFieldCount(), project.getRowType().getFieldCount()); for (Ord<RexNode> o : Ord.zip(project.getProjects())) { if (o.e instanceof RexInputRef) { int sIdx = ((RexInputRef) o.e).getIndex(); m.set(sIdx, o.i); columnsMappedBuilder.set(sIdx); } } // Go over childPullUpPredicates. If a predicate only contains columns in // 'columnsMapped' construct a new predicate based on mapping. final ImmutableBitSet columnsMapped = columnsMappedBuilder.build(); for (RexNode r : childInfo.pulledUpPredicates) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r); if (columnsMapped.contains(rCols)) { r = r.accept(new RexPermuteInputsShuttle(m, child)); projectPullUpPredicates.add(r); } } // Project can also generate constants. We need to include them. for (Ord<RexNode> expr : Ord.zip(project.getProjects())) { if (RexLiteral.isNullLiteral(expr.e)) { projectPullUpPredicates.add( rexBuilder.makeCall( SqlStdOperatorTable.IS_NULL, rexBuilder.makeInputRef(project, expr.i))); } else if (RexUtil.isConstant(expr.e)) { final List<RexNode> args = ImmutableList.of(rexBuilder.makeInputRef(project, expr.i), expr.e); final SqlOperator op = args.get(0).getType().isNullable() || args.get(1).getType().isNullable() ? SqlStdOperatorTable.IS_NOT_DISTINCT_FROM : SqlStdOperatorTable.EQUALS; projectPullUpPredicates.add(rexBuilder.makeCall(op, args)); } } return RelOptPredicateList.of(projectPullUpPredicates); }
public Double getMaxRowCount(RelSubset rel, RelMetadataQuery mq) { // FIXME This is a short-term fix for [CALCITE-1018]. A complete // solution will come with [CALCITE-1048]. Util.discard(Bug.CALCITE_1048_FIXED); for (RelNode node : rel.getRels()) { if (node instanceof Sort) { Sort sort = (Sort) node; if (sort.fetch != null) { return (double) RexLiteral.intValue(sort.fetch); } } } return Double.POSITIVE_INFINITY; }
private void checkNotSatisfiable(RexNode e) { assertFalse(SubstitutionVisitor.mayBeSatisfiable(e)); final RexNode simple = SubstitutionVisitor.simplify(rexBuilder, e); assertFalse(RexLiteral.booleanValue(simple)); }
private static Object literalToString(RexLiteral literal) { return literal.getValue2(); }