Search in sources :

Example 1 with HiveSubQRemoveRelBuilder

use of org.apache.hadoop.hive.ql.optimizer.calcite.HiveSubQRemoveRelBuilder in project hive by apache.

the class HiveSubQueryRemoveRule method onMatch.

public void onMatch(RelOptRuleCall call) {
    final RelNode relNode = call.rel(0);
    final HiveSubQRemoveRelBuilder builder = new HiveSubQRemoveRelBuilder(null, call.rel(0).getCluster(), null);
    // if subquery is in FILTER
    if (relNode instanceof Filter) {
        final Filter filter = call.rel(0);
        final RexSubQuery e = RexUtil.SubQueryFinder.find(filter.getCondition());
        assert e != null;
        final RelOptUtil.Logic logic = LogicVisitor.find(RelOptUtil.Logic.TRUE, ImmutableList.of(filter.getCondition()), e);
        builder.push(filter.getInput());
        final int fieldCount = builder.peek().getRowType().getFieldCount();
        assert (filter instanceof HiveFilter);
        SubqueryConf subqueryConfig = filter.getCluster().getPlanner().getContext().unwrap(SubqueryConf.class);
        boolean isCorrScalarQuery = subqueryConfig.getCorrScalarRexSQWithAgg().contains(e.rel);
        boolean hasNoWindowingAndNoGby = subqueryConfig.getScalarAggWithoutGbyWindowing().contains(e.rel);
        final RexNode target = apply(e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery, hasNoWindowingAndNoGby);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.filter(shuttle.apply(filter.getCondition()));
        builder.project(fields(builder, filter.getRowType().getFieldCount()));
        call.transformTo(builder.build());
    } else if (relNode instanceof Project) {
        // if subquery is in PROJECT
        final Project project = call.rel(0);
        final RexSubQuery e = RexUtil.SubQueryFinder.find(project.getProjects());
        assert e != null;
        final RelOptUtil.Logic logic = LogicVisitor.find(RelOptUtil.Logic.TRUE_FALSE_UNKNOWN, project.getProjects(), e);
        builder.push(project.getInput());
        final int fieldCount = builder.peek().getRowType().getFieldCount();
        SubqueryConf subqueryConfig = project.getCluster().getPlanner().getContext().unwrap(SubqueryConf.class);
        boolean isCorrScalarQuery = subqueryConfig.getCorrScalarRexSQWithAgg().contains(e.rel);
        boolean hasNoWindowingAndNoGby = subqueryConfig.getScalarAggWithoutGbyWindowing().contains(e.rel);
        final RexNode target = apply(e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery, hasNoWindowingAndNoGby);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.project(shuttle.apply(project.getProjects()), project.getRowType().getFieldNames());
        call.transformTo(builder.build());
    }
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) RelOptUtil(org.apache.calcite.plan.RelOptUtil) SubqueryConf(org.apache.hadoop.hive.ql.optimizer.calcite.SubqueryConf) HiveFilter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter) Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) HiveFilter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter) HiveSubQRemoveRelBuilder(org.apache.hadoop.hive.ql.optimizer.calcite.HiveSubQRemoveRelBuilder) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelOptUtil (org.apache.calcite.plan.RelOptUtil)1 RelNode (org.apache.calcite.rel.RelNode)1 Filter (org.apache.calcite.rel.core.Filter)1 Project (org.apache.calcite.rel.core.Project)1 RexNode (org.apache.calcite.rex.RexNode)1 RexShuttle (org.apache.calcite.rex.RexShuttle)1 RexSubQuery (org.apache.calcite.rex.RexSubQuery)1 HiveSubQRemoveRelBuilder (org.apache.hadoop.hive.ql.optimizer.calcite.HiveSubQRemoveRelBuilder)1 SubqueryConf (org.apache.hadoop.hive.ql.optimizer.calcite.SubqueryConf)1 HiveFilter (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter)1