Search in sources :

Example 1 with HiveProject

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

the class HiveAggregateProjectMergeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final HiveAggregate aggregate = call.rel(0);
    final HiveProject project = call.rel(1);
    RelNode x = apply(aggregate, project);
    if (x != null) {
        call.transformTo(x);
    }
}
Also used : HiveAggregate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)

Example 2 with HiveProject

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

the class PlanModifierUtil method fixTopOBSchema.

protected static void fixTopOBSchema(final RelNode rootRel, Pair<RelNode, RelNode> topSelparentPair, List<FieldSchema> resultSchema, boolean replaceProject) throws CalciteSemanticException {
    if (!(topSelparentPair.getKey() instanceof Sort) || !HiveCalciteUtil.orderRelNode(topSelparentPair.getKey())) {
        return;
    }
    HiveSortLimit obRel = (HiveSortLimit) topSelparentPair.getKey();
    Project obChild = (Project) topSelparentPair.getValue();
    if (obChild.getRowType().getFieldCount() <= resultSchema.size()) {
        return;
    }
    RelDataType rt = obChild.getRowType();
    @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++) {
        if (collationInputRefs.contains(i)) {
            RexNode obyExpr = obChild.getProjects().get(i);
            if (obyExpr instanceof RexCall) {
                LOG.debug("Old RexCall : " + obyExpr);
                obyExpr = adjustOBSchema((RexCall) obyExpr, obChild, resultSchema);
                LOG.debug("New RexCall : " + obyExpr);
            }
            inputRefToCallMapBldr.put(i, obyExpr);
        }
    }
    ImmutableMap<Integer, RexNode> inputRefToCallMap = inputRefToCallMapBldr.build();
    if ((obChild.getRowType().getFieldCount() - inputRefToCallMap.size()) != resultSchema.size()) {
        LOG.error(generateInvalidSchemaMessage(obChild, resultSchema, inputRefToCallMap.size()));
        throw new CalciteSemanticException("Result Schema didn't match Optimized Op Tree Schema");
    }
    if (replaceProject) {
        // This removes order-by only expressions from the projections.
        HiveProject replacementProjectRel = HiveProject.create(obChild.getInput(), obChild.getProjects().subList(0, resultSchema.size()), obChild.getRowType().getFieldNames().subList(0, resultSchema.size()));
        obRel.replaceInput(0, replacementProjectRel);
    }
    obRel.setInputRefToCallMap(inputRefToCallMap);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) HiveSortLimit(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit) ImmutableMap(com.google.common.collect.ImmutableMap) RexCall(org.apache.calcite.rex.RexCall) Project(org.apache.calcite.rel.core.Project) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) Sort(org.apache.calcite.rel.core.Sort) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException) HashSet(java.util.HashSet) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with HiveProject

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

the class PlanModifierForReturnPath method introduceProjectIfNeeded.

private static RelNode introduceProjectIfNeeded(RelNode optimizedOptiqPlan, List<FieldSchema> resultSchema) throws CalciteSemanticException {
    List<String> fieldNames = new ArrayList<>();
    for (FieldSchema fieldSchema : resultSchema) {
        fieldNames.add(fieldSchema.getName());
    }
    RelNode newRoot = null;
    List<RexNode> projectList = null;
    if (!(optimizedOptiqPlan instanceof Project)) {
        projectList = HiveCalciteUtil.getProjsFromBelowAsInputRef(optimizedOptiqPlan);
        newRoot = HiveProject.create(optimizedOptiqPlan, projectList, fieldNames);
    } else {
        HiveProject project = (HiveProject) optimizedOptiqPlan;
        newRoot = HiveProject.create(project.getInput(0), project.getProjects(), fieldNames);
    }
    return newRoot;
}
Also used : Project(org.apache.calcite.rel.core.Project) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with HiveProject

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

the class HiveProjectSortExchangeTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    final HiveProject project = call.rel(0);
    final HiveSortExchange sortExchange = call.rel(1);
    final RelOptCluster cluster = project.getCluster();
    List<RelFieldCollation> fieldCollations = getNewRelFieldCollations(project, sortExchange.getCollation(), cluster);
    if (fieldCollations == null) {
        return;
    }
    RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize(RelCollationImpl.of(fieldCollations));
    List<Integer> newDistributionKeys = getNewRelDistributionKeys(project, sortExchange.getDistribution());
    RelDistribution newDistribution = RelDistributionTraitDef.INSTANCE.canonize(new HiveRelDistribution(sortExchange.getDistribution().getType(), newDistributionKeys));
    RelTraitSet newTraitSet = TraitsUtil.getDefaultTraitSet(sortExchange.getCluster()).replace(newCollation).replace(newDistribution);
    // New operators
    final RelNode newProject = project.copy(sortExchange.getInput().getTraitSet(), ImmutableList.of(sortExchange.getInput()));
    final SortExchange newSort = sortExchange.copy(newTraitSet, newProject, newDistribution, newCollation);
    call.transformTo(newSort);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelTraitSet(org.apache.calcite.plan.RelTraitSet) HiveSortExchange(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortExchange) SortExchange(org.apache.calcite.rel.core.SortExchange) HiveSortExchange(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortExchange) HiveRelDistribution(org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelDistribution) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelDistribution(org.apache.calcite.rel.RelDistribution) HiveRelDistribution(org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelDistribution)

Example 5 with HiveProject

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

the class HiveSubQueryRemoveRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final RelNode relNode = call.rel(0);
    final RelBuilder builder = call.builder();
    // if subquery is in FILTER
    if (relNode instanceof HiveFilter) {
        final HiveFilter 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();
        SubqueryConf subqueryConfig = filter.getCluster().getPlanner().getContext().unwrap(SubqueryConf.class);
        boolean isCorrScalarQuery = subqueryConfig.getCorrScalarRexSQWithAgg().contains(e.rel);
        final RexNode target = apply(call.getMetadataQuery(), e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.filter(shuttle.apply(filter.getCondition()));
        builder.project(fields(builder, filter.getRowType().getFieldCount()));
        RelNode newRel = builder.build();
        call.transformTo(newRel);
    } else if (relNode instanceof HiveProject) {
        // if subquery is in PROJECT
        final HiveProject 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);
        final RexNode target = apply(call.getMetadataQuery(), e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.project(shuttle.apply(project.getProjects()), project.getRowType().getFieldNames());
        call.transformTo(builder.build());
    }
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) 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) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)14 RelNode (org.apache.calcite.rel.RelNode)8 RexNode (org.apache.calcite.rex.RexNode)8 ArrayList (java.util.ArrayList)3 RelCollation (org.apache.calcite.rel.RelCollation)3 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)3 HiveSortLimit (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 Project (org.apache.calcite.rel.core.Project)2 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)2 HiveJdbcConverter (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.jdbc.HiveJdbcConverter)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashSet (java.util.HashSet)1 JdbcProject (org.apache.calcite.adapter.jdbc.JdbcRules.JdbcProject)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1 RelOptUtil (org.apache.calcite.plan.RelOptUtil)1 RelDistribution (org.apache.calcite.rel.RelDistribution)1 Sort (org.apache.calcite.rel.core.Sort)1 SortExchange (org.apache.calcite.rel.core.SortExchange)1