Search in sources :

Example 1 with OrderSpec

use of org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec in project flink by apache.

the class HiveParserBaseSemanticAnalyzer method processOrderSpec.

static OrderSpec processOrderSpec(HiveParserASTNode sortNode) {
    OrderSpec oSpec = new OrderSpec();
    int exprCnt = sortNode.getChildCount();
    for (int i = 0; i < exprCnt; i++) {
        OrderExpression exprSpec = new OrderExpression();
        HiveParserASTNode orderSpec = (HiveParserASTNode) sortNode.getChild(i);
        HiveParserASTNode nullOrderSpec = (HiveParserASTNode) orderSpec.getChild(0);
        exprSpec.setExpression((HiveParserASTNode) nullOrderSpec.getChild(0));
        if (orderSpec.getType() == HiveASTParser.TOK_TABSORTCOLNAMEASC) {
            exprSpec.setOrder(Order.ASC);
        } else {
            exprSpec.setOrder(Order.DESC);
        }
        if (nullOrderSpec.getType() == HiveASTParser.TOK_NULLS_FIRST) {
            exprSpec.setNullOrder(NullOrder.NULLS_FIRST);
        } else {
            exprSpec.setNullOrder(NullOrder.NULLS_LAST);
        }
        oSpec.addExpression(exprSpec);
    }
    return oSpec;
}
Also used : OrderSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec) OrderExpression(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderExpression)

Example 2 with OrderSpec

use of org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec in project flink by apache.

the class HiveParserWindowingSpec method effectiveWindowFrame.

private void effectiveWindowFrame(WindowFunctionSpec wFn) throws SemanticException {
    WindowSpec wdwSpec = wFn.getWindowSpec();
    WindowFunctionInfo wFnInfo = FunctionRegistry.getWindowFunctionInfo(wFn.getName());
    boolean supportsWindowing = wFnInfo == null || wFnInfo.isSupportsWindow();
    WindowFrameSpec wFrame = wdwSpec.getWindowFrame();
    OrderSpec orderSpec = wdwSpec.getOrder();
    if (wFrame == null) {
        if (!supportsWindowing) {
            if (wFn.getName().toLowerCase().equals(FunctionRegistry.LAST_VALUE_FUNC_NAME) && orderSpec != null) {
                /*
                     * last_value: when an Sort Key is specified, then last_value should return the
                     * last value among rows with the same Sort Key value.
                     */
                wFrame = new WindowFrameSpec(WindowType.ROWS, new BoundarySpec(Direction.CURRENT), new BoundarySpec(Direction.FOLLOWING, 0));
            } else {
                wFrame = new WindowFrameSpec(WindowType.ROWS, new BoundarySpec(Direction.PRECEDING, BoundarySpec.UNBOUNDED_AMOUNT), new BoundarySpec(Direction.FOLLOWING, BoundarySpec.UNBOUNDED_AMOUNT));
            }
        } else {
            if (orderSpec == null) {
                wFrame = new WindowFrameSpec(WindowType.ROWS, new BoundarySpec(Direction.PRECEDING, BoundarySpec.UNBOUNDED_AMOUNT), new BoundarySpec(Direction.FOLLOWING, BoundarySpec.UNBOUNDED_AMOUNT));
            } else {
                wFrame = new WindowFrameSpec(WindowType.RANGE, new BoundarySpec(Direction.PRECEDING, BoundarySpec.UNBOUNDED_AMOUNT), new BoundarySpec(Direction.CURRENT));
            }
        }
        wdwSpec.setWindowFrame(wFrame);
    } else if (wFrame.getEnd() == null) {
        wFrame.setEnd(new BoundarySpec(Direction.CURRENT));
    }
}
Also used : WindowFunctionInfo(org.apache.hadoop.hive.ql.exec.WindowFunctionInfo) OrderSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec)

Example 3 with OrderSpec

use of org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec in project flink by apache.

the class HiveParserBaseSemanticAnalyzer method processPTFPartitionSpec.

static PartitioningSpec processPTFPartitionSpec(HiveParserASTNode pSpecNode) {
    PartitioningSpec partitioning = new PartitioningSpec();
    HiveParserASTNode firstChild = (HiveParserASTNode) pSpecNode.getChild(0);
    int type = firstChild.getType();
    if (type == HiveASTParser.TOK_DISTRIBUTEBY || type == HiveASTParser.TOK_CLUSTERBY) {
        PartitionSpec pSpec = processPartitionSpec(firstChild);
        partitioning.setPartSpec(pSpec);
        HiveParserASTNode sortNode = pSpecNode.getChildCount() > 1 ? (HiveParserASTNode) pSpecNode.getChild(1) : null;
        if (sortNode != null) {
            OrderSpec oSpec = processOrderSpec(sortNode);
            partitioning.setOrderSpec(oSpec);
        }
    } else if (type == HiveASTParser.TOK_SORTBY || type == HiveASTParser.TOK_ORDERBY) {
        OrderSpec oSpec = processOrderSpec(firstChild);
        partitioning.setOrderSpec(oSpec);
    }
    return partitioning;
}
Also used : OrderSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec) PartitionSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.PartitionSpec) PartitioningSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.PartitioningSpec)

Example 4 with OrderSpec

use of org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec in project flink by apache.

the class HiveParserWindowingSpec method setAndValidateOrderSpec.

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.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Aggregations

OrderSpec (org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec)4 OrderExpression (org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderExpression)1 PartitionSpec (org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.PartitionSpec)1 PartitioningSpec (org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.PartitioningSpec)1 WindowFunctionInfo (org.apache.hadoop.hive.ql.exec.WindowFunctionInfo)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1