Search in sources :

Example 1 with WindowFunctionDef

use of com.sap.hadoop.windowing.query2.definition.WindowFunctionDef in project SQLWindowing by hbutani.

the class WindowingTableFunction method getRange.

static Range getRange(WindowFunctionDef wFnDef, int currRow, Partition p) throws WindowingException {
    BoundaryDef startB = wFnDef.getWindow().getWindow().getStart();
    BoundaryDef endB = wFnDef.getWindow().getWindow().getEnd();
    int start = getIndex(startB, currRow, p, false);
    int end = getIndex(endB, currRow, p, true);
    return new Range(start, end, p);
}
Also used : RangeBoundaryDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.RangeBoundaryDef) ValueBoundaryDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.ValueBoundaryDef) BoundaryDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.BoundaryDef)

Example 2 with WindowFunctionDef

use of com.sap.hadoop.windowing.query2.definition.WindowFunctionDef in project SQLWindowing by hbutani.

the class WindowingTableFunction method executeFnwithWindow.

static ArrayList<Object> executeFnwithWindow(QueryDef qDef, WindowFunctionDef wFnDef, Partition iPart) throws HiveException, WindowingException {
    ArrayList<Object> vals = new ArrayList<Object>();
    GenericUDAFEvaluator fEval = wFnDef.getEvaluator();
    Object[] args = new Object[wFnDef.getArgs().size()];
    for (int i = 0; i < iPart.size(); i++) {
        AggregationBuffer aggBuffer = fEval.getNewAggregationBuffer();
        Range rng = getRange(wFnDef, i, iPart);
        PartitionIterator<Object> rItr = rng.iterator();
        RuntimeUtils.connectLeadLagFunctionsToPartition(qDef, rItr);
        while (rItr.hasNext()) {
            Object row = rItr.next();
            int j = 0;
            for (ArgDef arg : wFnDef.getArgs()) {
                args[j++] = arg.getExprEvaluator().evaluate(row);
            }
            fEval.aggregate(aggBuffer, args);
        }
        Object out = fEval.evaluate(aggBuffer);
        out = ObjectInspectorUtils.copyToStandardObject(out, wFnDef.getOI(), ObjectInspectorCopyOption.WRITABLE);
        vals.add(out);
    }
    return vals;
}
Also used : GenericUDAFEvaluator(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator) ArrayList(java.util.ArrayList) AggregationBuffer(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator.AggregationBuffer) ArgDef(com.sap.hadoop.windowing.query2.definition.ArgDef)

Example 3 with WindowFunctionDef

use of com.sap.hadoop.windowing.query2.definition.WindowFunctionDef in project SQLWindowing by hbutani.

the class WindowFunctionTranslation method setupRankingArgs.

static void setupRankingArgs(QueryDef qDef, TableFuncDef windowTableFnDef, WindowFunctionDef wFnDef, WindowFunctionSpec wSpec) throws WindowingException {
    if (wSpec.getArgs().size() > 0) {
        throw new WindowingException("Ranking Functions can take no arguments");
    }
    QueryInputDef inpDef = windowTableFnDef.getInput();
    InputInfo inpInfo = qDef.getTranslationInfo().getInputInfo(inpDef);
    OrderDef oDef = getTableFuncOrderDef(windowTableFnDef);
    ArrayList<OrderColumnDef> oCols = oDef.getColumns();
    for (OrderColumnDef oCol : oCols) {
        wFnDef.addArg(TranslateUtils.buildArgDef(qDef, inpInfo, oCol.getExpression()));
    }
}
Also used : InputInfo(com.sap.hadoop.windowing.query2.translate.QueryTranslationInfo.InputInfo) OrderColumnDef(com.sap.hadoop.windowing.query2.definition.OrderColumnDef) QueryInputDef(com.sap.hadoop.windowing.query2.definition.QueryInputDef) WindowingException(com.sap.hadoop.windowing.WindowingException) OrderDef(com.sap.hadoop.windowing.query2.definition.OrderDef)

Example 4 with WindowFunctionDef

use of com.sap.hadoop.windowing.query2.definition.WindowFunctionDef in project SQLWindowing by hbutani.

the class WindowFunctionTranslation method validateWindowDefForWFn.

