Search in sources :

Example 1 with OrderExpression

use of org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression in project hive by apache.

the class PTFTranslator method buildOrderExpressions.

/**
   * Collect order expressions for RANGE based windowing
   * @throws SemanticException
   */
private OrderDef buildOrderExpressions(ShapeDetails inpShape, List<OrderExpression> orderExpressions) throws SemanticException {
    OrderDef orderDef = new OrderDef();
    for (OrderExpression oe : orderExpressions) {
        PTFTranslator.validateNoLeadLagInValueBoundarySpec(oe.getExpression());
        PTFExpressionDef exprDef = null;
        try {
            exprDef = buildExpressionDef(inpShape, oe.getExpression());
        } catch (HiveException he) {
            throw new SemanticException(he);
        }
        PTFTranslator.validateValueBoundaryExprType(exprDef.getOI());
        OrderExpressionDef orderExprDef = new OrderExpressionDef(exprDef);
        orderExprDef.setOrder(oe.getOrder());
        orderExprDef.setNullOrder(oe.getNullOrder());
        orderDef.addExpression(orderExprDef);
    }
    return orderDef;
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) OrderExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression) PTFExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef) OrderDef(org.apache.hadoop.hive.ql.plan.ptf.OrderDef) OrderExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.OrderExpressionDef)

Example 2 with OrderExpression

use of org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression in project hive by apache.

the class SemanticAnalyzer method genReduceSinkPlanForWindowing.

private Operator genReduceSinkPlanForWindowing(WindowingSpec spec, RowResolver inputRR, Operator input) throws SemanticException {
    ArrayList<ExprNodeDesc> partCols = new ArrayList<ExprNodeDesc>();
    ArrayList<ExprNodeDesc> orderCols = new ArrayList<ExprNodeDesc>();
    StringBuilder order = new StringBuilder();
    StringBuilder nullOrder = new StringBuilder();
    for (PartitionExpression partCol : spec.getQueryPartitionSpec().getExpressions()) {
        ExprNodeDesc partExpr = genExprNodeDesc(partCol.getExpression(), inputRR);
        if (ExprNodeDescUtils.indexOf(partExpr, partCols) < 0) {
            partCols.add(partExpr);
            orderCols.add(partExpr);
            order.append('+');
            nullOrder.append('a');
        }
    }
    if (spec.getQueryOrderSpec() != null) {
        for (OrderExpression orderCol : spec.getQueryOrderSpec().getExpressions()) {
            ExprNodeDesc orderExpr = genExprNodeDesc(orderCol.getExpression(), inputRR);
            char orderChar = orderCol.getOrder() == PTFInvocationSpec.Order.ASC ? '+' : '-';
            char nullOrderChar = orderCol.getNullOrder() == PTFInvocationSpec.NullOrder.NULLS_FIRST ? 'a' : 'z';
            int index = ExprNodeDescUtils.indexOf(orderExpr, orderCols);
            if (index >= 0) {
                order.setCharAt(index, orderChar);
                nullOrder.setCharAt(index, nullOrderChar);
                continue;
            }
            orderCols.add(genExprNodeDesc(orderCol.getExpression(), inputRR));
            order.append(orderChar);
            nullOrder.append(nullOrderChar);
        }
    }
    return genReduceSinkPlan(input, partCols, orderCols, order.toString(), nullOrder.toString(), -1, Operation.NOT_ACID);
}
Also used : PartitionExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression) OrderExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression) ArrayList(java.util.ArrayList) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc)

Example 3 with OrderExpression

use of org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression in project hive by apache.

the class SemanticAnalyzer method processOrderSpec.

