Java源码示例:org.apache.calcite.rex.RexExecutorImpl

示例1
/** 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);
}
 
示例2
/**
 * 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;
  }
}
 
示例3
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];
}
 
示例4
public RexImplicationChecker(
    RexBuilder builder,
    RexExecutorImpl executor,
    RelDataType rowType) {
  this.builder = Objects.requireNonNull(builder);
  this.executor = Objects.requireNonNull(executor);
  this.rowType = Objects.requireNonNull(rowType);
}
 
示例5
/**
 * 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);
}
 
示例6
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();
}
 
示例7
/** 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);
}
 
示例8
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()));
}
 
示例9
/**
 * 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);
}
 
示例10
/** Creates MockRelOptPlanner. */
public MockRelOptPlanner(Context context) {
  super(RelOptCostImpl.FACTORY, context);
  setExecutor(new RexExecutorImpl(Schemas.createDataContext(null, null)));
}
 
示例11
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);
}