本文整理汇总了Java中org.apache.calcite.util.ImmutableBitSet.cardinality方法的典型用法代码示例。如果您正苦于以下问题:Java ImmutableBitSet.cardinality方法的具体用法?Java ImmutableBitSet.cardinality怎么用?Java ImmutableBitSet.cardinality使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.calcite.util.ImmutableBitSet
的用法示例。
在下文中一共展示了ImmutableBitSet.cardinality方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Run
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
Run(final List<Column> columns) {
for (Ord<Column> column : Ord.zip(columns)) {
if (column.e.ordinal != column.i) {
throw new IllegalArgumentException();
}
}
this.columns = columns;
this.singletonSpaces =
new ArrayList<>(Collections.nCopies(columns.size(), (Space) null));
for (ImmutableBitSet ordinals
: ImmutableBitSet.range(columns.size()).powerSet()) {
final Space space = new Space(ordinals, toColumns(ordinals));
spaces.add(space);
if (ordinals.cardinality() == 1) {
singletonSpaces.set(ordinals.nth(0), space);
}
}
}
示例2: aggsToExpr
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
public static List<NamedExpression> aggsToExpr(
RelDataType rowType, RelNode input, ImmutableBitSet groupSet, List<AggregateCall> aggCalls) {
final List<String> fields = rowType.getFieldNames();
final List<String> childFields = input.getRowType().getFieldNames();
final List<NamedExpression> aggExprs = Lists.newArrayList();
for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
int aggExprOrdinal = groupSet.cardinality() + aggCall.i;
FieldReference ref = FieldReference.getWithQuotedRef(fields.get(aggExprOrdinal));
LogicalExpression expr = toExpr(aggCall.e, childFields);
NamedExpression ne = new NamedExpression(expr, ref);
aggExprs.add(ne);
}
return aggExprs;
}
示例3: mappings
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
Iterable<Mapping> mappings(final RexNode predicate) {
return new Iterable<Mapping>() {
public Iterator<Mapping> iterator() {
ImmutableBitSet fields = exprFields.get(predicate.toString());
if (fields.cardinality() == 0) {
return Collections.emptyIterator();
}
return new ExprsItr(fields);
}
};
}
示例4: ExprsItr
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
ExprsItr(ImmutableBitSet fields) {
nextMapping = null;
columns = new int[fields.cardinality()];
columnSets = new BitSet[fields.cardinality()];
iterationIdx = new int[fields.cardinality()];
for (int j = 0, i = fields.nextSetBit(0); i >= 0; i = fields
.nextSetBit(i + 1), j++) {
columns[j] = i;
columnSets[j] = equivalence.get(i);
iterationIdx[j] = 0;
}
firstCall = true;
}
示例5: getAdjustedIndex
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
private int getAdjustedIndex(final int initIndex,
final ImmutableBitSet beReferred, final int windowInputColumn) {
if (initIndex >= windowInputColumn) {
return beReferred.cardinality() + (initIndex - windowInputColumn);
} else {
return beReferred.get(0, initIndex).cardinality();
}
}
示例6: getDistinctRowCountFromEstimateRowCount
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的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);
}
示例7: areColumnsUnique
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
public Boolean areColumnsUnique(Join rel, RelMetadataQuery mq,
ImmutableBitSet columns, boolean ignoreNulls) {
if (columns.cardinality() == 0) {
return false;
}
final RelNode left = rel.getLeft();
final RelNode right = rel.getRight();
// Divide up the input column mask into column masks for the left and
// right sides of the join
final Pair<ImmutableBitSet, ImmutableBitSet> leftAndRightColumns =
splitLeftAndRightColumns(rel.getLeft().getRowType().getFieldCount(),
columns);
final ImmutableBitSet leftColumns = leftAndRightColumns.left;
final ImmutableBitSet rightColumns = leftAndRightColumns.right;
// If the original column mask contains columns from both the left and
// right hand side, then the columns are unique if and only if they're
// unique for their respective join inputs
Boolean leftUnique = mq.areColumnsUnique(left, leftColumns, ignoreNulls);
Boolean rightUnique = mq.areColumnsUnique(right, rightColumns, ignoreNulls);
if ((leftColumns.cardinality() > 0)
&& (rightColumns.cardinality() > 0)) {
if ((leftUnique == null) || (rightUnique == null)) {
return null;
} else {
return leftUnique && rightUnique;
}
}
// If we're only trying to determine uniqueness for columns that
// originate from one join input, then determine if the equijoin
// columns from the other join input are unique. If they are, then
// the columns are unique for the entire join if they're unique for
// the corresponding join input, provided that input is not null
// generating.
final JoinInfo joinInfo = rel.analyzeCondition();
if (leftColumns.cardinality() > 0) {
if (rel.getJoinType().generatesNullsOnLeft()) {
return false;
}
Boolean rightJoinColsUnique =
mq.areColumnsUnique(right, joinInfo.rightSet(), ignoreNulls);
if ((rightJoinColsUnique == null) || (leftUnique == null)) {
return null;
}
return rightJoinColsUnique && leftUnique;
} else if (rightColumns.cardinality() > 0) {
if (rel.getJoinType().generatesNullsOnRight()) {
return false;
}
Boolean leftJoinColsUnique =
mq.areColumnsUnique(left, joinInfo.leftSet(), ignoreNulls);
if ((leftJoinColsUnique == null) || (rightUnique == null)) {
return null;
}
return leftJoinColsUnique && rightUnique;
}
throw new AssertionError();
}
示例8: createUnion
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
@Override protected RelNode createUnion(RelBuilder relBuilder, RexBuilder rexBuilder,
RelNode topProject, RelNode unionInputQuery, RelNode unionInputView) {
// Union
relBuilder.push(unionInputQuery);
relBuilder.push(unionInputView);
relBuilder.union(true);
List<RexNode> exprList = new ArrayList<>(relBuilder.peek().getRowType().getFieldCount());
List<String> nameList = new ArrayList<>(relBuilder.peek().getRowType().getFieldCount());
for (int i = 0; i < relBuilder.peek().getRowType().getFieldCount(); i++) {
// We can take unionInputQuery as it is query based.
RelDataTypeField field = unionInputQuery.getRowType().getFieldList().get(i);
exprList.add(
rexBuilder.ensureType(
field.getType(),
rexBuilder.makeInputRef(relBuilder.peek(), i),
true));
nameList.add(field.getName());
}
relBuilder.project(exprList, nameList);
// Rollup aggregate
Aggregate aggregate = (Aggregate) unionInputQuery;
final ImmutableBitSet groupSet = ImmutableBitSet.range(aggregate.getGroupCount());
final List<AggCall> aggregateCalls = new ArrayList<>();
for (int i = 0; i < aggregate.getAggCallList().size(); i++) {
AggregateCall aggCall = aggregate.getAggCallList().get(i);
if (aggCall.isDistinct()) {
// Cannot ROLLUP distinct
return null;
}
aggregateCalls.add(
relBuilder.aggregateCall(
SubstitutionVisitor.getRollup(aggCall.getAggregation()),
aggCall.isDistinct(), aggCall.isApproximate(), null,
aggCall.name,
rexBuilder.makeInputRef(relBuilder.peek(),
aggregate.getGroupCount() + i)));
}
RelNode prevNode = relBuilder.peek();
RelNode result = relBuilder
.aggregate(relBuilder.groupKey(groupSet, null), aggregateCalls)
.build();
if (prevNode == result && groupSet.cardinality() != result.getRowType().getFieldCount()) {
// Aggregate was not inserted but we need to prune columns
result = relBuilder
.push(result)
.project(relBuilder.fields(groupSet.asList()))
.build();
}
if (topProject != null) {
// Top project
return topProject.copy(topProject.getTraitSet(), ImmutableList.of(result));
}
// Result
return result;
}
示例9: findRemovableSelfJoins
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
/**
* Locates pairs of joins that are self-joins where the join can be removed
* because the join condition between the two factors is an equality join on
* unique keys.
*
* @param multiJoin join factors being optimized
*/
private void findRemovableSelfJoins(RelMetadataQuery mq, LoptMultiJoin multiJoin) {
// Candidates for self-joins must be simple factors
Map<Integer, RelOptTable> simpleFactors = getSimpleFactors(mq, multiJoin);
// See if a simple factor is repeated and therefore potentially is
// part of a self-join. Restrict each factor to at most one
// self-join.
final List<RelOptTable> repeatedTables = new ArrayList<>();
final TreeSet<Integer> sortedFactors = new TreeSet<>();
sortedFactors.addAll(simpleFactors.keySet());
final Map<Integer, Integer> selfJoinPairs = new HashMap<>();
Integer [] factors =
sortedFactors.toArray(new Integer[sortedFactors.size()]);
for (int i = 0; i < factors.length; i++) {
if (repeatedTables.contains(simpleFactors.get(factors[i]))) {
continue;
}
for (int j = i + 1; j < factors.length; j++) {
int leftFactor = factors[i];
int rightFactor = factors[j];
if (simpleFactors.get(leftFactor).getQualifiedName().equals(
simpleFactors.get(rightFactor).getQualifiedName())) {
selfJoinPairs.put(leftFactor, rightFactor);
repeatedTables.add(simpleFactors.get(leftFactor));
break;
}
}
}
// From the candidate self-join pairs, determine if there is
// the appropriate join condition between the two factors that will
// allow the join to be removed.
for (Integer factor1 : selfJoinPairs.keySet()) {
final int factor2 = selfJoinPairs.get(factor1);
final List<RexNode> selfJoinFilters = new ArrayList<>();
for (RexNode filter : multiJoin.getJoinFilters()) {
ImmutableBitSet joinFactors =
multiJoin.getFactorsRefByJoinFilter(filter);
if ((joinFactors.cardinality() == 2)
&& joinFactors.get(factor1)
&& joinFactors.get(factor2)) {
selfJoinFilters.add(filter);
}
}
if ((selfJoinFilters.size() > 0)
&& isSelfJoinFilterUnique(
mq,
multiJoin,
factor1,
factor2,
selfJoinFilters)) {
multiJoin.addRemovableSelfJoinPair(factor1, factor2);
}
}
}
示例10: setFactorWeights
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
/**
* Sets weighting for each combination of factors, depending on which join
* filters reference which factors. Greater weight is given to equality
* conditions. Also, sets bitmaps indicating which factors are referenced by
* each factor within join filters that are comparisons.
*/
public void setFactorWeights() {
factorWeights = new int[nJoinFactors][nJoinFactors];
factorsRefByFactor = new ImmutableBitSet[nJoinFactors];
for (int i = 0; i < nJoinFactors; i++) {
factorsRefByFactor[i] = ImmutableBitSet.of();
}
for (RexNode joinFilter : allJoinFilters) {
ImmutableBitSet factorRefs = factorsRefByJoinFilter.get(joinFilter);
// don't give weights to non-comparison expressions
if (!(joinFilter instanceof RexCall)) {
continue;
}
if (!joinFilter.isA(SqlKind.COMPARISON)) {
continue;
}
// OR the factors referenced in this join filter into the
// bitmaps corresponding to each of the factors; however,
// exclude the bit corresponding to the factor itself
for (int factor : factorRefs) {
factorsRefByFactor[factor] =
factorsRefByFactor[factor]
.rebuild()
.addAll(factorRefs)
.clear(factor)
.build();
}
if (factorRefs.cardinality() == 2) {
int leftFactor = factorRefs.nextSetBit(0);
int rightFactor = factorRefs.nextSetBit(leftFactor + 1);
final RexCall call = (RexCall) joinFilter;
ImmutableBitSet leftFields = fieldBitmap(call.getOperands().get(0));
ImmutableBitSet leftBitmap = factorBitmap(leftFields);
// filter contains only two factor references, one on each
// side of the operator
int weight;
if (leftBitmap.cardinality() == 1) {
// give higher weight to equi-joins
switch (joinFilter.getKind()) {
case EQUALS:
weight = 3;
break;
default:
weight = 2;
}
} else {
// cross product of two tables
weight = 1;
}
setFactorWeight(weight, leftFactor, rightFactor);
} else {
// multiple factor references -- set a weight for each
// combination of factors referenced within the filter
final List<Integer> list = ImmutableIntList.copyOf(factorRefs);
for (int outer : list) {
for (int inner : list) {
if (outer != inner) {
setFactorWeight(1, outer, inner);
}
}
}
}
}
}
示例11: removeJoin
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的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]--;
}
}
示例12: nextBatch
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
/** Populates {@code spaces} with the next batch.
* Returns an empty list if done. */
List<Space> nextBatch(int pass) {
final List<Space> spaces = new ArrayList<>();
loop:
for (;;) {
if (spaces.size() >= combinationsPerPass) {
// We have enough for the next pass.
return spaces;
}
// First, see if there is a space we did have room for last pass.
final ImmutableBitSet ordinals = spaceQueue.poll();
if (ordinals != null) {
final Space space = new Space(this, ordinals, toColumns(ordinals));
spaces.add(space);
if (ordinals.cardinality() == 1) {
singletonSpaces.set(ordinals.nth(0), space);
}
} else {
// Next, take a space that was done last time, generate its
// successors, and add the interesting ones to the space queue.
for (;;) {
final Space doneSpace = doneQueue.poll();
if (doneSpace == null) {
// There are no more done spaces. We're done.
return spaces;
}
if (doneSpace.columnOrdinals.cardinality() > 4) {
// Do not generate successors for groups with lots of columns,
// probably initial groups
continue;
}
for (Column column : columns) {
if (!doneSpace.columnOrdinals.get(column.ordinal)) {
if (pass == 0
|| doneSpace.columnOrdinals.cardinality() == 0
|| !containsKey(
doneSpace.columnOrdinals.set(column.ordinal))
&& predicate.apply(Pair.of(doneSpace, column))) {
final ImmutableBitSet nextOrdinals =
doneSpace.columnOrdinals.set(column.ordinal);
if (resultSet.add(nextOrdinals)) {
spaceQueue.add(nextOrdinals);
}
}
}
}
// We've converted at a space into at least one interesting
// successor.
if (!spaceQueue.isEmpty()) {
continue loop;
}
}
}
}
}
示例13: trimFields
import org.apache.calcite.util.ImmutableBitSet; //导入方法依赖的package包/类
/**
* Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
* {@link org.apache.calcite.rel.logical.LogicalFilter}.
*/
public TrimResult trimFields(
Filter filter,
ImmutableBitSet fieldsUsed,
Set<RelDataTypeField> extraFields) {
final RelDataType rowType = filter.getRowType();
final int fieldCount = rowType.getFieldCount();
final RexNode conditionExpr = filter.getCondition();
final RelNode input = filter.getInput();
// We use the fields used by the consumer, plus any fields used in the
// filter.
final Set<RelDataTypeField> inputExtraFields =
new LinkedHashSet<>(extraFields);
RelOptUtil.InputFinder inputFinder =
new RelOptUtil.InputFinder(inputExtraFields);
inputFinder.inputBitSet.addAll(fieldsUsed);
conditionExpr.accept(inputFinder);
final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
// Create input with trimmed columns.
TrimResult trimResult =
trimChild(filter, input, inputFieldsUsed, inputExtraFields);
RelNode newInput = trimResult.left;
final Mapping inputMapping = trimResult.right;
// If the input is unchanged, and we need to project all columns,
// there's nothing we can do.
if (newInput == input
&& fieldsUsed.cardinality() == fieldCount) {
return result(filter, Mappings.createIdentity(fieldCount));
}
// Build new project expressions, and populate the mapping.
final RexVisitor<RexNode> shuttle =
new RexPermuteInputsShuttle(inputMapping, newInput);
RexNode newConditionExpr =
conditionExpr.accept(shuttle);
// Use copy rather than relBuilder so that correlating variables get set.
relBuilder.push(
filter.copy(filter.getTraitSet(), newInput, newConditionExpr));
// The result has the same mapping as the input gave us. Sometimes we
// return fields that the consumer didn't ask for, because the filter
// needs them for its condition.
return result(relBuilder.build(), inputMapping);
}