當前位置: 首頁>>代碼示例>>Java>>正文


Java RelMdUtil類代碼示例

本文整理匯總了Java中org.apache.calcite.rel.metadata.RelMdUtil的典型用法代碼示例。如果您正苦於以下問題:Java RelMdUtil類的具體用法?Java RelMdUtil怎麽用?Java RelMdUtil使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RelMdUtil類屬於org.apache.calcite.rel.metadata包,在下文中一共展示了RelMdUtil類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: estimateCpuCost

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
private double estimateCpuCost(RelMetadataQuery relMetadataQuery) {
  RelNode child = this.getInput();
  final double rows = relMetadataQuery.getRowCount(child);
  double compNum = rows;
  double rowCompNum = child.getRowType().getFieldCount() * rows ;


  for (int i = 0; i< numConjuncts; i++) {
    RexNode conjFilter = RexUtil.composeConjunction(this.getCluster().getRexBuilder(), conjunctions.subList(0, i + 1), false);
    compNum += RelMdUtil.estimateFilteredRows(child, conjFilter, relMetadataQuery);
  }

  return compNum * DremioCost.COMPARE_CPU_COST + rowCompNum * DremioCost.COPY_COST;
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:15,代碼來源:FilterRelBase.java

示例2: computeSelfCost

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
@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 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());
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:16,代碼來源:DruidQuery.java

