org.apache.calcite.util.Util Java Examples

The following examples show how to use org.apache.calcite.util.Util. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DruidQuery.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  return Util.last(rels)
      .computeSelfCost(planner, mq)
      // Cost increases with the number of fields queried.
      // A plan returning 100 or more columns will have 2x the cost of a
      // plan returning 2 columns.
      // A plan where all extra columns are pruned will be preferred.
      .multiplyBy(
          RelMdUtil.linear(querySpec.fieldNames.size(), 2, 100, 1d, 2d))
      .multiplyBy(getQueryTypeCostMultiplier())
      // A Scan leaf filter is better than having filter spec if possible.
      .multiplyBy(rels.size() > 1 && rels.get(1) instanceof Filter ? 0.5 : 1.0)
      // a plan with sort pushed to druid is better than doing sort outside of druid
      .multiplyBy(Util.last(rels) instanceof Sort ? 0.1 : 1.0)
      .multiplyBy(getIntervalCostMultiplier());
}
 
Example #2
Source File: DruidRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final DruidQuery query = call.rel(1);
  if (!DruidQuery.isValidSignature(query.signature() + 'l')) {
    return;
  }
  // Either it is:
  // - a pure limit above a query of type scan
  // - a sort and limit on a dimension/metric part of the druid group by query
  if (sort.offset != null && RexLiteral.intValue(sort.offset) != 0) {
    // offset not supported by Druid
    return;
  }
  if (query.getQueryType() == QueryType.SCAN && !RelOptUtil.isPureLimit(sort)) {
    return;
  }

  final RelNode newSort = sort
      .copy(sort.getTraitSet(), ImmutableList.of(Util.last(query.rels)));
  call.transformTo(DruidQuery.extendQuery(query, newSort));
}
 
Example #3
Source File: MaterializedViewRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
protected RexNode shuttleReferences(final RexBuilder rexBuilder,
    final RexNode node, final Mapping mapping) {
  try {
    RexShuttle visitor =
        new RexShuttle() {
          @Override public RexNode visitInputRef(RexInputRef inputRef) {
            int pos = mapping.getTargetOpt(inputRef.getIndex());
            if (pos != -1) {
              // Found it
              return rexBuilder.makeInputRef(inputRef.getType(), pos);
            }
            throw Util.FoundOne.NULL;
          }
        };
    return visitor.apply(node);
  } catch (Util.FoundOne ex) {
    Util.swallow(ex, null);
    return null;
  }
}
 
Example #4
Source File: SubQueryRemoveRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet, RelOptUtil.Logic logic,
        RelBuilder builder) {
    builder.push(e.getRel());

    builder.project(builder.alias(builder.literal(true), "i"));
    switch (logic) {
    case TRUE:
        // Handles queries with single EXISTS in filter condition:
        // select e.deptno from emp as e
        // where exists (select deptno from emp)
        builder.aggregate(builder.groupKey(0));
        builder.as("dt");
        builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
        return builder.literal(true);
    default:
        builder.distinct();
    }

    builder.as("dt");

    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

    return builder.isNotNull(Util.last(builder.fields()));
}
 
Example #5
Source File: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar, RexNode operand) {
    final TimestampString ts;
    final int p;
    switch (operand.getType().getSqlTypeName()) {
    case TIMESTAMP:
        ts = TimestampString.fromCalendarFields(calendar);
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampLiteral(ts, p);
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        ts = TimestampString.fromCalendarFields(calendar);
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        final TimestampString localTs = new TimestampWithTimeZoneString(ts, tz)
                .withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
    case DATE:
        final DateString d = DateString.fromCalendarFields(calendar);
        return rexBuilder.makeDateLiteral(d);
    default:
        throw Util.unexpected(operand.getType().getSqlTypeName());
    }
}
 
Example #6
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any
 * {@link org.apache.calcite.rex.RexFieldAccess} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsFieldAccess(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitFieldAccess(RexFieldAccess fieldAccess) {
            throw new Util.FoundOne(fieldAccess);
          }
        };
    node.accept(visitor);
    return false;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return true;
  }
}
 
