Search in sources :

Example 1 with WindowSpec

use of org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec in project hive by apache.

the class SemanticAnalyzer method processQueryWindowClause.

private void processQueryWindowClause(WindowingSpec spec, ASTNode node) throws SemanticException {
    ASTNode nameNode = (ASTNode) node.getChild(0);
    ASTNode wsNode = (ASTNode) node.getChild(1);
    if (spec.getWindowSpecs() != null && spec.getWindowSpecs().containsKey(nameNode.getText())) {
        throw new SemanticException(generateErrorMessage(nameNode, "Duplicate definition of window " + nameNode.getText() + " is not allowed"));
    }
    WindowSpec ws = processWindowSpec(wsNode);
    spec.addWindowSpec(nameNode.getText(), ws);
}
Also used : WindowSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)

Example 2 with WindowSpec

use of org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec 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) {
        WindowSpec ws = processWindowSpec(wsNode);
        wfSpec.setWindowSpec(ws);
    }
    return wfSpec;
}
Also used : WindowFunctionSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFunctionSpec) WindowSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec)

Example 3 with WindowSpec

use of org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec in project hive by apache.

the class ExprNodeConverter method visitOver.

@Override
public ExprNodeDesc visitOver(RexOver over) {
    if (!deep) {
        return null;
    }
    final RexWindow window = over.getWindow();
    final WindowSpec windowSpec = new WindowSpec();
    final PartitioningSpec partitioningSpec = getPSpec(window);
    windowSpec.setPartitioning(partitioningSpec);
    final WindowFrameSpec windowFrameSpec = getWindowRange(window);
    windowSpec.setWindowFrame(windowFrameSpec);
    WindowFunctionSpec wfs = new WindowFunctionSpec();
    wfs.setWindowSpec(windowSpec);
    final Schema schema = new Schema(tabAlias, inputRowType.getFieldList());
    final ASTNode wUDAFAst = new ASTConverter.RexVisitor(schema).visitOver(over);
    wfs.setExpression(wUDAFAst);
    ASTNode nameNode = (ASTNode) wUDAFAst.getChild(0);
    wfs.setName(nameNode.getText());
    for (int i = 1; i < wUDAFAst.getChildCount() - 1; i++) {
        ASTNode child = (ASTNode) wUDAFAst.getChild(i);
        wfs.addArg(child);
    }
    if (wUDAFAst.getText().equals("TOK_FUNCTIONSTAR")) {
        wfs.setStar(true);
    }
    String columnAlias = getWindowColumnAlias();
    wfs.setAlias(columnAlias);
    this.windowFunctionSpecs.add(wfs);
    return new ExprNodeColumnDesc(TypeConverter.convert(over.getType()), columnAlias, tabAlias, false);
}
Also used : RexVisitor(org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.RexVisitor) Schema(org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.Schema) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) WindowFunctionSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFunctionSpec) RexWindow(org.apache.calcite.rex.RexWindow) WindowSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec) PartitioningSpec(org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitioningSpec) WindowFrameSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFrameSpec)

Example 4 with WindowSpec

use of org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec 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());
    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)

Example 5 with WindowSpec

use of org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec 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)

Aggregations

WindowSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowSpec)5 PartitioningSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitioningSpec)2 WindowFrameSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFrameSpec)2 WindowFunctionSpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFunctionSpec)2 RexWindow (org.apache.calcite.rex.RexWindow)1 WindowFunctionInfo (org.apache.hadoop.hive.ql.exec.WindowFunctionInfo)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)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 OrderSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.OrderSpec)1 PartitionSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionSpec)1 BoundarySpec (org.apache.hadoop.hive.ql.parse.WindowingSpec.BoundarySpec)1 ExprNodeColumnDesc (org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc)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 WindowFunctionDef (org.apache.hadoop.hive.ql.plan.ptf.WindowFunctionDef)1