Search in sources :

Example 6 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 7 with HiveProject

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

the class JDBCProjectPushDownRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    final HiveProject project = call.rel(0);
    final HiveJdbcConverter conv = call.rel(1);
    for (RexNode currProject : project.getProjects()) {
        if (!JDBCRexCallValidator.isValidJdbcOperation(currProject, conv.getJdbcDialect())) {
            return false;
        }
        if (!validDataType(conv.getJdbcDialect(), currProject)) {
            return false;
        }
    }
    return true;
}
Also used : HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) HiveJdbcConverter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.jdbc.HiveJdbcConverter) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with HiveProject

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

the class HiveSortProjectTransposeRule method onMatch.

// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    final HiveSortLimit sort = call.rel(0);
    final HiveProject project = 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());
    for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
        if (map.getTargetOpt(fc.getFieldIndex()) < 0) {
            return;
        }
    }
    // Create new collation
    final RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize(RexUtil.apply(map, sort.getCollation()));
    // New operators
    final HiveSortLimit newSort = sort.copy(sort.getTraitSet().replace(newCollation), project.getInput(), newCollation, sort.offset, sort.fetch);
    final RelNode newProject = project.copy(sort.getTraitSet(), ImmutableList.<RelNode>of(newSort));
    call.transformTo(newProject);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) Mappings(org.apache.calcite.util.mapping.Mappings) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) HiveSortLimit(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit)

Example 9 with HiveProject

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

the class PlanModifierForASTConv method renameTopLevelSelectInResultSchema.

public static RelNode renameTopLevelSelectInResultSchema(final RelNode rootRel, Pair<RelNode, RelNode> topSelparentPair, List<FieldSchema> resultSchema) throws CalciteSemanticException {
    RelNode parentOforiginalProjRel = topSelparentPair.getKey();
    HiveProject originalProjRel = (HiveProject) topSelparentPair.getValue();
    // Assumption: top portion of tree could only be
    // (limit)?(OB)?(Project)....
    List<RexNode> rootChildExps = originalProjRel.getProjects();
    if (resultSchema.size() != rootChildExps.size()) {
        // Safeguard against potential issues in CBO RowResolver construction. Disable CBO for now.
        LOG.error(PlanModifierUtil.generateInvalidSchemaMessage(originalProjRel, resultSchema, 0));
        throw new CalciteSemanticException("Result Schema didn't match Optimized Op Tree Schema");
    }
    List<String> newSelAliases = new ArrayList<String>();
    String colAlias;
    for (int i = 0; i < rootChildExps.size(); i++) {
        colAlias = resultSchema.get(i).getName();
        colAlias = getNewColAlias(newSelAliases, colAlias);
        newSelAliases.add(colAlias);
    }
    HiveProject replacementProjectRel = HiveProject.create(originalProjRel.getInput(), originalProjRel.getProjects(), newSelAliases);
    if (rootRel == originalProjRel) {
        return replacementProjectRel;
    } else {
        parentOforiginalProjRel.replaceInput(0, replacementProjectRel);
        return rootRel;
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) ArrayList(java.util.ArrayList) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException) RexNode(org.apache.calcite.rex.RexNode)

Example 10 with HiveProject

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

the class PlanModifierForASTConv method introduceDerivedTable.

private static RelNode introduceDerivedTable(final RelNode rel) {
    List<RexNode> projectList = HiveCalciteUtil.getProjsFromBelowAsInputRef(rel);
    HiveProject select = HiveProject.create(rel.getCluster(), rel, projectList, rel.getRowType(), Collections.emptyList());
    return select;
}
Also used : HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) 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