Example #7
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@link org.apache.calcite.schema.CalciteSchema.TableEntry} in a
 * given catalog reader whose table has the given name, possibly qualified.
 *
 * <p>Uses the case-sensitivity policy of the specified catalog reader.
 *
 * <p>If not found, returns null.
 *
 * @param catalogReader accessor to the table metadata
 * @param names Name of table, may be qualified or fully-qualified
 *
 * @return TableEntry with a table with the given name, or null
 */
public static CalciteSchema.TableEntry getTableEntry(
    SqlValidatorCatalogReader catalogReader, List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  for (List<String> schemaPath : catalogReader.getSchemaPaths()) {
    CalciteSchema schema =
        getSchema(catalogReader.getRootSchema(),
            Iterables.concat(schemaPath, Util.skipLast(names)),
            catalogReader.nameMatcher());
    if (schema == null) {
      continue;
    }
    CalciteSchema.TableEntry entry =
        getTableEntryFrom(schema, Util.last(names),
            catalogReader.nameMatcher().isCaseSensitive());
    if (entry != null) {
      return entry;
    }
  }
  return null;
}
 
Example #8
Source File: MongoTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes an "aggregate" operation on the underlying collection.
 *
 * <p>For example:
 * <code>zipsTable.aggregate(
 * "{$filter: {state: 'OR'}",
 * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}")
 * </code></p>
 *
 * @param mongoDb MongoDB connection
 * @param fields List of fields to project; or null to return map
 * @param operations One or more JSON strings
 * @return Enumerator of results
 */
private Enumerable<Object> aggregate(final MongoDatabase mongoDb,
    final List<Map.Entry<String, Class>> fields,
    final List<String> operations) {
  final List<Bson> list = new ArrayList<>();
  for (String operation : operations) {
    list.add(BsonDocument.parse(operation));
  }
  final Function1<Document, Object> getter =
      MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      final Iterator<Document> resultIterator;
      try {
        resultIterator = mongoDb.getCollection(collectionName)
            .aggregate(list).iterator();
      } catch (Exception e) {
        throw new RuntimeException("While running MongoDB query "
            + Util.toString(operations, "[", ",\n", "]"), e);
      }
      return new MongoEnumerator(resultIterator, getter);
    }
  };
}
 
Example #9
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Helper for {@link #getColumnStrategies()}. */
public static List<ColumnStrategy> columnStrategies(final RelOptTable table) {
    final int fieldCount = table.getRowType().getFieldCount();
    final InitializerExpressionFactory ief = Util.first(table.unwrap(InitializerExpressionFactory.class),
            NullInitializerExpressionFactory.INSTANCE);
    return new AbstractList<ColumnStrategy>() {
        @Override
        public int size() {
            return fieldCount;
        }

        @Override
        public ColumnStrategy get(int index) {
            return ief.generationStrategy(table, index);
        }
    };
}
 
Example #10
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexTableInputRef} nodes.
 *
 * @param node a RexNode tree
 * @return first such node found or null if it there is no such node
 */
public static RexTableInputRef containsTableInputRef(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitTableInputRef(RexTableInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexTableInputRef) e.getNode();
  }
}
 
Example #11
Source File: CsvTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static void collect(List<String> result, ResultSet resultSet)
    throws SQLException {
  final StringBuilder buf = new StringBuilder();
  while (resultSet.next()) {
    buf.setLength(0);
    int n = resultSet.getMetaData().getColumnCount();
    String sep = "";
    for (int i = 1; i <= n; i++) {
      buf.append(sep)
          .append(resultSet.getMetaData().getColumnLabel(i))
          .append("=")
          .append(resultSet.getString(i));
      sep = "; ";
    }
    result.add(Util.toLinux(buf.toString()));
  }
}
 
