use of org.apache.calcite.rel.RelNode in project hive by apache.
the class HivePointLookupOptimizerRule method onMatch.
public void onMatch(RelOptRuleCall call) {
final Filter filter = call.rel(0);
final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
final RexNode condition = RexUtil.pullFactors(rexBuilder, filter.getCondition());
// 1. We try to transform possible candidates
RexTransformIntoInClause transformIntoInClause = new RexTransformIntoInClause(rexBuilder, filter, minNumORClauses);
RexNode newCondition = transformIntoInClause.apply(condition);
// 2. We merge IN expressions
RexMergeInClause mergeInClause = new RexMergeInClause(rexBuilder);
newCondition = mergeInClause.apply(newCondition);
// 3. If we could not transform anything, we bail out
if (newCondition.toString().equals(condition.toString())) {
return;
}
// 4. We create the filter with the new condition
RelNode newFilter = filter.copy(filter.getTraitSet(), filter.getInput(), newCondition);
call.transformTo(newFilter);
}
use of org.apache.calcite.rel.RelNode in project hive by apache.
the class HivePreFilteringRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final Filter filter = call.rel(0);
// 0. Register that we have visited this operator in this rule
HiveRulesRegistry registry = call.getPlanner().getContext().unwrap(HiveRulesRegistry.class);
if (registry != null) {
registry.registerVisited(this, filter);
}
final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
// 1. Recompose filter possibly by pulling out common elements from DNF
// expressions
RexNode topFilterCondition = RexUtil.pullFactors(rexBuilder, filter.getCondition());
// 2. We extract possible candidates to be pushed down
List<RexNode> operandsToPushDown = new ArrayList<>();
List<RexNode> deterministicExprs = new ArrayList<>();
List<RexNode> nonDeterministicExprs = new ArrayList<>();
switch(topFilterCondition.getKind()) {
case AND:
ImmutableList<RexNode> operands = RexUtil.flattenAnd(((RexCall) topFilterCondition).getOperands());
Set<String> operandsToPushDownDigest = new HashSet<String>();
List<RexNode> extractedCommonOperands = null;
for (RexNode operand : operands) {
if (operand.getKind() == SqlKind.OR) {
extractedCommonOperands = extractCommonOperands(rexBuilder, operand, maxCNFNodeCount);
for (RexNode extractedExpr : extractedCommonOperands) {
if (operandsToPushDownDigest.add(extractedExpr.toString())) {
operandsToPushDown.add(extractedExpr);
}
}
}
// elements of DNF/CNF & extract more deterministic pieces out.
if (HiveCalciteUtil.isDeterministic(operand)) {
deterministicExprs.add(operand);
} else {
nonDeterministicExprs.add(operand);
}
}
// NOTE: Hive by convention doesn't pushdown non deterministic expressions
if (nonDeterministicExprs.size() > 0) {
for (RexNode expr : deterministicExprs) {
if (!operandsToPushDownDigest.contains(expr.toString())) {
operandsToPushDown.add(expr);
operandsToPushDownDigest.add(expr.toString());
}
}
topFilterCondition = RexUtil.pullFactors(rexBuilder, RexUtil.composeConjunction(rexBuilder, nonDeterministicExprs, false));
}
break;
case OR:
operandsToPushDown = extractCommonOperands(rexBuilder, topFilterCondition, maxCNFNodeCount);
break;
default:
return;
}
// 2. If we did not generate anything for the new predicate, we bail out
if (operandsToPushDown.isEmpty()) {
return;
}
// 3. If the new conjuncts are already present in the plan, we bail out
final List<RexNode> newConjuncts = HiveCalciteUtil.getPredsNotPushedAlready(filter.getInput(), operandsToPushDown);
RexNode newPredicate = RexUtil.composeConjunction(rexBuilder, newConjuncts, false);
if (newPredicate.isAlwaysTrue()) {
return;
}
// 4. Otherwise, we create a new condition
final RexNode newChildFilterCondition = RexUtil.pullFactors(rexBuilder, newPredicate);
// 5. We create the new filter that might be pushed down
RelNode newChildFilter = filterFactory.createFilter(filter.getInput(), newChildFilterCondition);
RelNode newTopFilter = filterFactory.createFilter(newChildFilter, topFilterCondition);
// 6. We register both so we do not fire the rule on them again
if (registry != null) {
registry.registerVisited(this, newChildFilter);
registry.registerVisited(this, newTopFilter);
}
call.transformTo(newTopFilter);
}
use of org.apache.calcite.rel.RelNode in project hive by apache.
the class HiveProjectFilterPullUpConstantsRule method onMatch.
public void onMatch(RelOptRuleCall call) {
final Project project = call.rel(0);
final Filter filter = call.rel(1);
final RelBuilder builder = call.builder();
List<RexNode> projects = project.getChildExps();
List<RexNode> newProjects = rewriteProjects(projects, filter.getCondition(), builder);
if (newProjects == null) {
return;
}
RelNode newProjRel = builder.push(filter).project(newProjects, project.getRowType().getFieldNames()).build();
call.transformTo(newProjRel);
}
use of org.apache.calcite.rel.RelNode in project hive by apache.
the class HiveProjectSortTransposeRule method onMatch.
//~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
final HiveProject project = call.rel(0);
final HiveSortLimit sort = call.rel(1);
// Determine mapping between project input and output fields. If sort
// relies on non-trivial expressions, we can't push.
final Mappings.TargetMapping map = RelOptUtil.permutation(project.getProjects(), project.getInput().getRowType()).inverse();
for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
if (map.getTarget(fc.getFieldIndex()) < 0) {
return;
}
}
// Create new collation
final RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize(RexUtil.apply(map, sort.getCollation()));
// New operators
final RelNode newProject = project.copy(sort.getInput().getTraitSet(), ImmutableList.<RelNode>of(sort.getInput()));
final HiveSortLimit newSort = sort.copy(newProject.getTraitSet(), newProject, newCollation, sort.offset, sort.fetch);
call.transformTo(newSort);
}
use of org.apache.calcite.rel.RelNode in project hive by apache.
the class HiveFilter method traverseFilter.
//traverse the given node to find all correlated variables
// Note that correlated variables are supported in Filter only i.e. Where & Having
private static void traverseFilter(RexNode node, Set<CorrelationId> allVars) {
if (node instanceof RexSubQuery) {
//we expect correlated variables in HiveFilter only for now.
// Also check for case where operator has 0 inputs .e.g TableScan
RelNode input = ((RexSubQuery) node).rel.getInput(0);
while (input != null && !(input instanceof HiveFilter) && input.getInputs().size() >= 1) {
// we only expect cor vars in top level filter
if (input.getInputs().size() > 1) {
return;
}
input = input.getInput(0);
}
if (input != null && input instanceof HiveFilter) {
findCorrelatedVar(((HiveFilter) input).getCondition(), allVars);
}
return;
}
//AND, NOT etc
if (node instanceof RexCall) {
int numOperands = ((RexCall) node).getOperands().size();
for (int i = 0; i < numOperands; i++) {
RexNode op = ((RexCall) node).getOperands().get(i);
traverseFilter(op, allVars);
}
}
}
Aggregations