org.apache.calcite.rel.metadata.RelMetadataQuery Java Examples

The following examples show how to use org.apache.calcite.rel.metadata.RelMetadataQuery. 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: AggregateRel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery relMetadataQuery) {
  for (AggregateCall aggCall : getAggCallList()) {
    // For avg, stddev_pop, stddev_samp, var_pop and var_samp, the ReduceAggregatesRule is supposed
    // to convert them to use sum and count. Here, we make the cost of the original functions high
    // enough such that the planner does not choose them and instead chooses the rewritten functions.
    if (aggCall.getAggregation().getKind() == SqlKind.AVG
        || aggCall.getAggregation().getKind() == SqlKind.STDDEV_SAMP
        || aggCall.getAggregation().getKind() == SqlKind.STDDEV_POP
        || aggCall.getAggregation().getKind() == SqlKind.VAR_POP
        || aggCall.getAggregation().getKind() == SqlKind.VAR_SAMP) {
      return planner.getCostFactory().makeHugeCost();
    }
  }

  final double rowCount = relMetadataQuery.getRowCount(this);
  final double childRowCount = relMetadataQuery.getRowCount(this.getInput());
  // Aggregates with more aggregate functions cost a bit more
  float multiplier = 1f + aggCalls.size() * 0.125f;
  return ((Factory) planner.getCostFactory()).makeCost(rowCount,childRowCount * multiplier * DremioCost.FUNC_CPU_COST, 0, 0);
}
 
Example #2
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Match}'s collation.
 */
public static List<RelCollation> match(RelMetadataQuery mq, RelNode input,
		RelDataType rowType,
		RexNode pattern,
		boolean strictStart,
		boolean strictEnd,
		Map<String, RexNode> patternDefinitions,
		Map<String, RexNode> measures,
		RexNode after,
		Map<String, ? extends SortedSet<String>> subsets,
		boolean allRows,
		ImmutableBitSet partitionKeys,
		RelCollation orderKeys,
		RexNode interval) {
	return mq.collations(input);
}
 
Example #3
Source File: EnumerableCorrelate.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates an EnumerableCorrelate. */
public static EnumerableCorrelate create(
    RelNode left,
    RelNode right,
    CorrelationId correlationId,
    ImmutableBitSet requiredColumns,
    JoinRelType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.enumerableCorrelate(mq, left, right, joinType));
  return new EnumerableCorrelate(
      cluster,
      traitSet,
      left,
      right,
      correlationId,
      requiredColumns,
      joinType);
}
 
Example #4
Source File: DrillScanRel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(final RelOptPlanner planner, RelMetadataQuery mq) {
  final ScanStats stats = getGroupScan().getScanStats(settings);
  int columnCount = getRowType().getFieldCount();
  double ioCost = 0;
  boolean isStarQuery = Utilities.isStarQuery(columns);

  if (isStarQuery) {
    columnCount = STAR_COLUMN_COST;
  }

  // double rowCount = RelMetadataQuery.getRowCount(this);
  double rowCount = stats.getRecordCount();
  if (rowCount < 1) {
    rowCount = 1;
  }

  if(PrelUtil.getSettings(getCluster()).useDefaultCosting()) {
    return planner.getCostFactory().makeCost(rowCount * columnCount, stats.getCpuCost(), stats.getDiskCost());
  }

  double cpuCost = rowCount * columnCount; // for now, assume cpu cost is proportional to row count and number of columns

  DrillCostFactory costFactory = (DrillCostFactory)planner.getCostFactory();
  return costFactory.makeCost(rowCount, cpuCost, ioCost, 0);
}
 