private OrderSpec processOrderSpec(ASTNode sortNode) {
    OrderSpec oSpec = new OrderSpec();
    int exprCnt = sortNode.getChildCount();
    for (int i = 0; i < exprCnt; i++) {
        OrderExpression exprSpec = new OrderExpression();
        ASTNode orderSpec = (ASTNode) sortNode.getChild(i);
        ASTNode nullOrderSpec = (ASTNode) orderSpec.getChild(0);
        exprSpec.setExpression((ASTNode) nullOrderSpec.getChild(0));
        if (orderSpec.getType() == HiveParser.TOK_TABSORTCOLNAMEASC) {
            exprSpec.setOrder(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.Order.ASC);
        } else {
            exprSpec.setOrder(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.Order.DESC);
        }
        if (nullOrderSpec.getType() == HiveParser.TOK_NULLS_FIRST) {
            exprSpec.setNullOrder(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.NullOrder.NULLS_FIRST);
        } else {
            exprSpec.setNullOrder(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.NullOrder.NULLS_LAST);
        }
        oSpec.addExpression(exprSpec);
    }
    return oSpec;
}
Also used : OrderSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec) OrderExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression)

Example 4 with OrderExpression

use of org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression in project hive by apache.

the class PTFTranslator method addPartitionExpressionsToOrderList.

protected static ArrayList<OrderExpression> addPartitionExpressionsToOrderList(ArrayList<PartitionExpression> partCols, ArrayList<OrderExpression> orderCols) throws SemanticException {
    int numOfPartColumns = 0;
    int chkSize = partCols.size();
    chkSize = chkSize > orderCols.size() ? orderCols.size() : chkSize;
    for (int i = 0; i < chkSize; i++) {
        if (orderCols.get(i).getExpression().toStringTree().equals(partCols.get(i).getExpression().toStringTree())) {
            numOfPartColumns++;
        } else {
            break;
        }
    }
    if (numOfPartColumns != 0 && numOfPartColumns != partCols.size()) {
        List<String> partitionColumnNames = new ArrayList<String>();
        for (PartitionExpression partitionExpression : partCols) {
            ASTNode column = partitionExpression.getExpression();
            if (column != null && column.getChildCount() > 0) {
                partitionColumnNames.add(column.getChild(0).getText());
            }
        }
        throw new SemanticException(String.format("all partition columns %s must be in order clause or none should be specified", partitionColumnNames.toString()));
    }
    ArrayList<OrderExpression> combinedOrdExprs = new ArrayList<OrderExpression>();
    if (numOfPartColumns == 0) {
        for (PartitionExpression partCol : partCols) {
            OrderExpression orderCol = new OrderExpression(partCol);
            combinedOrdExprs.add(orderCol);
        }
    }
    combinedOrdExprs.addAll(orderCols);
    return combinedOrdExprs;
}
Also used : PartitionExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression) OrderExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression) ArrayList(java.util.ArrayList)

Example 5 with OrderExpression

use of org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression in project hive by apache.

the class HiveOpConverter method genPTF.

