use of org.apache.calcite.rel.metadata.RelMetadataQuery in project hive by apache.
the class HiveJoin method getSortedInputs.
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();
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project hive by apache.
the class HiveExpandDistinctAggregatesRule method onMatch.
// ~ Methods ----------------------------------------------------------------
@Override
public void onMatch(RelOptRuleCall call) {
final Aggregate aggregate = call.rel(0);
int numCountDistinct = getNumCountDistinctCall(aggregate);
if (numCountDistinct == 0) {
return;
}
// Find all of the agg expressions. We use a List (for all count(distinct))
// as well as a Set (for all others) to ensure determinism.
int nonDistinctCount = 0;
List<List<Integer>> argListList = new ArrayList<List<Integer>>();
Set<List<Integer>> argListSets = new LinkedHashSet<List<Integer>>();
Set<Integer> positions = new HashSet<>();
for (AggregateCall aggCall : aggregate.getAggCallList()) {
if (!aggCall.isDistinct()) {
++nonDistinctCount;
continue;
}
ArrayList<Integer> argList = new ArrayList<Integer>();
for (Integer arg : aggCall.getArgList()) {
argList.add(arg);
positions.add(arg);
}
// Aggr checks for sorted argList.
argListList.add(argList);
argListSets.add(argList);
}
Util.permAssert(argListSets.size() > 0, "containsDistinctCall lied");
if (numCountDistinct > 1 && numCountDistinct == aggregate.getAggCallList().size() && aggregate.getGroupSet().isEmpty()) {
LOG.debug("Trigger countDistinct rewrite. numCountDistinct is " + numCountDistinct);
// now positions contains all the distinct positions, i.e., $5, $4, $6
// we need to first sort them as group by set
// and then get their position later, i.e., $4->1, $5->2, $6->3
cluster = aggregate.getCluster();
rexBuilder = cluster.getRexBuilder();
RelNode converted = null;
List<Integer> sourceOfForCountDistinct = new ArrayList<>();
sourceOfForCountDistinct.addAll(positions);
Collections.sort(sourceOfForCountDistinct);
try {
converted = convert(aggregate, argListList, sourceOfForCountDistinct);
} catch (CalciteSemanticException e) {
LOG.debug(e.toString());
throw new RuntimeException(e);
}
call.transformTo(converted);
return;
}
// If all of the agg expressions are distinct and have the same
// arguments then we can use a more efficient form.
final RelMetadataQuery mq = call.getMetadataQuery();
if ((nonDistinctCount == 0) && (argListSets.size() == 1)) {
for (Integer arg : argListSets.iterator().next()) {
Set<RelColumnOrigin> colOrigs = mq.getColumnOrigins(aggregate, arg);
if (null != colOrigs) {
for (RelColumnOrigin colOrig : colOrigs) {
RelOptHiveTable hiveTbl = (RelOptHiveTable) colOrig.getOriginTable();
if (hiveTbl.getPartColInfoMap().containsKey(colOrig.getOriginColumnOrdinal())) {
// Encountered partitioning column, this will be better handled by MetadataOnly optimizer.
return;
}
}
}
}
RelNode converted = convertMonopole(aggregate, argListSets.iterator().next());
call.transformTo(converted);
return;
}
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project hive by apache.
the class HiveReduceExpressionsWithStatsRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final Filter filter = call.rel(0);
final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
final RelMetadataQuery metadataProvider = call.getMetadataQuery();
// 1. Recompose filter possibly by pulling out common elements from DNF
// expressions
RexNode newFilterCondition = RexUtil.pullFactors(rexBuilder, filter.getCondition());
// 2. Reduce filter with stats information
RexReplacer replacer = new RexReplacer(filter, rexBuilder, metadataProvider);
newFilterCondition = replacer.apply(newFilterCondition);
// 3. Transform if we have created a new filter operator
if (!filter.getCondition().toString().equals(newFilterCondition.toString())) {
Filter newFilter = filter.copy(filter.getTraitSet(), filter.getInput(), newFilterCondition);
call.transformTo(newFilter);
}
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class EnumerableValues method create.
/**
* Creates an EnumerableValues.
*/
public static EnumerableValues create(RelOptCluster cluster, final RelDataType rowType, final ImmutableList<ImmutableList<RexLiteral>> tuples) {
final RelMetadataQuery mq = cluster.getMetadataQuery();
final RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
public List<RelCollation> get() {
return RelMdCollation.values(mq, rowType, tuples);
}
}).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {
public RelDistribution get() {
return RelMdDistribution.values(rowType, tuples);
}
});
return new EnumerableValues(cluster, rowType, tuples, traitSet);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class EnumerableFilter method create.
/**
* Creates an EnumerableFilter.
*/
public static EnumerableFilter create(final RelNode input, RexNode condition) {
final RelOptCluster cluster = input.getCluster();
final RelMetadataQuery mq = cluster.getMetadataQuery();
final RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
public List<RelCollation> get() {
return RelMdCollation.filter(mq, input);
}
}).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {
public RelDistribution get() {
return RelMdDistribution.filter(mq, input);
}
});
return new EnumerableFilter(cluster, traitSet, input, condition);
}
Aggregations