Example #12
Source File: RelFactories.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static @Nonnull Struct fromContext(Context context) {
  Struct struct = context.unwrap(Struct.class);
  if (struct != null) {
    return struct;
  }
  return new Struct(
      Util.first(context.unwrap(FilterFactory.class),
          DEFAULT_FILTER_FACTORY),
      Util.first(context.unwrap(ProjectFactory.class),
          DEFAULT_PROJECT_FACTORY),
      Util.first(context.unwrap(AggregateFactory.class),
          DEFAULT_AGGREGATE_FACTORY),
      Util.first(context.unwrap(SortFactory.class),
          DEFAULT_SORT_FACTORY),
      Util.first(context.unwrap(ExchangeFactory.class),
          DEFAULT_EXCHANGE_FACTORY),
      Util.first(context.unwrap(SortExchangeFactory.class),
          DEFAULT_SORT_EXCHANGE_FACTORY),
      Util.first(context.unwrap(SetOpFactory.class),
          DEFAULT_SET_OP_FACTORY),
      Util.first(context.unwrap(JoinFactory.class),
          DEFAULT_JOIN_FACTORY),
      Util.first(context.unwrap(CorrelateFactory.class),
          DEFAULT_CORRELATE_FACTORY),
      Util.first(context.unwrap(ValuesFactory.class),
          DEFAULT_VALUES_FACTORY),
      Util.first(context.unwrap(TableScanFactory.class),
          DEFAULT_TABLE_SCAN_FACTORY),
      Util.first(context.unwrap(TableFunctionScanFactory.class),
          DEFAULT_TABLE_FUNCTION_SCAN_FACTORY),
      Util.first(context.unwrap(SnapshotFactory.class),
          DEFAULT_SNAPSHOT_FACTORY),
      Util.first(context.unwrap(MatchFactory.class),
          DEFAULT_MATCH_FACTORY),
      Util.first(context.unwrap(SpoolFactory.class),
          DEFAULT_SPOOL_FACTORY),
      Util.first(context.unwrap(RepeatUnionFactory.class),
          DEFAULT_REPEAT_UNION_FACTORY));
}
 
Example #13
Source File: ListScope.java    From Bats with Apache License 2.0 5 votes vote down vote up
private ScopeChild findChild(List<String> names,
    SqlNameMatcher nameMatcher) {
  for (ScopeChild child : children) {
    String lastName = Util.last(names);
    if (child.name != null) {
      if (!nameMatcher.matches(child.name, lastName)) {
        // Alias does not match last segment. Don't consider the
        // fully-qualified name. E.g.
        //    SELECT sales.emp.name FROM sales.emp AS otherAlias
        continue;
      }
      if (names.size() == 1) {
        return child;
      }
    }

    // Look up the 2 tables independently, in case one is qualified with
    // catalog & schema and the other is not.
    final SqlValidatorTable table = child.namespace.getTable();
    if (table != null) {
      final ResolvedImpl resolved = new ResolvedImpl();
      resolveTable(names, nameMatcher, Path.EMPTY, resolved);
      if (resolved.count() == 1
          && resolved.only().remainingNames.isEmpty()
          && resolved.only().namespace instanceof TableNamespace
          && resolved.only().namespace.getTable().getQualifiedName().equals(
              table.getQualifiedName())) {
        return child;
      }
    }
  }
  return null;
}
 
Example #14
Source File: ProjectCorrelateTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected RelNode visitChild(RelNode parent, int i, RelNode child) {
  if (child instanceof HepRelVertex) {
    child = ((HepRelVertex) child).getCurrentRel();
  } else if (child instanceof RelSubset) {
    RelSubset subset = (RelSubset) child;
    child = Util.first(subset.getBest(), subset.getOriginal());
  }
  return super.visitChild(parent, i, child).accept(rexVisitor);
}
 
Example #15
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode toCnf(RexNode rex) {
  try {
    this.currentCount = 0;
    return toCnf2(rex);
  } catch (OverflowError e) {
    Util.swallow(e, null);
    return rex;
  }
}
 
Example #16
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Project project = call.rel(1);
  final LogicalDelta newDelta = LogicalDelta.create(project.getInput());
  final LogicalProject newProject =
      LogicalProject.create(newDelta,
          project.getHints(),
          project.getProjects(),
          project.getRowType().getFieldNames());
  call.transformTo(newProject);
}
 
Example #17
Source File: SqlAdvisorValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the parent class method and masks Farrago exception thrown.
 */
