org.apache.calcite.rex.RexExecutorImpl Java Examples

The following examples show how to use org.apache.calcite.rex.RexExecutorImpl. 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: SubstitutionVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Returns if one rel is weaker than another. */
protected boolean isWeaker(MutableRel rel0, MutableRel rel) {
    if (rel0 == rel || equivalents.get(rel0).contains(rel)) {
        return false;
    }

    if (!(rel0 instanceof MutableFilter) || !(rel instanceof MutableFilter)) {
        return false;
    }

    if (!rel.rowType.equals(rel0.rowType)) {
        return false;
    }

    final MutableRel rel0input = ((MutableFilter) rel0).getInput();
    final MutableRel relinput = ((MutableFilter) rel).getInput();
    if (rel0input != relinput && !equivalents.get(rel0input).contains(relinput)) {
        return false;
    }

    RexExecutorImpl rexImpl = (RexExecutorImpl) (rel.cluster.getPlanner().getExecutor());
    RexImplicationChecker rexImplicationChecker = new RexImplicationChecker(rel.cluster.getRexBuilder(), rexImpl,
            rel.rowType);

    return rexImplicationChecker.implies(((MutableFilter) rel0).condition, ((MutableFilter) rel).condition);
}
 
Example #2
Source File: FilterAggStarRule.java    From quark with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if filter satisfies the lattice filter
 * i.e., it needs data captured by the lattice
 */
private boolean isLatticeFilterSatisfied(final RelOptLattice lattice,
                                 final Filter filter,
                                 final StarTable.StarTableScan scan) {
  if (lattice.lattice.filter == null) {
    return true;
  }
  RexExecutorImpl rexImpl =
      (RexExecutorImpl) (scan.getCluster().getPlanner().getExecutor());
  RexImplicationChecker solver =
      new RexImplicationChecker(scan.getCluster().getRexBuilder(),
          rexImpl, scan.getRowType());
  try {
    return solver.implies(filter.getCondition(), lattice.lattice.filter);
  } catch (Exception e) {
    LOG.debug("Exception thrown while solving "
        + filter.getCondition()
        + "  =>  "
        + lattice.lattice.filter);
    return false;
  }
}
 
Example #3
Source File: RexImplicationChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean isSatisfiable(RexNode second, DataContext dataValues) {
  if (dataValues == null) {
    return false;
  }

  ImmutableList<RexNode> constExps = ImmutableList.of(second);
  final RexExecutable exec = RexExecutorImpl.getExecutable(builder, constExps, rowType);

  Object[] result;
  exec.setDataContext(dataValues);
  try {
    result = exec.execute();
  } catch (Exception e) {
    // TODO: CheckSupport should not allow this exception to be thrown
    // Need to monitor it and handle all the cases raising them.
    LOGGER.warn("Exception thrown while checking if => {}: {}", second, e.getMessage());
    return false;
  }
  return result != null
      && result.length == 1
      && result[0] instanceof Boolean
      && (Boolean) result[0];
}
 
Example #4
Source File: RexImplicationChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RexImplicationChecker(
    RexBuilder builder,
    RexExecutorImpl executor,
    RelDataType rowType) {
  this.builder = Objects.requireNonNull(builder);
  this.executor = Objects.requireNonNull(executor);
  this.rowType = Objects.requireNonNull(rowType);
}
 
Example #5
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Determines whether any of the fields in a given relational expression may
 * contain null values, taking into account constraints on the field types and
 * also deduced predicates.
 *
 * <p>The method is cautious: It may sometimes return {@code true} when the
 * actual answer is {@code false}. In particular, it does this when there
 * is no executor, or the executor is not a sub-class of
 * {@link RexExecutorImpl}.
 */