Example #5
Source File: NestedLoopJoinPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  if (PrelUtil.getSettings(getCluster()).useDefaultCosting()) {
    return super.computeSelfCost(planner, mq).multiplyBy(.1);
  }
  double leftRowCount = mq.getRowCount(this.getLeft());
  double rightRowCount = mq.getRowCount(this.getRight());
  double nljFactor = PrelUtil.getSettings(getCluster()).getNestedLoopJoinFactor();

  // cpu cost of evaluating each expression in join condition
  int exprNum = RelOptUtil.conjunctions(getCondition()).size() + RelOptUtil.disjunctions(getCondition()).size();
  double joinConditionCost = DrillCostBase.COMPARE_CPU_COST * exprNum;

  double cpuCost = joinConditionCost * (leftRowCount * rightRowCount) * nljFactor;

  DrillCostFactory costFactory = (DrillCostFactory) planner.getCostFactory();
  return costFactory.makeCost(leftRowCount * rightRowCount, cpuCost, 0, 0, 0);
}
 
Example #6
Source File: Aggregate.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  // REVIEW jvs 24-Aug-2008:  This is bogus, but no more bogus
  // than what's currently in Join.
  double rowCount = mq.getRowCount(this);
  // Aggregates with more aggregate functions cost a bit more
  float multiplier = 1f + (float) aggCalls.size() * 0.125f;
  for (AggregateCall aggCall : aggCalls) {
    if (aggCall.getAggregation().getName().equals("SUM")) {
      // Pretend that SUM costs a little bit more than $SUM0,
      // to make things deterministic.
      multiplier += 0.0125f;
    }
  }
  return planner.getCostFactory().makeCost(rowCount * multiplier, 0, 0);
}
 
Example #7
Source File: ReduceExpressionsUtil.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static int numReducibleExprs(Project project) {
  final List<RexNode> expList = Lists.newArrayList(project.getProjects());
  final RelMetadataQuery mq = project.getCluster().getMetadataQuery();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(project.getInput());
  boolean reducible = reduceExpressions(project, expList, predicates);
  if (reducible) {
    int numReducible = 0;
    for (int i = 0; i < project.getProjects().size(); i++) {
      if (!project.getProjects().get(i).toString().equals(expList.get(i).toString())) {
        numReducible++;
      }
    }
    return numReducible;
  } else {
    return 0;
  }
}
 
Example #8
Source File: StreamAggPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  if(PrelUtil.getSettings(getCluster()).useDefaultCosting()) {
    return super.computeSelfCost(planner, mq).multiplyBy(.1);
  }
  RelNode child = this.getInput();
  double inputRows = mq.getRowCount(child);

  int numGroupByFields = this.getGroupCount();
  int numAggrFields = this.aggCalls.size();
  double cpuCost = DrillCostBase.COMPARE_CPU_COST * numGroupByFields * inputRows;
  // add cpu cost for computing the aggregate functions
  cpuCost += DrillCostBase.FUNC_CPU_COST * numAggrFields * inputRows;
  DrillCostFactory costFactory = (DrillCostFactory)planner.getCostFactory();
  return costFactory.makeCost(inputRows, cpuCost, 0 /* disk i/o cost */, 0 /* network cost */);
}
 
Example #9
Source File: EnumerableBatchNestedLoopJoin.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static EnumerableBatchNestedLoopJoin create(
    RelNode left,
    RelNode right,
    RexNode condition,
    ImmutableBitSet requiredColumns,
    Set<CorrelationId> variablesSet,
    JoinRelType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.enumerableBatchNestedLoopJoin(mq, left, right, joinType));
  return new EnumerableBatchNestedLoopJoin(
      cluster,
      traitSet,
      left,
      right,
      condition,
      variablesSet,
      requiredColumns,
      joinType);
}
 
Example #10
Source File: AggregateRemoveRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Aggregate aggregate = call.rel(0);
  final RelNode input = call.rel(1);
  if (!aggregate.getAggCallList().isEmpty() || aggregate.indicator) {
    return;
  }
  final RelMetadataQuery mq = call.getMetadataQuery();
  if (!SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()))) {
    return;
  }
  // Distinct is "GROUP BY c1, c2" (where c1, c2 are a set of columns on
  // which the input is unique, i.e. contain a key) and has no aggregate
  // functions. It can be removed.
  final RelNode newInput = convert(input, aggregate.getTraitSet().simplify());

  // If aggregate was projecting a subset of columns, add a project for the
  // same effect.
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(newInput);
  if (newInput.getRowType().getFieldCount()
      > aggregate.getRowType().getFieldCount()) {
    relBuilder.project(relBuilder.fields(aggregate.getGroupSet().asList()));
  }
  call.transformTo(relBuilder.build());
}
 