private OpAttr genPTF(OpAttr inputOpAf, WindowingSpec wSpec) throws SemanticException {
    Operator<?> input = inputOpAf.inputs.get(0);
    wSpec.validateAndMakeEffective();
    WindowingComponentizer groups = new WindowingComponentizer(wSpec);
    RowResolver rr = new RowResolver();
    for (ColumnInfo ci : input.getSchema().getSignature()) {
        rr.put(inputOpAf.tabAlias, ci.getInternalName(), ci);
    }
    while (groups.hasNext()) {
        wSpec = groups.next(hiveConf, semanticAnalyzer, unparseTranslator, rr);
        // 1. Create RS and backtrack Select operator on top
        ArrayList<ExprNodeDesc> keyCols = new ArrayList<ExprNodeDesc>();
        ArrayList<ExprNodeDesc> partCols = new ArrayList<ExprNodeDesc>();
        StringBuilder order = new StringBuilder();
        StringBuilder nullOrder = new StringBuilder();
        for (PartitionExpression partCol : wSpec.getQueryPartitionSpec().getExpressions()) {
            ExprNodeDesc partExpr = semanticAnalyzer.genExprNodeDesc(partCol.getExpression(), rr);
            if (ExprNodeDescUtils.indexOf(partExpr, partCols) < 0) {
                keyCols.add(partExpr);
                partCols.add(partExpr);
                order.append('+');
                nullOrder.append('a');
            }
        }
        if (wSpec.getQueryOrderSpec() != null) {
            for (OrderExpression orderCol : wSpec.getQueryOrderSpec().getExpressions()) {
                ExprNodeDesc orderExpr = semanticAnalyzer.genExprNodeDesc(orderCol.getExpression(), rr);
                char orderChar = orderCol.getOrder() == PTFInvocationSpec.Order.ASC ? '+' : '-';
                char nullOrderChar = orderCol.getNullOrder() == PTFInvocationSpec.NullOrder.NULLS_FIRST ? 'a' : 'z';
                int index = ExprNodeDescUtils.indexOf(orderExpr, keyCols);
                if (index >= 0) {
                    order.setCharAt(index, orderChar);
                    nullOrder.setCharAt(index, nullOrderChar);
                    continue;
                }
                keyCols.add(orderExpr);
                order.append(orderChar);
                nullOrder.append(nullOrderChar);
            }
        }
        SelectOperator selectOp = genReduceSinkAndBacktrackSelect(input, keyCols.toArray(new ExprNodeDesc[keyCols.size()]), 0, partCols, order.toString(), nullOrder.toString(), -1, Operation.NOT_ACID, hiveConf);
        // 2. Finally create PTF
        PTFTranslator translator = new PTFTranslator();
        PTFDesc ptfDesc = translator.translate(wSpec, semanticAnalyzer, hiveConf, rr, unparseTranslator);
        RowResolver ptfOpRR = ptfDesc.getFuncDef().getOutputShape().getRr();
        Operator<?> ptfOp = OperatorFactory.getAndMakeChild(ptfDesc, new RowSchema(ptfOpRR.getColumnInfos()), selectOp);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Generated " + ptfOp + " with row schema: [" + ptfOp.getSchema() + "]");
        }
        // 3. Prepare for next iteration (if any)
        rr = ptfOpRR;
        input = ptfOp;
    }
    return inputOpAf.clone(input);
}
Also used : RowSchema(org.apache.hadoop.hive.ql.exec.RowSchema) OrderExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression) ArrayList(java.util.ArrayList) PTFTranslator(org.apache.hadoop.hive.ql.parse.PTFTranslator) ColumnInfo(org.apache.hadoop.hive.ql.exec.ColumnInfo) RowResolver(org.apache.hadoop.hive.ql.parse.RowResolver) SelectOperator(org.apache.hadoop.hive.ql.exec.SelectOperator) PartitionExpression(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression) PTFDesc(org.apache.hadoop.hive.ql.plan.PTFDesc) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) WindowingComponentizer(org.apache.hadoop.hive.ql.parse.WindowingComponentizer)

Aggregations

OrderExpression (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression)7 PartitionExpression (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression)4 ArrayList (java.util.ArrayList)3 OrderSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)2 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)2 OrderDef (org.apache.hadoop.hive.ql.plan.ptf.OrderDef)2 OrderExpressionDef (org.apache.hadoop.hive.ql.plan.ptf.OrderExpressionDef)2 RexFieldCollation (org.apache.calcite.rex.RexFieldCollation)1 RexNode (org.apache.calcite.rex.RexNode)1 ColumnInfo (org.apache.hadoop.hive.ql.exec.ColumnInfo)1 RowSchema (org.apache.hadoop.hive.ql.exec.RowSchema)1 SelectOperator (org.apache.hadoop.hive.ql.exec.SelectOperator)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 RexVisitor (org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.RexVisitor)1 Schema (org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.Schema)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1 NullOrder (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.NullOrder)1 Order (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.Order)1 PartitionSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionSpec)1 PartitioningSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitioningSpec)1