private static boolean containsNullableFields(RelNode r) {
    final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
    final RelDataType rowType = r.getRowType();
    final List<RexNode> list = new ArrayList<>();
    final RelMetadataQuery mq = r.getCluster().getMetadataQuery();
    for (RelDataTypeField field : rowType.getFieldList()) {
        if (field.getType().isNullable()) {
            list.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                    rexBuilder.makeInputRef(field.getType(), field.getIndex())));
        }
    }
    if (list.isEmpty()) {
        // All columns are declared NOT NULL.
        return false;
    }
    final RelOptPredicateList predicates = mq.getPulledUpPredicates(r);
    if (predicates.pulledUpPredicates.isEmpty()) {
        // We have no predicates, so cannot deduce that any of the fields
        // declared NULL are really NOT NULL.
        return true;
    }
    final RexExecutor executor = r.getCluster().getPlanner().getExecutor();
    if (!(executor instanceof RexExecutorImpl)) {
        // Cannot proceed without an executor.
        return true;
    }
    final RexImplicationChecker checker = new RexImplicationChecker(rexBuilder, (RexExecutorImpl) executor,
            rowType);
    final RexNode first = RexUtil.composeConjunction(rexBuilder, predicates.pulledUpPredicates);
    final RexNode second = RexUtil.composeConjunction(rexBuilder, list);
    // Suppose we have EMP(empno INT NOT NULL, mgr INT),
    // and predicates [empno > 0, mgr > 0].
    // We make first: "empno > 0 AND mgr > 0"
    // and second: "mgr IS NOT NULL"
    // and ask whether first implies second.
    // It does, so we have no nullable columns.
    return !checker.implies(first, second);
}
 
Example #6
Source File: SqlWorker.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
                   RelTraitSet requiredOutputTraits,
                   List<RelOptMaterialization> materializations,
                   List<RelOptLattice> lattices) {
  planner.clear();

  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  //((VolcanoPlanner) planner).registerAbstractRelationalRules();

  RelOptUtil.registerAbstractRels(planner);
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }

  planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE);
  planner.addRule(ProjectTableScanRule.INSTANCE);
  planner.addRule(ProjectTableScanRule.INTERPRETER);
  planner.addRule(EnumerableInterpreterRule.INSTANCE);

  final CalciteSchema rootSchema = CalciteSchema.from(context.getRootSchema());
  planner.setExecutor(new RexExecutorImpl(null));
  planner.setRoot(rel);

  MaterializationService.setThreadLocal(materializationService);
  plannerHolder.setPlanner(planner);
  populateMaterializationsAndLattice(plannerHolder, rootSchema);
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
    planner.setRoot(rel);
  }

  RelOptPlanner planner2 = planner.chooseDelegate();
  return planner2.findBestExp();
}
 
Example #7
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Check if condition cond0 implies cond1. */
private static boolean implies(
    RelOptCluster cluster, RexNode cond0, RexNode cond1, RelDataType rowType) {
  RexExecutorImpl rexImpl =
      (RexExecutorImpl) (cluster.getPlanner().getExecutor());
  RexImplicationChecker rexImplicationChecker =
      new RexImplicationChecker(cluster.getRexBuilder(), rexImpl, rowType);
  return rexImplicationChecker.implies(cond0, cond1);
}
 
Example #8
Source File: ConstExecutor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public ConstExecutor(FunctionImplementationRegistry funcImplReg, FunctionContext udfUtilities, PlannerSettings plannerSettings) {
  this.funcImplReg = funcImplReg;
  this.udfUtilities = udfUtilities;
  this.plannerSettings = plannerSettings;
  this.calciteExecutor = new RexExecutorImpl(new DremioDataContext(udfUtilities.getContextInformation()));
}
 
Example #9
Source File: RelOptUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether any of the fields in a given relational expression may
 * contain null values, taking into account constraints on the field types and
 * also deduced predicates.
 *
 * <p>The method is cautious: It may sometimes return {@code true} when the
 * actual answer is {@code false}. In particular, it does this when there
 * is no executor, or the executor is not a sub-class of
 * {@link RexExecutorImpl}.
 */