Example #11
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * First, the method takes the node expressions {@code nodeExprs} and swaps the table
 * and column references using the table mapping and the equivalence classes.
 * If {@code swapTableColumn} is true, it swaps the table reference and then the column reference,
 * otherwise it swaps the column reference and then the table reference.
 *
 * <p>Then, the method will rewrite the input expressions {@code exprsToRewrite}, replacing the
 * {@link RexTableInputRef} by references to the positions in {@code nodeExprs}.
 *
 * <p>The method will return the rewritten expressions. If any of the subexpressions in the input
 * expressions cannot be mapped, it will return null.
 */
private static List<RexNode> rewriteExpressions(RexBuilder rexBuilder, RelMetadataQuery mq, RelNode targetNode,
        RelNode node, List<RexNode> nodeExprs, BiMap<RelTableRef, RelTableRef> tableMapping, EquivalenceClasses ec,
        boolean swapTableColumn, List<RexNode> exprsToRewrite) {
    NodeLineage nodeLineage;
    if (swapTableColumn) {
        nodeLineage = generateSwapTableColumnReferencesLineage(rexBuilder, mq, node, tableMapping, ec, nodeExprs);
    } else {
        nodeLineage = generateSwapColumnTableReferencesLineage(rexBuilder, mq, node, tableMapping, ec, nodeExprs);
    }

    List<RexNode> rewrittenExprs = new ArrayList<>(exprsToRewrite.size());
    for (RexNode exprToRewrite : exprsToRewrite) {
        RexNode rewrittenExpr = replaceWithOriginalReferences(rexBuilder, targetNode, nodeLineage, exprToRewrite);
        if (RexUtil.containsTableInputRef(rewrittenExpr) != null) {
            // Some expressions were not present in view output
            return null;
        }
        rewrittenExprs.add(rewrittenExpr);
    }
    return rewrittenExprs;
}
 
Example #12
Source File: JdbcCrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  // Make the cost inversely proportional to the height of the rel tree under this node.  We want to maximize the
  // height of the tree because we want to pushdown as much as possible to the jdbc source.  The main problem here is the
  // Projects.  ProjectRelBase.computeSelfCost() generally returns a cost of "tiny" (which is 1).  So, even if we
  // match LogicalProject to two different options with the same cost (one with Project on top of JdbcRel, and another with
  // JdbcProject below JdbcRel), we may choose the one with Project on top of JdbcRel.  This is because if the costs
  // are the same, calcite will choose the first plan option it generated.

  // Compute the height of the tree.
  int minDepth = MoreRelOptUtil.getDepth(input);
  if (minDepth <= 0) {
    return planner.getCostFactory().makeInfiniteCost();
  }

  return planner.getCostFactory().makeCost(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE).multiplyBy(1.0/minDepth);
}
 
Example #13
Source File: Aggregate.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public double estimateRowCount(RelMetadataQuery mq) {
  // Assume that each sort column has 50% of the value count.
  // Therefore one sort column has .5 * rowCount,
  // 2 sort columns give .75 * rowCount.
  // Zero sort columns yields 1 row (or 0 if the input is empty).
  final int groupCount = groupSet.cardinality();
  if (groupCount == 0) {
    return 1;
  } else {
    double rowCount = super.estimateRowCount(mq);
    rowCount *= 1.0 - Math.pow(.5, groupCount);
    return rowCount;
  }
}
 
Example #14
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testColumnUniquenessForExchangeWithConstantColumns() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode exchange = builder.scan("EMP")
      .project(builder.field("DEPTNO"), builder.field("SAL"))
      .distinct()
      .filter(builder.equals(builder.field("SAL"), builder.literal(1)))
      .exchange(RelDistributions.hash(ImmutableList.of(1)))
      .build();
  final RelMetadataQuery mq = exchange.getCluster().getMetadataQuery();
  assertThat(mq.areColumnsUnique(exchange, ImmutableBitSet.of(0)), is(true));
}
 