public static void validateWindowDefForWFn(TableFuncDef tFnDef, WindowFunctionDef wFnDef) throws WindowingException {
    WindowDef tWindow = tFnDef.getWindow();
    WindowDef fWindow = wFnDef.getWindow();
    PartitionDef tPart = tWindow == null ? null : tWindow.getPartDef();
    PartitionDef fPart = fWindow == null ? null : fWindow.getPartDef();
    if (!TranslateUtils.isCompatible(tPart, fPart)) {
        throw new WindowingException(sprintf("Window Function '%s' has an incompatible partition clause", wFnDef.getSpec()));
    }
    OrderDef tOrder = tWindow == null ? null : tWindow.getOrderDef();
    OrderDef fOrder = fWindow == null ? null : fWindow.getOrderDef();
    if (!TranslateUtils.isCompatible(tOrder, fOrder)) {
        throw new WindowingException(sprintf("Window Function '%s' has an incompatible order clause", wFnDef.getSpec()));
    }
}
Also used : WindowDef(com.sap.hadoop.windowing.query2.definition.WindowDef) PartitionDef(com.sap.hadoop.windowing.query2.definition.PartitionDef) WindowingException(com.sap.hadoop.windowing.WindowingException) OrderDef(com.sap.hadoop.windowing.query2.definition.OrderDef)

Example 5 with WindowFunctionDef

use of com.sap.hadoop.windowing.query2.definition.WindowFunctionDef in project SQLWindowing by hbutani.

the class WindowFunctionTranslation method setupEvaluator.

static void setupEvaluator(WindowFunctionDef wFnDef) throws WindowingException {
    try {
        WindowFunctionSpec wSpec = wFnDef.getSpec();
        ArrayList<ArgDef> args = wFnDef.getArgs();
        ArrayList<ObjectInspector> argOIs = getWritableObjectInspector(args);
        GenericUDAFEvaluator wFnEval = org.apache.hadoop.hive.ql.exec.FunctionRegistry.getGenericUDAFEvaluator(wSpec.getName(), argOIs, wSpec.isDistinct(), wSpec.isStar());
        ObjectInspector[] funcArgOIs = null;
        if (args != null) {
            funcArgOIs = new ObjectInspector[args.size()];
            int i = 0;
            for (ArgDef arg : args) {
                funcArgOIs[i++] = arg.getOI();
            }
        }
        ObjectInspector OI = wFnEval.init(GenericUDAFEvaluator.Mode.COMPLETE, funcArgOIs);
        wFnDef.setEvaluator(wFnEval);
        wFnDef.setOI(OI);
    } catch (HiveException he) {
        throw new WindowingException(he);
    }
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) GenericUDAFEvaluator(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator) WindowingException(com.sap.hadoop.windowing.WindowingException) WindowFunctionSpec(com.sap.hadoop.windowing.query2.specification.WindowFunctionSpec) ArgDef(com.sap.hadoop.windowing.query2.definition.ArgDef)

Aggregations

WindowingException (com.sap.hadoop.windowing.WindowingException)4 ArgDef (com.sap.hadoop.windowing.query2.definition.ArgDef)4 GenericUDAFEvaluator (org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator)3 OrderDef (com.sap.hadoop.windowing.query2.definition.OrderDef)2 WindowDef (com.sap.hadoop.windowing.query2.definition.WindowDef)2 WindowFunctionDef (com.sap.hadoop.windowing.query2.definition.WindowFunctionDef)2 InputInfo (com.sap.hadoop.windowing.query2.translate.QueryTranslationInfo.InputInfo)2 ArrayList (java.util.ArrayList)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2 AggregationBuffer (org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator.AggregationBuffer)2 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)2 SameList (com.sap.hadoop.ds.SameList)1 WindowFunctionInfo (com.sap.hadoop.windowing.functions2.FunctionRegistry.WindowFunctionInfo)1 OrderColumnDef (com.sap.hadoop.windowing.query2.definition.OrderColumnDef)1 PartitionDef (com.sap.hadoop.windowing.query2.definition.PartitionDef)1 QueryInputDef (com.sap.hadoop.windowing.query2.definition.QueryInputDef)1 BoundaryDef (com.sap.hadoop.windowing.query2.definition.WindowFrameDef.BoundaryDef)1 RangeBoundaryDef (com.sap.hadoop.windowing.query2.definition.WindowFrameDef.RangeBoundaryDef)1 ValueBoundaryDef (com.sap.hadoop.windowing.query2.definition.WindowFrameDef.ValueBoundaryDef)1 WindowFunctionSpec (com.sap.hadoop.windowing.query2.specification.WindowFunctionSpec)1