Search in sources :

Example 6 with OrderSpec

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

the class SemanticAnalyzer method processWindowSpec.

private WindowSpec processWindowSpec(ASTNode node) throws SemanticException {
    String sourceId = null;
    PartitionSpec partition = null;
    OrderSpec order = null;
    WindowFrameSpec windowFrame = null;
    boolean hasSrcId = false, hasPartSpec = false, hasWF = false;
    int srcIdIdx = -1, partIdx = -1, wfIdx = -1;
    for (int i = 0; i < node.getChildCount(); i++) {
        int type = node.getChild(i).getType();
        switch(type) {
            case HiveParser.Identifier:
                hasSrcId = true;
                srcIdIdx = i;
                break;
            case HiveParser.TOK_PARTITIONINGSPEC:
                hasPartSpec = true;
                partIdx = i;
                break;
            case HiveParser.TOK_WINDOWRANGE:
            case HiveParser.TOK_WINDOWVALUES:
                hasWF = true;
                wfIdx = i;
                break;
        }
    }
    WindowSpec ws = new WindowSpec();
    if (hasSrcId) {
        ASTNode nameNode = (ASTNode) node.getChild(srcIdIdx);
        ws.setSourceId(nameNode.getText());
    }
    if (hasPartSpec) {
        ASTNode partNode = (ASTNode) node.getChild(partIdx);
        PartitioningSpec partitioning = processPTFPartitionSpec(partNode);
        ws.setPartitioning(partitioning);
    }
    if (hasWF) {
        ASTNode wfNode = (ASTNode) node.getChild(wfIdx);
        WindowFrameSpec wfSpec = processWindowFrame(wfNode);
        ws.setWindowFrame(wfSpec);
    }
    return ws;
}
Also used : OrderSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec) PartitionSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionSpec) WindowSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec) WindowFrameSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFrameSpec) PartitioningSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitioningSpec)

Example 7 with OrderSpec

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

the class WindowingSpec method setAndValidateOrderSpec.

/**
   * Add default order spec if there is no order and validate order spec for valued based
   * windowing since only one sort key is allowed.
   * @param wFn Window function spec
   * @throws SemanticException
   */
private void setAndValidateOrderSpec(WindowFunctionSpec wFn) throws SemanticException {
    WindowSpec wdwSpec = wFn.getWindowSpec();
    wdwSpec.ensureOrderSpec(wFn);
    WindowFrameSpec wFrame = wdwSpec.getWindowFrame();
    OrderSpec order = wdwSpec.getOrder();
    BoundarySpec start = wFrame.getStart();
    BoundarySpec end = wFrame.getEnd();
    if (wFrame.getWindowType() == WindowType.RANGE) {
        if (order == null || order.getExpressions().size() == 0) {
            throw new SemanticException("Range based Window Frame needs to specify ORDER BY clause");
        }
        boolean currentRange = start.getDirection() == Direction.CURRENT && end.getDirection() == Direction.CURRENT;
        boolean defaultPreceding = start.getDirection() == Direction.PRECEDING && start.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT && end.getDirection() == Direction.CURRENT;
        boolean defaultFollowing = start.getDirection() == Direction.CURRENT && end.getDirection() == Direction.FOLLOWING && end.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT;
        boolean defaultPrecedingFollowing = start.getDirection() == Direction.PRECEDING && start.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT && end.getDirection() == Direction.FOLLOWING && end.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT;
        boolean multiOrderAllowed = currentRange || defaultPreceding || defaultFollowing || defaultPrecedingFollowing;
        if (order.getExpressions().size() != 1 && !multiOrderAllowed) {
            throw new SemanticException("Range value based Window Frame can have only 1 Sort Key");
        }
    }
}
Also used : OrderSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)

Aggregations

OrderSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)7 PartitionSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionSpec)4 PartitioningSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitioningSpec)3 OrderExpression (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderExpression)2 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 RexFieldCollation (org.apache.calcite.rex.RexFieldCollation)1 RexNode (org.apache.calcite.rex.RexNode)1 WindowFunctionInfo (org.apache.hadoop.hive.ql.exec.WindowFunctionInfo)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 PTFInputSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PTFInputSpec)1 PTFQueryInputSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PTFQueryInputSpec)1 PartitionExpression (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression)1 PartitionedTableFunctionSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionedTableFunctionSpec)1 WindowFrameSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFrameSpec)1 WindowSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec)1