Example #15
Source File: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 5 votes vote down vote up
private Double getSubsetSelectivity(RelSubset rel, RelMetadataQuery mq, RexNode predicate) {
  if (rel.getBest() != null) {
    return getSelectivity(rel.getBest(), mq, predicate);
  } else {
    List<RelNode> list = rel.getRelList();
    if (list != null && list.size() > 0) {
      return getSelectivity(list.get(0), mq, predicate);
    }
  }
  //TODO: Not required? return mq.getSelectivity(((RelSubset)rel).getOriginal(), predicate);
  return RelMdUtil.guessSelectivity(predicate);
}
 
Example #16
Source File: RelMdPredicates.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public RelOptPredicateList getPredicates(RelSubset subset,
    RelMetadataQuery mq) {
  // Currently disabled in Calcite upstream
  // Only go over the best node if it exists, and try the original node otherwise
  RelOptPredicateList predicates = mq.getPulledUpPredicates(Util.first(subset.getBest(), subset.getOriginal()));
  return predicates;
}
 
Example #17
Source File: RowKeyJoinPrel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  if(PrelUtil.getSettings(getCluster()).useDefaultCosting()) {
    return super.computeSelfCost(planner).multiplyBy(.1);
  }
  double rowCount = mq.getRowCount(this.getRight());
  DrillCostFactory costFactory = (DrillCostFactory) planner.getCostFactory();

  // RowKeyJoin operator by itself incurs negligible CPU and I/O cost since it is not doing a real join.
  // The actual cost is attributed to the skip-scan (random I/O). The RK join will hold 1 batch in memory but
  // it is not making any extra copy of either the left or right batches, so the memory cost is 0
  return costFactory.makeCost(rowCount, 0, 0, 0, 0);
}
 
Example #18
Source File: Calc.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  double dRows = mq.getRowCount(this);
  double dCpu = mq.getRowCount(getInput())
      * program.getExprCount();
  double dIo = 0;
  return planner.getCostFactory().makeCost(dRows, dCpu, dIo);
}
 
Example #19
Source File: MaterializedViewRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites the query using the given view query.
 *
 * <p>The input node is a Scan on the view table and possibly a compensation Filter
 * on top. If a rewriting can be produced, we return that rewriting. If it cannot
 * be produced, we will return null.
 */
protected abstract RelNode rewriteView(RelBuilder relBuilder, RexBuilder rexBuilder,
    RexSimplify simplify, RelMetadataQuery mq, MatchModality matchModality,
    boolean unionRewriting, RelNode input,
    Project topProject, RelNode node,
    Project topViewProject, RelNode viewNode,
    BiMap<RelTableRef, RelTableRef> queryToViewTableMapping,
    EquivalenceClasses queryEC);
 
Example #20
Source File: RelMdDistinctRowCount.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public Double getDistinctRowCount(Join rel, RelMetadataQuery mq,
                                  ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  return getDistinctRowCountFromEstimateRowCount(rel, mq, groupKey, predicate);
}
 
Example #21
Source File: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Double getSelectivity(RelNode rel, RelMetadataQuery mq, RexNode predicate) {
  if (rel instanceof RelSubset && !DrillRelOptUtil.guessRows(rel)) {
    return getSubsetSelectivity((RelSubset) rel, mq, predicate);
  } else if (rel instanceof TableScan) {
    return getScanSelectivity(rel, mq, predicate);
  } else if (rel instanceof DrillJoinRelBase) {
    return getJoinSelectivity(((DrillJoinRelBase) rel), mq, predicate);
  } /*else if (rel instanceof SingleRel && !DrillRelOptUtil.guessRows(rel)) {
    return getSelectivity(((SingleRel)rel).getInput(), mq, predicate);
  }*/ else {
    return super.getSelectivity(rel, mq, predicate);
  }
}
 