protected void validateHavingClause(SqlSelect select) {
  try {
    super.validateHavingClause(select);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example #18
Source File: Sort.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  // Higher cost if rows are wider discourages pushing a project through a
  // sort.
  final double rowCount = mq.getRowCount(this);
  final double bytesPerRow = getRowType().getFieldCount() * 4;
  final double cpu = Util.nLogN(rowCount) * bytesPerRow;
  return planner.getCostFactory().makeCost(rowCount, cpu, 0);
}
 
Example #19
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
Enumerable<MetaSchema> schemas(final String catalog) {
  return Linq4j.asEnumerable(
      getConnection().rootSchema.getSubSchemaMap().values())
      .select((Function1<CalciteSchema, MetaSchema>) calciteSchema ->
          new CalciteMetaSchema(calciteSchema, catalog,
              calciteSchema.getName()))
      .orderBy((Function1<MetaSchema, Comparable>) metaSchema ->
          (Comparable) FlatLists.of(Util.first(metaSchema.tableCatalog, ""),
              metaSchema.tableSchem));
}
 
Example #20
Source File: SubstitutionVisitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SubstitutionVisitor(RelNode target_, RelNode query_, ImmutableList<UnifyRule> rules,
        RelBuilderFactory relBuilderFactory) {
    this.cluster = target_.getCluster();
    final RexExecutor executor = Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR);
    final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
    this.simplify = new RexSimplify(cluster.getRexBuilder(), predicates, executor);
    this.rules = rules;
    this.query = Holder.of(MutableRels.toMutable(query_));
    this.target = MutableRels.toMutable(target_);
    this.relBuilder = relBuilderFactory.create(cluster, null);
    final Set<MutableRel> parents = Sets.newIdentityHashSet();
    final List<MutableRel> allNodes = new ArrayList<>();
    final MutableRelVisitor visitor = new MutableRelVisitor() {
        @Override
        public void visit(MutableRel node) {
            parents.add(node.getParent());
            allNodes.add(node);
            super.visit(node);
        }
    };
    visitor.go(target);

    // Populate the list of leaves in the tree under "target".
    // Leaves are all nodes that are not parents.
    // For determinism, it is important that the list is in scan order.
    allNodes.removeAll(parents);
    targetLeaves = ImmutableList.copyOf(allNodes);

    allNodes.clear();
    parents.clear();
    visitor.go(query);
    allNodes.removeAll(parents);
    queryLeaves = ImmutableList.copyOf(allNodes);
}
 
Example #21
Source File: ModelHandler.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void visit(JsonView jsonView) {
  try {
    checkRequiredAttributes(jsonView, "name");
    final SchemaPlus schema = currentMutableSchema("view");
    final List<String> path = Util.first(jsonView.path, currentSchemaPath());
    final List<String> viewPath = ImmutableList.<String>builder().addAll(path)
        .add(jsonView.name).build();
    schema.add(jsonView.name,
        ViewTable.viewMacro(schema, jsonView.getSql(), path, viewPath,
            jsonView.modifiable));
  } catch (Exception e) {
    throw new RuntimeException("Error instantiating " + jsonView, e);
  }
}
 
Example #22
Source File: JaninoRelMetadataProvider.java    From Bats with Apache License 2.0 5 votes vote down vote up
synchronized <M extends Metadata, H extends MetadataHandler<M>> H create(
    MetadataDef<M> def) {
  try {
    final Key key = new Key((MetadataDef) def, provider,
        ImmutableList.copyOf(ALL_RELS));
    //noinspection unchecked
    return (H) HANDLERS.get(key);
  } catch (UncheckedExecutionException | ExecutionException e) {
    Util.throwIfUnchecked(e.getCause());
    throw new RuntimeException(e.getCause());
  }
}
 
Example #23
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorLtRewrite() {
  final Calendar c = Util.calendar();

  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.lt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2011-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.lt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2010-01-01 00:00:00)"));
}
 
Example #24
Source File: SqlIntervalQualifier.java    From calcite with Apache License 2.0 5 votes vote down vote up
private String stripLeadingSign(String value) {
  String unsignedValue = value;

  if (!Util.isNullOrEmpty(value)) {
    if (('-' == value.charAt(0)) || ('+' == value.charAt(0))) {
      unsignedValue = value.substring(1);
    }
  }

  return unsignedValue;
}
 