示例3: getIntervalCostMultiplier

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
private double getIntervalCostMultiplier() {
  int days = 0;
  for (Interval interval : intervals) {
    days += interval.toDuration().getStandardDays();
  }
  // Cost increases with the wider interval being queries.
  // A plan querying 10 or more years of data will have 10x the cost of a
  // plan returning 1 day data.
  // A plan where least interval is queries will be preferred.
  return RelMdUtil.linear(days, 1, DAYS_IN_TEN_YEARS, 0.1d, 1d);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:12,代碼來源:DruidQuery.java

示例4: areSelfJoinKeysUnique

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
/**
 * 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());
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:42,代碼來源:LoptOptimizeJoinRule.java

示例5: onMatch

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  List<RelNode> inputs = new ArrayList<>();
  // Thus we use 'ret' as a flag to identify if we have finished pushing the
  // sort past a union.
  boolean ret = true;
  final RelMetadataQuery mq = call.getMetadataQuery();
  for (RelNode input : union.getInputs()) {
    if (!RelMdUtil.checkInputForCollationAndLimit(mq, input,
        sort.getCollation(), sort.offset, sort.fetch)) {
      ret = false;
      Sort branchSort = sort.copy(sort.getTraitSet(), input,
          sort.getCollation(), sort.offset, sort.fetch);
      inputs.add(branchSort);
    } else {
      inputs.add(input);
    }
  }
  // there is nothing to change
  if (ret) {
    return;
  }
  // create new union and sort
  Union unionCopy = (Union) union
      .copy(union.getTraitSet(), inputs, union.all);
  Sort result = sort.copy(sort.getTraitSet(), unionCopy, sort.getCollation(),
      sort.offset, sort.fetch);
  call.transformTo(result);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:31,代碼來源:SortUnionTransposeRule.java

示例6: estimateJoinedRows

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
/** @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 = RelMetadataQuery.instance();
  return Util.first(RelMdUtil.getJoinRowCount(mq, joinRel, condition), 1D);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:9,代碼來源:Join.java

示例7: estimateRowCount

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
@Override public double estimateRowCount(RelMetadataQuery mq) {
  double dRows = RelMdUtil.getUnionAllRowCount(mq, this);
  if (!all) {
    dRows *= 0.5;
  }
  return dRows;
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:8,代碼來源:Union.java

示例8: testLinear

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
/** Unit test for {@link RelMdUtil#linear(int, int, int, double, double)}. */
@Test public void testLinear() {
  assertThat(RelMdUtil.linear(0, 0, 10, 100, 200), is(100d));
  assertThat(RelMdUtil.linear(5, 0, 10, 100, 200), is(150d));
  assertThat(RelMdUtil.linear(6, 0, 10, 100, 200), is(160d));
  assertThat(RelMdUtil.linear(10, 0, 10, 100, 200), is(200d));
  assertThat(RelMdUtil.linear(-2, 0, 10, 100, 200), is(100d));
  assertThat(RelMdUtil.linear(12, 0, 10, 100, 200), is(200d));
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:10,代碼來源:RelMetadataTest.java

示例9: getDistinctRowCountFromEstimateRowCount

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
private Double getDistinctRowCountFromEstimateRowCount(RelNode rel, RelMetadataQuery mq, ImmutableBitSet groupKey, RexNode predicate) {
  final int groupKeySize = groupKey.cardinality();
  return rel.estimateRowCount(mq) * (1.0 - Math.pow(0.9, groupKeySize)) * RelMdUtil.guessSelectivity(predicate);
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:5,代碼來源:RelMdDistinctRowCount.java

示例10: estimateRowCount

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
@Override public double estimateRowCount(RelMetadataQuery mq) {
  return RelMdUtil.estimateFilteredRows(getInput(), program, mq);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:4,代碼來源:SparkRules.java

示例11: onMatch

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
public void onMatch(RelOptRuleCall call) {
  Aggregate aggRel = call.rel(0);
  Union union = call.rel(1);

  if (!union.all) {
    // This transformation is only valid for UNION ALL.
    // Consider t1(i) with rows (5), (5) and t2(i) with
    // rows (5), (10), and the query
    // select sum(i) from (select i from t1) union (select i from t2).
    // The correct answer is 15.  If we apply the transformation,
    // we get
    // select sum(i) from
    // (select sum(i) as i from t1) union (select sum(i) as i from t2)
    // which yields 25 (incorrect).
    return;
  }

  int groupCount = aggRel.getGroupSet().cardinality();

  List<AggregateCall> transformedAggCalls =
      transformAggCalls(
          aggRel.copy(aggRel.getTraitSet(), aggRel.getInput(), false,
              aggRel.getGroupSet(), null, aggRel.getAggCallList()),
          groupCount, aggRel.getAggCallList());
  if (transformedAggCalls == null) {
    // we've detected the presence of something like AVG,
    // which we can't handle
    return;
  }

  // create corresponding aggregates on top of each union child
  final RelBuilder relBuilder = call.builder();
  int transformCount = 0;
  final RelMetadataQuery mq = call.getMetadataQuery();
  for (RelNode input : union.getInputs()) {
    boolean alreadyUnique =
        RelMdUtil.areColumnsDefinitelyUnique(mq, input,
            aggRel.getGroupSet());

    relBuilder.push(input);
    if (!alreadyUnique) {
      ++transformCount;
      relBuilder.aggregate(relBuilder.groupKey(aggRel.getGroupSet(), null),
          aggRel.getAggCallList());
    }
  }

  if (transformCount == 0) {
    // none of the children could benefit from the push-down,
    // so bail out (preventing the infinite loop to which most
    // planners would succumb)
    return;
  }

  // create a new union whose children are the aggregates created above
  relBuilder.union(true, union.getInputs().size());
  relBuilder.aggregate(
      relBuilder.groupKey(aggRel.getGroupSet(), aggRel.getGroupSets()),
      transformedAggCalls);
  call.transformTo(relBuilder.build());
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:62,代碼來源:AggregateUnionTransposeRule.java

示例12: computeScore

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
/**
 * Computes a score relevant to applying a set of semijoins on a fact table.
 * The higher the score, the better.
 *
 * @param factRel fact table being filtered
 * @param dimRel dimension table that participates in semijoin
 * @param semiJoin semijoin between fact and dimension tables
 *
 * @return computed score of applying the dimension table filters on the
 * fact table
 */
private double computeScore(
    RelNode factRel,
    RelNode dimRel,
    SemiJoin semiJoin) {
  // Estimate savings as a result of applying semijoin filter on fact
  // table.  As a heuristic, the selectivity of the semijoin needs to
  // be less than half.  There may be instances where an even smaller
  // selectivity value is required because of the overhead of
  // index lookups on a very large fact table.  Half was chosen as
  // a middle ground based on testing that was done with a large
  // data set.
  final ImmutableBitSet dimCols = ImmutableBitSet.of(semiJoin.getRightKeys());
  final double selectivity =
      RelMdUtil.computeSemiJoinSelectivity(mq, factRel, dimRel, semiJoin);
  if (selectivity > .5) {
    return 0;
  }

  final RelOptCost factCost = mq.getCumulativeCost(factRel);

  // if not enough information, return a low score
  if (factCost == null) {
    return 0;
  }
  double savings =
      (1.0 - Math.sqrt(selectivity))
          * Math.max(1.0, factCost.getRows());

  // Additional savings if the dimension columns are unique.  We can
  // ignore nulls since they will be filtered out by the semijoin.
  boolean uniq =
      RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq,
          dimRel, dimCols);
  if (uniq) {
    savings *= 2.0;
  }

  // compute the cost of doing an extra scan on the dimension table,
  // including the distinct sort on top of the scan; if the dimension
  // columns are already unique, no need to add on the dup removal cost
  final Double dimSortCost = mq.getRowCount(dimRel);
  final Double dupRemCost = uniq ? 0 : dimSortCost;
  final RelOptCost dimCost = mq.getCumulativeCost(dimRel);
  if ((dimSortCost == null)
      || (dupRemCost == null)
      || (dimCost == null)) {
    return 0;
  }

  Double dimRows = dimCost.getRows();
  if (dimRows < 1.0) {
    dimRows = 1.0;
  }
  return savings / dimRows;
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:67,代碼來源:LoptSemiJoinOptimizer.java

示例13: removeJoin

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
/**
 * Determines whether a join of the dimension table in a semijoin can be
 * removed. It can be if the dimension keys are unique and the only fields
 * referenced from the dimension table are its semijoin keys. The semijoin
 * keys can be mapped to the corresponding keys from the fact table (because
 * of the equality condition associated with the semijoin keys). Therefore,
 * that's why the dimension table can be removed even though those fields
 * are referenced elsewhere in the query tree.
 *
 * @param multiJoin join factors being optimized
 * @param semiJoin semijoin under consideration
 * @param factIdx id of the fact table in the semijoin
 * @param dimIdx id of the dimension table in the semijoin
 */
private void removeJoin(
    LoptMultiJoin multiJoin,
    SemiJoin semiJoin,
    int factIdx,
    int dimIdx) {
  // if the dimension can be removed because of another semijoin, then
  // no need to proceed any further
  if (multiJoin.getJoinRemovalFactor(dimIdx) != null) {
    return;
  }

  // Check if the semijoin keys corresponding to the dimension table
  // are unique.  The semijoin will filter out the nulls.
  final ImmutableBitSet dimKeys = ImmutableBitSet.of(semiJoin.getRightKeys());
  final RelNode dimRel = multiJoin.getJoinFactor(dimIdx);
  if (!RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, dimRel,
      dimKeys)) {
    return;
  }

  // check that the only fields referenced from the dimension table
  // in either its projection or join conditions are the dimension
  // keys
  ImmutableBitSet dimProjRefs = multiJoin.getProjFields(dimIdx);
  if (dimProjRefs == null) {
    int nDimFields = multiJoin.getNumFieldsInJoinFactor(dimIdx);
    dimProjRefs = ImmutableBitSet.range(0, nDimFields);
  }
  if (!dimKeys.contains(dimProjRefs)) {
    return;
  }
  int [] dimJoinRefCounts = multiJoin.getJoinFieldRefCounts(dimIdx);
  for (int i = 0; i < dimJoinRefCounts.length; i++) {
    if (dimJoinRefCounts[i] > 0) {
      if (!dimKeys.get(i)) {
        return;
      }
    }
  }

  // criteria met; keep track of the fact table and the semijoin that
  // allow the join of this dimension table to be removed
  multiJoin.setJoinRemovalFactor(dimIdx, factIdx);
  multiJoin.setJoinRemovalSemiJoin(dimIdx, semiJoin);

  // if the dimension table doesn't reference anything in its projection
  // and the only fields referenced in its joins are the dimension keys
  // of this semijoin, then we can decrement the join reference counts
  // corresponding to the fact table's semijoin keys, since the
  // dimension table doesn't need to use those keys
  if (dimProjRefs.cardinality() != 0) {
    return;
  }
  for (int i = 0; i < dimJoinRefCounts.length; i++) {
    if (dimJoinRefCounts[i] > 1) {
      return;
    } else if (dimJoinRefCounts[i] == 1) {
      if (!dimKeys.get(i)) {
        return;
      }
    }
  }
  int [] factJoinRefCounts = multiJoin.getJoinFieldRefCounts(factIdx);
  for (Integer key : semiJoin.getLeftKeys()) {
    factJoinRefCounts[key]--;
  }
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:82,代碼來源:LoptSemiJoinOptimizer.java

示例14: onMatch

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Join join = call.rel(1);

  // We create a new sort operator on the corresponding input
  final RelNode newLeftInput;
  final RelNode newRightInput;
  final RelMetadataQuery mq = call.getMetadataQuery();
  if (join.getJoinType() == JoinRelType.LEFT) {
    // If the input is already sorted and we are not reducing the number of tuples,
    // we bail out
    if (RelMdUtil.checkInputForCollationAndLimit(mq, join.getLeft(),
        sort.getCollation(), sort.offset, sort.fetch)) {
      return;
    }
    newLeftInput = sort.copy(sort.getTraitSet(), join.getLeft(), sort.getCollation(),
        sort.offset, sort.fetch);
    newRightInput = join.getRight();
  } else {
    final RelCollation rightCollation =
        RelCollationTraitDef.INSTANCE.canonize(
            RelCollations.shift(sort.getCollation(),
                -join.getLeft().getRowType().getFieldCount()));
    // If the input is already sorted and we are not reducing the number of tuples,
    // we bail out
    if (RelMdUtil.checkInputForCollationAndLimit(mq, join.getRight(),
        rightCollation, sort.offset, sort.fetch)) {
      return;
    }
    newLeftInput = join.getLeft();
    newRightInput = sort.copy(sort.getTraitSet().replace(rightCollation),
        join.getRight(), rightCollation, sort.offset, sort.fetch);
  }
  // We copy the join and the top sort operator
  final RelNode joinCopy = join.copy(join.getTraitSet(), join.getCondition(), newLeftInput,
      newRightInput, join.getJoinType(), join.isSemiJoinDone());
  final RelNode sortCopy = sort.copy(sort.getTraitSet(), joinCopy, sort.getCollation(),
      sort.offset, sort.fetch);

  call.transformTo(sortCopy);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:42,代碼來源:SortJoinTransposeRule.java

示例15: estimateRowCount

import org.apache.calcite.rel.metadata.RelMdUtil; //導入依賴的package包/類
@Override public double estimateRowCount(RelMetadataQuery mq) {
  return Util.first(RelMdUtil.getJoinRowCount(mq, this, condition), 1D);
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:4,代碼來源:Join.java


注:本文中的org.apache.calcite.rel.metadata.RelMdUtil類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。