Search in sources :

Example 1 with WindowFunctionInfo

use of org.apache.hadoop.hive.ql.exec.WindowFunctionInfo 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 2 with WindowFunctionInfo

use of org.apache.hadoop.hive.ql.exec.WindowFunctionInfo in project hive by apache.

the class WindowingTableFunction method initializeWindowingFunctionInfoHelpers.

private void initializeWindowingFunctionInfoHelpers() throws SemanticException {
    // to the object during the map/reduce tasks.
    if (windowingFunctionHelpers != null) {
        return;
    }
    windowingFunctionHelpers = new HashMap<String, WindowingFunctionInfoHelper>();
    WindowTableFunctionDef tabDef = (WindowTableFunctionDef) getTableDef();
    for (int i = 0; i < tabDef.getWindowFunctions().size(); i++) {
        WindowFunctionDef wFn = tabDef.getWindowFunctions().get(i);
        WindowFunctionInfo wFnInfo = FunctionRegistry.getWindowFunctionInfo(wFn.getName());
        boolean supportsWindow = wFnInfo.isSupportsWindow();
        windowingFunctionHelpers.put(wFn.getName(), new WindowingFunctionInfoHelper(supportsWindow));
    }
}
Also used : WindowFunctionInfo(org.apache.hadoop.hive.ql.exec.WindowFunctionInfo) WindowTableFunctionDef(org.apache.hadoop.hive.ql.plan.ptf.WindowTableFunctionDef) WindowFunctionDef(org.apache.hadoop.hive.ql.plan.ptf.WindowFunctionDef)

Example 3 with WindowFunctionInfo

use of org.apache.hadoop.hive.ql.exec.WindowFunctionInfo in project hive by apache.

the class SemanticAnalyzer method processWindowFunction.

private WindowFunctionSpec processWindowFunction(ASTNode node, ASTNode wsNode) throws SemanticException {
    WindowFunctionSpec wfSpec = new WindowFunctionSpec();
    switch(node.getType()) {
        case HiveParser.TOK_FUNCTIONSTAR:
            wfSpec.setStar(true);
            break;
        case HiveParser.TOK_FUNCTIONDI:
            wfSpec.setDistinct(true);
            break;
    }
    wfSpec.setExpression(node);
    ASTNode nameNode = (ASTNode) node.getChild(0);
    wfSpec.setName(nameNode.getText());
    for (int i = 1; i < node.getChildCount() - 1; i++) {
        ASTNode child = (ASTNode) node.getChild(i);
        wfSpec.addArg(child);
    }
    if (wsNode != null) {
        WindowFunctionInfo functionInfo = FunctionRegistry.getWindowFunctionInfo(wfSpec.name);
        if (functionInfo == null) {
            throw new SemanticException(ErrorMsg.INVALID_FUNCTION.getMsg(wfSpec.name));
        }
        wfSpec.setRespectNulls(processRespectIgnoreNulls(functionInfo, wsNode));
        wfSpec.setWindowSpec(processWindowSpec(wsNode));
    }
    return wfSpec;
}
Also used : WindowFunctionInfo(org.apache.hadoop.hive.ql.exec.WindowFunctionInfo) WindowFunctionSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFunctionSpec) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) DefaultConstraint(org.apache.hadoop.hive.ql.metadata.DefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)

Example 4 with WindowFunctionInfo

use of org.apache.hadoop.hive.ql.exec.WindowFunctionInfo in project hive by apache.

the class WindowingSpec method effectiveWindowFrame.

/*
   * - A Window Frame that has only the start boundary, then it is interpreted as:
   *     BETWEEN <start boundary> AND CURRENT ROW
   * - A Window Specification with an Order Specification and no Window Frame is
   *   interpreted as: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
   * - A Window Specification with no Order and no Window Frame is interpreted as:
   *     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
   */
private void effectiveWindowFrame(WindowFunctionSpec wFn) throws SemanticException {
    WindowSpec wdwSpec = wFn.getWindowSpec();
    WindowFunctionInfo wFnInfo = FunctionRegistry.getWindowFunctionInfo(wFn.getName());
    boolean supportsWindowing = wFnInfo == null ? true : 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.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)