Example #22
Source File: OLAPProjectRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
/**
 * Since the project under aggregate maybe reduce expressions by {@link org.apache.kylin.query.optrule.AggregateProjectReduceRule},
 * consider the count of expressions into cost, the reduced project will be used.
 *
 * Made RexOver much more expensive so we can transform into {@link org.apache.kylin.query.relnode.OLAPWindowRel}
 * by rules in {@link org.apache.calcite.rel.rules.ProjectToWindowRule}
 */
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
    boolean hasRexOver = RexOver.containsOver(getProjects(), null);
    RelOptCost relOptCost = super.computeSelfCost(planner, mq).multiplyBy(.05)
            .multiplyBy(getProjects().size() * (double) (hasRexOver ? 50 : 1))
            .plus(planner.getCostFactory().makeCost(0.1 * caseCount, 0, 0));
    return planner.getCostFactory().makeCost(relOptCost.getRows(), 0, 0);
}
 
Example #23
Source File: ElasticsearchIntermediatePrel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  int minDepth = nodes.size();
  if (minDepth == 0) {
    return planner.getCostFactory().makeInfiniteCost();
  }

  /* Intermediate Prel doesn't really have any cost associated to it, but its cost is inversely proportional to the number of children it has
   * i.e. more pushdown the better.
   */
  return planner.getCostFactory().makeCost(Integer.MAX_VALUE, Integer.MAX_VALUE, 0).multiplyBy(1.0/minDepth);
}
 
Example #24
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testExpressionLineageSelfJoin() {
  // deptno is column 7 in catalog.sales.emp
  // sal is column 5 in catalog.sales.emp
  final RelNode rel = convertSql("select a.deptno, b.sal from (select * from emp limit 7) as a\n"
      + "inner join (select * from emp limit 2) as b\n"
      + "on a.deptno = b.deptno");
  final RelNode tableRel = convertSql("select * from emp");
  final RelMetadataQuery mq = tableRel.getCluster().getMetadataQuery();

  final RexNode ref1 = RexInputRef.of(0, rel.getRowType().getFieldList());
  final Set<RexNode> r1 = mq.getExpressionLineage(rel, ref1);
  final String inputRef1 = RexInputRef.of(7, tableRel.getRowType().getFieldList()).toString();
  assertThat(r1.size(), is(1));
  final String resultString1 = r1.iterator().next().toString();
  assertThat(resultString1, startsWith(EMP_QNAME.toString()));
  assertThat(resultString1, endsWith(inputRef1));

  final RexNode ref2 = RexInputRef.of(1, rel.getRowType().getFieldList());
  final Set<RexNode> r2 = mq.getExpressionLineage(rel, ref2);
  final String inputRef2 = RexInputRef.of(5, tableRel.getRowType().getFieldList()).toString();
  assertThat(r2.size(), is(1));
  final String resultString2 = r2.iterator().next().toString();
  assertThat(resultString2, startsWith(EMP_QNAME.toString()));
  assertThat(resultString2, endsWith(inputRef2));

  assertThat(((RexTableInputRef) r1.iterator().next()).getIdentifier(),
      not(((RexTableInputRef) r2.iterator().next()).getIdentifier()));
}
 
Example #25
Source File: LoptOptimizeJoinRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the equality portion of a self-join condition is between
 * identical keys that are unique.
 *
 * @param mq Metadata query
 * @param leftRel left side of the join
 * @param rightRel right side of the join
 * @param joinFilters the join condition
 *
 * @return true if the equality join keys are the same and unique
 */