private static boolean containsNullableFields(RelNode r) {
  final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
  final RelDataType rowType = r.getRowType();
  final List<RexNode> list = new ArrayList<>();
  final RelMetadataQuery mq = r.getCluster().getMetadataQuery();
  for (RelDataTypeField field : rowType.getFieldList()) {
    if (field.getType().isNullable()) {
      list.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(field.getType(), field.getIndex())));
    }
  }
  if (list.isEmpty()) {
    // All columns are declared NOT NULL.
    return false;
  }
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(r);
  if (predicates.pulledUpPredicates.isEmpty()) {
    // We have no predicates, so cannot deduce that any of the fields
    // declared NULL are really NOT NULL.
    return true;
  }
  final RexExecutor executor = r.getCluster().getPlanner().getExecutor();
  if (!(executor instanceof RexExecutorImpl)) {
    // Cannot proceed without an executor.
    return true;
  }
  final RexImplicationChecker checker =
      new RexImplicationChecker(rexBuilder, executor,
          rowType);
  final RexNode first =
      RexUtil.composeConjunction(rexBuilder, predicates.pulledUpPredicates);
  final RexNode second = RexUtil.composeConjunction(rexBuilder, list);
  // Suppose we have EMP(empno INT NOT NULL, mgr INT),
  // and predicates [empno > 0, mgr > 0].
  // We make first: "empno > 0 AND mgr > 0"
  // and second: "mgr IS NOT NULL"
  // and ask whether first implies second.
  // It does, so we have no nullable columns.
  return !checker.implies(first, second);
}
 
Example #10
Source File: MockRelOptPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates MockRelOptPlanner. */
public MockRelOptPlanner(Context context) {
  super(RelOptCostImpl.FACTORY, context);
  setExecutor(new RexExecutorImpl(Schemas.createDataContext(null, null)));
}
 
Example #11
Source File: RexImplicationCheckerTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Fixture() {
  typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  rexBuilder = new RexBuilder(typeFactory);
  boolRelDataType = typeFactory.createJavaType(Boolean.class);
  intRelDataType = typeFactory.createJavaType(Integer.class);
  decRelDataType = typeFactory.createJavaType(Double.class);
  longRelDataType = typeFactory.createJavaType(Long.class);
  shortDataType = typeFactory.createJavaType(Short.class);
  byteDataType = typeFactory.createJavaType(Byte.class);
  floatDataType = typeFactory.createJavaType(Float.class);
  charDataType = typeFactory.createJavaType(Character.class);
  dateDataType = typeFactory.createJavaType(Date.class);
  timestampDataType = typeFactory.createJavaType(Timestamp.class);
  timeDataType = typeFactory.createJavaType(Time.class);
  stringDataType = typeFactory.createJavaType(String.class);

  bl = ref(0, this.boolRelDataType);
  i = ref(1, intRelDataType);
  dec = ref(2, decRelDataType);
  lg = ref(3, longRelDataType);
  sh = ref(4, shortDataType);
  by = ref(5, byteDataType);
  fl = ref(6, floatDataType);
  ch = ref(7, charDataType);
  d = ref(8, dateDataType);
  ts = ref(9, timestampDataType);
  t = ref(10, timeDataType);
  str = ref(11, stringDataType);

  rowType = typeFactory.builder()
      .add("bool", this.boolRelDataType)
      .add("int", intRelDataType)
      .add("dec", decRelDataType)
      .add("long", longRelDataType)
      .add("short", shortDataType)
      .add("byte", byteDataType)
      .add("float", floatDataType)
      .add("char", charDataType)
      .add("date", dateDataType)
      .add("timestamp", timestampDataType)
      .add("time", timeDataType)
      .add("string", stringDataType)
      .build();

  executor = Frameworks.withPrepare(
      (cluster, relOptSchema, rootSchema, statement) ->
          new RexExecutorImpl(
              Schemas.createDataContext(statement.getConnection(),
                  rootSchema)));
  simplify =
      new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor)
          .withParanoid(true);
  checker = new RexImplicationChecker(rexBuilder, executor, rowType);
}