private Statistic buildStatistic() { if (stats != null || primaryKey == -1) { return stats; } Direction dir = primaryKeyMonotonicity == INCREASING ? ASCENDING : DESCENDING; RelFieldCollation collation = new RelFieldCollation(primaryKey, dir, NullDirection.UNSPECIFIED); return Statistics.of(fields.size(), ImmutableList.of(ImmutableBitSet.of(primaryKey)), ImmutableList.of(RelCollations.of(collation))); }
public ImmutableBitSet getSortedInputs() throws CalciteSemanticException { ImmutableBitSet.Builder sortedInputsBuilder = ImmutableBitSet.builder(); JoinPredicateInfo joinPredInfo = HiveCalciteUtil.JoinPredicateInfo. constructJoinPredicateInfo(this); List<ImmutableIntList> joinKeysInChildren = new ArrayList<ImmutableIntList>(); joinKeysInChildren.add( ImmutableIntList.copyOf( joinPredInfo.getProjsFromLeftPartOfJoinKeysInChildSchema())); joinKeysInChildren.add( ImmutableIntList.copyOf( joinPredInfo.getProjsFromRightPartOfJoinKeysInChildSchema())); final RelMetadataQuery mq = this.left.getCluster().getMetadataQuery(); for (int i=0; i<this.getInputs().size(); i++) { boolean correctOrderFound = RelCollations.contains( mq.collations(this.getInputs().get(i)), joinKeysInChildren.get(i)); if (correctOrderFound) { sortedInputsBuilder.set(i); } } return sortedInputsBuilder.build(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) Set<Integer> collationInputRefs = new HashSet( RelCollations.ordinals(obRel.getCollation())); ImmutableMap.Builder<Integer, RexNode> inputRefToCallMapBldr = ImmutableMap.builder(); for (int i = resultSchema.size(); i < rt.getFieldCount(); i++) {
@Test public void testCollationContains() { final RelCollation collation21 = RelCollations.of( new RelFieldCollation(2, RelFieldCollation.Direction.ASCENDING), new RelFieldCollation(1, RelFieldCollation.Direction.DESCENDING)); assertThat(RelCollations.contains(collation21, Arrays.asList(2)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(0)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 0)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 3)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList()), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 2)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 1)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(1, 2, 1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(1, 1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 2)), is(true));
/** Helper method to determine a {@link Join}'s collation assuming that it * uses a merge-join algorithm. * * <p>If the inputs are sorted on other keys <em>in addition to</em> the join * key, the result preserves those collations too. */ public static List<RelCollation> mergeJoin(RelMetadataQuery mq, RelNode left, RelNode right, ImmutableIntList leftKeys, ImmutableIntList rightKeys) { final ImmutableList.Builder<RelCollation> builder = ImmutableList.builder(); final ImmutableList<RelCollation> leftCollations = mq.collations(left); assert RelCollations.contains(leftCollations, leftKeys) : "cannot merge join: left input is not sorted on left keys"; builder.addAll(leftCollations); final ImmutableList<RelCollation> rightCollations = mq.collations(right); assert RelCollations.contains(rightCollations, rightKeys) : "cannot merge join: right input is not sorted on right keys"; final int leftFieldCount = left.getRowType().getFieldCount(); for (RelCollation collation : rightCollations) { builder.add(RelCollations.shift(collation, leftFieldCount)); } return builder.build(); } }
@Deprecated // to be removed before 2.0 public static List<RelCollation> createSingleton(int fieldIndex) { return RelCollations.createSingleton(fieldIndex); }
@Override public void onMatch(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); final HiveJoin join = call.rel(1); RelNode inputLeft = join.getLeft(); RelNode inputRight = join.getRight(); // We create a new sort operator on the corresponding input if (join.getJoinType() == JoinRelType.LEFT) { inputLeft = sortLimit.copy(sortLimit.getTraitSet(), inputLeft, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputLeft).setRuleCreated(true); } else { // Adjust right collation final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sortLimit.getCollation(), -join.getLeft().getRowType().getFieldCount())); inputRight = sortLimit.copy(sortLimit.getTraitSet().replace(rightCollation), inputRight, rightCollation, sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputRight).setRuleCreated(true); } // We copy the join and the top sort operator RelNode result = join.copy(join.getTraitSet(), join.getCondition(), inputLeft, inputRight, join.getJoinType(), join.isSemiJoinDone()); result = sortLimit.copy(sortLimit.getTraitSet(), result, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); call.transformTo(result); }
@Deprecated // to be removed before 2.0 public static boolean equal( List<RelCollation> collationList1, List<RelCollation> collationList2) { return RelCollations.equal(collationList1, collationList2); }
@Test public void testCollationContains() { final RelCollation collation21 = RelCollations.of( new RelFieldCollation(2, RelFieldCollation.Direction.ASCENDING), new RelFieldCollation(1, RelFieldCollation.Direction.DESCENDING)); assertThat(RelCollations.contains(collation21, Arrays.asList(2)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(0)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 0)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 3)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList()), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 2)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 1, 1)), is(true)); assertThat(RelCollations.contains(collation21, Arrays.asList(1, 2, 1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(1, 1)), is(false)); assertThat(RelCollations.contains(collation21, Arrays.asList(2, 2)), is(true));
/** Helper method to determine a {@link Join}'s collation assuming that it * uses a merge-join algorithm. * * <p>If the inputs are sorted on other keys <em>in addition to</em> the join * key, the result preserves those collations too. */ public static List<RelCollation> mergeJoin(RelMetadataQuery mq, RelNode left, RelNode right, ImmutableIntList leftKeys, ImmutableIntList rightKeys) { final ImmutableList.Builder<RelCollation> builder = ImmutableList.builder(); final ImmutableList<RelCollation> leftCollations = mq.collations(left); assert RelCollations.contains(leftCollations, leftKeys) : "cannot merge join: left input is not sorted on left keys"; builder.addAll(leftCollations); final ImmutableList<RelCollation> rightCollations = mq.collations(right); assert RelCollations.contains(rightCollations, rightKeys) : "cannot merge join: right input is not sorted on right keys"; final int leftFieldCount = left.getRowType().getFieldCount(); for (RelCollation collation : rightCollations) { builder.add(RelCollations.shift(collation, leftFieldCount)); } return builder.build(); } }
@Deprecated // to be removed before 2.0 public static List<RelCollation> createSingleton(int fieldIndex) { return RelCollations.createSingleton(fieldIndex); }
@Override public void onMatch(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); final HiveJoin join = call.rel(1); RelNode inputLeft = join.getLeft(); RelNode inputRight = join.getRight(); // We create a new sort operator on the corresponding input if (join.getJoinType() == JoinRelType.LEFT) { inputLeft = sortLimit.copy(sortLimit.getTraitSet(), inputLeft, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputLeft).setRuleCreated(true); } else { // Adjust right collation final RelCollation rightCollation = RelCollationTraitDef.INSTANCE.canonize( RelCollations.shift(sortLimit.getCollation(), -join.getLeft().getRowType().getFieldCount())); inputRight = sortLimit.copy(sortLimit.getTraitSet().replace(rightCollation), inputRight, rightCollation, sortLimit.offset, sortLimit.fetch); ((HiveSortLimit) inputRight).setRuleCreated(true); } // We copy the join and the top sort operator RelNode result = join.copy(join.getTraitSet(), join.getCondition(), inputLeft, inputRight, join.getJoinType(), join.isSemiJoinDone()); result = sortLimit.copy(sortLimit.getTraitSet(), result, sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch); call.transformTo(result); }
@Deprecated // to be removed before 2.0 public static boolean equal( List<RelCollation> collationList1, List<RelCollation> collationList2) { return RelCollations.equal(collationList1, collationList2); }
/** * Empty relationship can be expressed in many different ways, e.g., * filter(cond=false), empty LogicalValues(), etc. Calcite default implementation * uses empty LogicalValues(); however, currently there is not an equivalent to * this expression in Hive. Thus, we use limit 0, since Hive already includes * optimizations that will do early pruning of the result tree when it is found, * e.g., GlobalLimitOptimizer. */ public HiveSubQRemoveRelBuilder empty() { final RelNode input = build(); final RelNode sort = HiveRelFactories.HIVE_SORT_FACTORY.createSort( input, RelCollations.of(), null, literal(0)); return this.push(sort); }
public ImmutableBitSet getSortedInputs() throws CalciteSemanticException { ImmutableBitSet.Builder sortedInputsBuilder = ImmutableBitSet.builder(); JoinPredicateInfo joinPredInfo = HiveCalciteUtil.JoinPredicateInfo. constructJoinPredicateInfo(this); List<ImmutableIntList> joinKeysInChildren = new ArrayList<ImmutableIntList>(); joinKeysInChildren.add( ImmutableIntList.copyOf( joinPredInfo.getProjsFromLeftPartOfJoinKeysInChildSchema())); joinKeysInChildren.add( ImmutableIntList.copyOf( joinPredInfo.getProjsFromRightPartOfJoinKeysInChildSchema())); for (int i=0; i<this.getInputs().size(); i++) { boolean correctOrderFound = RelCollations.contains( RelMetadataQuery.instance().collations(this.getInputs().get(i)), joinKeysInChildren.get(i)); if (correctOrderFound) { sortedInputsBuilder.set(i); } } return sortedInputsBuilder.build(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) Set<Integer> collationInputRefs = new HashSet( RelCollations.ordinals(obRel.getCollation())); ImmutableMap.Builder<Integer, RexNode> inputRefToCallMapBldr = ImmutableMap.builder(); for (int i = resultSchema.size(); i < rt.getFieldCount(); i++) {