private static boolean areSelfJoinKeysUnique(RelMetadataQuery mq,
    RelNode leftRel, RelNode rightRel, RexNode joinFilters) {
  final JoinInfo joinInfo = JoinInfo.of(leftRel, rightRel, joinFilters);

  // Make sure each key on the left maps to the same simple column as the
  // corresponding key on the right
  for (IntPair pair : joinInfo.pairs()) {
    final RelColumnOrigin leftOrigin =
        mq.getColumnOrigin(leftRel, pair.source);
    if (leftOrigin == null) {
      return false;
    }
    final RelColumnOrigin rightOrigin =
        mq.getColumnOrigin(rightRel, pair.target);
    if (rightOrigin == null) {
      return false;
    }
    if (leftOrigin.getOriginColumnOrdinal()
        != rightOrigin.getOriginColumnOrdinal()) {
      return false;
    }
  }

  // Now that we've verified that the keys are the same, see if they
  // are unique.  When removing self-joins, if needed, we'll later add an
  // IS NOT NULL filter on the join keys that are nullable.  Therefore,
  // it's ok if there are nulls in the unique key.
  return RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, leftRel,
      joinInfo.leftSet());
}
 
Example #26
Source File: Join.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** @deprecated Use {@link RelMdUtil#getJoinRowCount(RelMetadataQuery, Join, RexNode)}. */
@Deprecated // to be removed before 2.0
public static double estimateJoinedRows(
    Join joinRel,
    RexNode condition) {
  final RelMetadataQuery mq = joinRel.getCluster().getMetadataQuery();
  return Util.first(RelMdUtil.getJoinRowCount(mq, joinRel, condition), 1D);
}
 
Example #27
Source File: ConverterImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  double dRows = mq.getRowCount(getInput());
  double dCpu = dRows;
  double dIo = 0;
  return planner.getCostFactory().makeCost(dRows, dCpu, dIo);
}
 
Example #28
Source File: MaterializedViewRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * It swaps the table references and then the column references of the input
 * expressions using the table mapping and the equivalence classes.
 */
protected NodeLineage generateSwapTableColumnReferencesLineage(
    RexBuilder rexBuilder,
    RelMetadataQuery mq,
    RelNode node,
    BiMap<RelTableRef, RelTableRef> tableMapping,
    EquivalenceClasses ec,
    List<RexNode> nodeExprs) {
  final Map<RexNode, Integer> exprsLineage = new HashMap<>();
  final Map<RexNode, Integer> exprsLineageLosslessCasts = new HashMap<>();
  for (int i = 0; i < nodeExprs.size(); i++) {
    final Set<RexNode> s = mq.getExpressionLineage(node, nodeExprs.get(i));
    if (s == null) {
      // Next expression
      continue;
    }
    // We only support project - filter - join, thus it should map to
    // a single expression
    assert s.size() == 1;
    // Rewrite expr. First we swap the table references following the table
    // mapping, then we take first element from the corresponding equivalence class
    final RexNode e = RexUtil.swapTableColumnReferences(rexBuilder,
        s.iterator().next(), tableMapping, ec.getEquivalenceClassesMap());
    exprsLineage.put(e, i);
    if (RexUtil.isLosslessCast(e)) {
      exprsLineageLosslessCasts.put(((RexCall) e).getOperands().get(0), i);
    }
  }
  return new NodeLineage(exprsLineage, exprsLineageLosslessCasts);
}
 
Example #29
Source File: ConverterImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  double dRows = mq.getRowCount(getInput());
  double dCpu = dRows;
  double dIo = 0;
  return planner.getCostFactory().makeCost(dRows, dCpu, dIo);
}
 
Example #30
Source File: SortRemoveConstantKeysRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelNode input = sort.getInput();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
  if (predicates == null) {
    return;
  }

  final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
  final List<RelFieldCollation> collationsList =
      sort.getCollation().getFieldCollations().stream()
          .filter(fc ->
              !predicates.constantMap.containsKey(
                  rexBuilder.makeInputRef(input, fc.getFieldIndex())))
          .collect(Collectors.toList());

  if (collationsList.size() == sort.collation.getFieldCollations().size()) {
    return;
  }

  // No active collations. Remove the sort completely
  if (collationsList.isEmpty() && sort.offset == null && sort.fetch == null) {
    call.transformTo(input);
    call.getPlanner().setImportance(sort, 0.0);
    return;
  }

  final Sort result =
      sort.copy(sort.getTraitSet(), input, RelCollations.of(collationsList));
  call.transformTo(result);
  call.getPlanner().setImportance(sort, 0.0);
}