Example 5 with WindowFunctionInfo

use of org.apache.hadoop.hive.ql.exec.WindowFunctionInfo in project hive by apache.

the class PTFTranslator method translate.

private WindowFunctionDef translate(WindowTableFunctionDef wdwTFnDef, WindowFunctionSpec spec) throws SemanticException {
    WindowFunctionInfo wFnInfo = FunctionRegistry.getWindowFunctionInfo(spec.getName());
    if (wFnInfo == null) {
        throw new SemanticException(ErrorMsg.INVALID_FUNCTION.getMsg(spec.getName()));
    }
    WindowFunctionDef def = new WindowFunctionDef();
    def.setName(spec.getName());
    def.setAlias(spec.getAlias());
    def.setDistinct(spec.isDistinct());
    def.setExpressionTreeString(spec.getExpression().toStringTree());
    def.setStar(spec.isStar());
    def.setPivotResult(wFnInfo.isPivotResult());
    def.setRespectNulls(spec.isRespectNulls());
    ShapeDetails inpShape = wdwTFnDef.getRawInputShape();
    /*
     * translate args
     */
    ArrayList<ASTNode> args = spec.getArgs();
    if (args != null) {
        for (ASTNode expr : args) {
            PTFExpressionDef argDef = null;
            try {
                argDef = buildExpressionDef(inpShape, expr);
            } catch (HiveException he) {
                throw new SemanticException(he);
            }
            def.addArg(argDef);
        }
    }
    if (FunctionRegistry.isRankingFunction(spec.getName())) {
        setupRankingArgs(wdwTFnDef, def, spec);
    }
    WindowSpec wdwSpec = spec.getWindowSpec();
    if (wdwSpec != null) {
        String desc = spec.toString();
        WindowFrameDef wdwFrame = translate(spec.getName(), inpShape, wdwSpec);
        if (!wFnInfo.isSupportsWindow()) {
            BoundarySpec start = wdwSpec.getWindowFrame().getStart();
            if (start.getAmt() != BoundarySpec.UNBOUNDED_AMOUNT) {
                throw new SemanticException(String.format("Expecting left window frame boundary for " + "function %s to be unbounded. Found : %d", desc, start.getAmt()));
            }
            BoundarySpec end = wdwSpec.getWindowFrame().getEnd();
            if (end.getAmt() != BoundarySpec.UNBOUNDED_AMOUNT) {
                throw new SemanticException(String.format("Expecting right window frame boundary for " + "function %s to be unbounded. Found : %d", desc, start.getAmt()));
            }
        }
        def.setWindowFrame(wdwFrame);
    }
    try {
        setupWdwFnEvaluator(def);
    } catch (HiveException he) {
        throw new SemanticException(he);
    }
    return def;
}
Also used : WindowFunctionInfo(org.apache.hadoop.hive.ql.exec.WindowFunctionInfo) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) WindowFrameDef(org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef) PTFExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef) WindowFunctionDef(org.apache.hadoop.hive.ql.plan.ptf.WindowFunctionDef) ShapeDetails(org.apache.hadoop.hive.ql.plan.ptf.ShapeDetails) BoundarySpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.BoundarySpec) WindowSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec)

Aggregations

WindowFunctionInfo (org.apache.hadoop.hive.ql.exec.WindowFunctionInfo)5 WindowFunctionDef (org.apache.hadoop.hive.ql.plan.ptf.WindowFunctionDef)2 OrderSpec (org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderSpec)1 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)1 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)1 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)1 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)1 DefaultConstraint (org.apache.hadoop.hive.ql.metadata.DefaultConstraint)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)1 OrderSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)1 BoundarySpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.BoundarySpec)1 WindowFunctionSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFunctionSpec)1 WindowSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec)1 PTFExpressionDef (org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef)1 ShapeDetails (org.apache.hadoop.hive.ql.plan.ptf.ShapeDetails)1 WindowFrameDef (org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef)1 WindowTableFunctionDef (org.apache.hadoop.hive.ql.plan.ptf.WindowTableFunctionDef)1