Example #25
Source File: ProfilerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void add(List<Comparable> row) {
  if (space.columnOrdinals.equals(OF)) {
    Util.discard(0);
  }
  int nullCountThisRow = 0;
  buf.clear();
  for (int columnOrdinal : columnOrdinals) {
    final Comparable value = row.get(columnOrdinal);
    if (value == NullSentinel.INSTANCE) {
      if (nullCountThisRow++ == 0) {
        nullCount++;
      }
      buf.put((byte) 0);
    } else if (value instanceof String) {
      buf.put((byte) 1)
          .put(((String) value).getBytes(StandardCharsets.UTF_8));
    } else if (value instanceof Double) {
      buf.put((byte) 2).putDouble((Double) value);
    } else if (value instanceof Float) {
      buf.put((byte) 3).putFloat((Float) value);
    } else if (value instanceof Long) {
      buf.put((byte) 4).putLong((Long) value);
    } else if (value instanceof Integer) {
      buf.put((byte) 5).putInt((Integer) value);
    } else if (value instanceof Boolean) {
      buf.put((Boolean) value ? (byte) 6 : (byte) 7);
    } else {
      buf.put((byte) 8)
          .put(value.toString().getBytes(StandardCharsets.UTF_8));
    }
  }
  sketch.update(Arrays.copyOf(buf.array(), buf.position()));
}
 
Example #26
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void validateLiteralAsDouble(SqlLiteral literal) {
	BigDecimal bd = (BigDecimal) literal.getValue();
	double d = bd.doubleValue();
	if (Double.isInfinite(d) || Double.isNaN(d)) {
		// overflow
		throw newValidationError(literal,
			RESOURCE.numberLiteralOutOfRange(Util.toScientificNotation(bd)));
	}

	// REVIEW jvs 4-Aug-2004:  what about underflow?
}
 
Example #27
Source File: RexImplicationCheckerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testSimpleTime() {
  final Fixture f = new Fixture();
  final TimeString t = TimeString.fromCalendarFields(Util.calendar());
  final RexNode node1 = f.lt(f.t, f.timeLiteral(t));
  final RexNode node2 = f.le(f.t, f.timeLiteral(t));
  f.checkImplies(node1, node2);
  f.checkNotImplies(node2, node1);
}
 
Example #28
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void checkRollUpInUsing(SqlIdentifier identifier, SqlNode leftOrRight) {
	leftOrRight = stripAs(leftOrRight);
	// if it's not a SqlIdentifier then that's fine, it'll be validated somewhere else.
	if (leftOrRight instanceof SqlIdentifier) {
		SqlIdentifier from = (SqlIdentifier) leftOrRight;
		Table table = findTable(catalogReader.getRootSchema(), Util.last(from.names),
			catalogReader.nameMatcher().isCaseSensitive());
		String name = Util.last(identifier.names);

		if (table != null && table.isRolledUp(name)) {
			throw newValidationError(identifier, RESOURCE.rolledUpNotAllowed(name, "USING"));
		}
	}
}
 
Example #29
Source File: FilterProjectTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Simplifies the filter condition using a simplifier created by the
 * information in the current call.
 *
 * <p>This method is an attempt to replicate the simplification behavior of
 * {@link RelBuilder#filter(RexNode...)} which cannot be used in the case of
 * copying nodes. The main difference with the behavior of that method is that
 * it does not drop entirely the filter if the condition is always false.
 */
private RexNode simplifyFilterCondition(RexNode condition, RelOptRuleCall call) {
  final RexBuilder xBuilder = call.builder().getRexBuilder();
  final RexExecutor executor =
      Util.first(call.getPlanner().getContext().unwrap(RexExecutor.class),
          Util.first(call.getPlanner().getExecutor(), RexUtil.EXECUTOR));
  // unknownAsFalse => true since in the WHERE clause:
  // 1>null evaluates to unknown and WHERE unknown behaves exactly like WHERE false
  RexSimplify simplifier =
      new RexSimplify(xBuilder, RelOptPredicateList.EMPTY, executor);
  return RexUtil.removeNullabilityCast(
      xBuilder.getTypeFactory(), simplifier.simplifyUnknownAsFalse(condition));
}
 
Example #30
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether a {@link Join} contains a sub-query. */
public static boolean containsSubQuery(Join join) {
    try {
        join.getCondition().accept(INSTANCE);
        return false;
    } catch (Util.FoundOne e) {
        return true;
    }
}