Search in sources :

Example 16 with QueryDef

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

the class WindowFunctionTranslation method translateWindowSpec.

public static WindowDef translateWindowSpec(QueryDef qDef, InputInfo iInfo, WindowFunctionSpec wFnSpec) throws WindowingException {
    WindowSpec wSpec = wFnSpec.getWindowSpec();
    if (wSpec == null)
        return null;
    WindowFunctionInfo wFnInfo = FunctionRegistry.getWindowFunctionInfo(wFnSpec.getName());
    String desc = wFnSpec.toString();
    if (wSpec != null && !wFnInfo.isSupportsWindow()) {
        throw new WindowingException(sprintf("Function %s doesn't support windowing", desc));
    }
    return WindowSpecTranslation.translateWindowSpecOnInput(qDef, wSpec, iInfo, desc);
}
Also used : WindowFunctionInfo(com.sap.hadoop.windowing.functions2.FunctionRegistry.WindowFunctionInfo) WindowingException(com.sap.hadoop.windowing.WindowingException) WindowSpec(com.sap.hadoop.windowing.query2.specification.WindowSpec)

Example 17 with QueryDef

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

the class WindowSpecTranslation method translateOrder.

static OrderDef translateOrder(QueryDef qDef, String inputDesc, InputInfo iInfo, OrderSpec spec, PartitionDef pDef) throws WindowingException {
    if (spec == null || spec.getColumns() == null || spec.getColumns().size() == 0) {
        if (pDef == null)
            return null;
        return new OrderDef(pDef);
    }
    if (pDef == null) {
        throw new WindowingException(sprintf("Input %s cannot have an Order spec w/o a Partition spec", inputDesc));
    }
    OrderDef oDef = new OrderDef(spec);
    for (OrderColumnSpec colSpec : spec.getColumns()) {
        OrderColumnDef cDef = translateOrderColumn(qDef, iInfo, colSpec);
        oDef.addColumn(cDef);
    }
    /*
		 * either all partition columns must be in Order list or none must be specified.
		 * If none are specified then add them all.
		 */
    int numOfPartColumns = 0;
    List<OrderColumnDef> orderCols = oDef.getColumns();
    List<ColumnDef> partCols = pDef.getColumns();
    int chkSize = partCols.size();
    chkSize = chkSize > orderCols.size() ? orderCols.size() : chkSize;
    for (int i = 0; i < chkSize; i++) {
        if (orderCols.get(i).getSpec().getColumnName().equals(partCols.get(i).getSpec().getColumnName())) {
            numOfPartColumns++;
        } else
            break;
    }
    if (numOfPartColumns != 0 && numOfPartColumns != partCols.size()) {
        throw new WindowingException(sprintf("For Input %s:n all partition columns must be in order clause or none should be specified", inputDesc));
    }
    ArrayList<OrderColumnDef> combinedOrderCols = new ArrayList<OrderColumnDef>();
    if (numOfPartColumns == 0) {
        for (ColumnDef cDef : partCols) {
            OrderColumnDef ocDef = new OrderColumnDef(cDef);
            combinedOrderCols.add(ocDef);
        }
        combinedOrderCols.addAll(orderCols);
        oDef.setColumns(combinedOrderCols);
    }
    return oDef;
}
Also used : OrderColumnDef(com.sap.hadoop.windowing.query2.definition.OrderColumnDef) OrderColumnSpec(com.sap.hadoop.windowing.query2.specification.OrderColumnSpec) WindowingException(com.sap.hadoop.windowing.WindowingException) ArrayList(java.util.ArrayList) OrderColumnDef(com.sap.hadoop.windowing.query2.definition.OrderColumnDef) ColumnDef(com.sap.hadoop.windowing.query2.definition.ColumnDef) OrderDef(com.sap.hadoop.windowing.query2.definition.OrderDef)

Example 18 with QueryDef

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

the class WindowSpecTranslation method translateBoundary.

static BoundaryDef translateBoundary(QueryDef qDef, BoundarySpec bndSpec, InputInfo iInfo) throws WindowingException {
    if (bndSpec instanceof ValueBoundarySpec) {
        ValueBoundarySpec vBndSpec = (ValueBoundarySpec) bndSpec;
        ValueBoundaryDef vbDef = new ValueBoundaryDef(vBndSpec);
        TranslateUtils.validateNoLeadLagInValueBoundarySpec(vBndSpec.getExpression());
        ExprNodeDesc exprNode = TranslateUtils.buildExprNode(vBndSpec.getExpression(), iInfo.getTypeCheckCtx());
        vbDef.setExprNode(exprNode);
        ExprNodeEvaluator exprEval = WindowingExprNodeEvaluatorFactory.get(qDef.getTranslationInfo(), exprNode);
        ObjectInspector OI = TranslateUtils.initExprNodeEvaluator(qDef, exprNode, exprEval, iInfo);
        TranslateUtils.validateValueBoundaryExprType(OI);
        vbDef.setExprEvaluator(exprEval);
        vbDef.setOI(OI);
        return vbDef;
    } else if (bndSpec instanceof RangeBoundarySpec) {
        RangeBoundarySpec rBndSpec = (RangeBoundarySpec) bndSpec;
        RangeBoundaryDef rbDef = new RangeBoundaryDef(rBndSpec);
        return rbDef;
    } else if (bndSpec instanceof CurrentRowSpec) {
        CurrentRowSpec cBndSpec = (CurrentRowSpec) bndSpec;
        CurrentRowDef cbDef = new CurrentRowDef(cBndSpec);
        return cbDef;
    }
    throw new WindowingException("Unknown Boundary: " + bndSpec);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ValueBoundarySpec(com.sap.hadoop.windowing.query2.specification.WindowFrameSpec.ValueBoundarySpec) ExprNodeEvaluator(org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator) WindowingException(com.sap.hadoop.windowing.WindowingException) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) CurrentRowSpec(com.sap.hadoop.windowing.query2.specification.WindowFrameSpec.CurrentRowSpec) ValueBoundaryDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.ValueBoundaryDef) RangeBoundaryDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.RangeBoundaryDef) RangeBoundarySpec(com.sap.hadoop.windowing.query2.specification.WindowFrameSpec.RangeBoundarySpec) CurrentRowDef(com.sap.hadoop.windowing.query2.definition.WindowFrameDef.CurrentRowDef)

Example 19 with QueryDef

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

the class InputTranslation method translate.

private static HiveQueryDef translate(QueryDef qDef, HiveQuerySpec spec) throws WindowingException {
    HiveQueryDef def = new HiveQueryDef();
    HiveQueryExecutor hiveQryExec = qDef.getTranslationInfo().getHiveQueryExecutor();
    Hive hive = qDef.getTranslationInfo().getHive();
    String tableName = hiveQryExec.createTableAsQuery(spec.getHiveQuery());
    HiveTableSpec tSpec = new HiveTableSpec();
    tSpec.setDbName(hive.getCurrentDatabase());
    tSpec.setTableName(tableName);
    tSpec.setPartition(spec.getPartition());
    tSpec.setOrder(spec.getOrder());
    def = (HiveQueryDef) InputTranslation.translate(qDef, tSpec, (HiveTableDef) def);
    return def;
}
Also used : HiveQueryExecutor(com.sap.hadoop.windowing.runtime2.HiveQueryExecutor) Hive(org.apache.hadoop.hive.ql.metadata.Hive) HiveQueryDef(com.sap.hadoop.windowing.query2.definition.HiveQueryDef) HiveTableSpec(com.sap.hadoop.windowing.query2.specification.HiveTableSpec)

Example 20 with QueryDef

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

the class InputTranslation method getTableAlias.

private static String getTableAlias(QueryDef qDef, int inputNum, QueryInputDef inputDef) throws WindowingException {
    if (inputDef instanceof HiveTableDef) {
        HiveTableDef hTbldef = (HiveTableDef) inputDef;
        String db = ((HiveTableSpec) hTbldef.getSpec()).getDbName();
        String tableName = ((HiveTableSpec) hTbldef.getSpec()).getTableName();
        return db + "." + tableName;
    } else if (inputDef instanceof TableFuncDef) {
        return "ptf_" + inputNum;
    }
    throw new WindowingException(sprintf("Internal Error: attempt to translate %s", inputDef.getSpec()));
}
Also used : WindowingException(com.sap.hadoop.windowing.WindowingException) HiveTableSpec(com.sap.hadoop.windowing.query2.specification.HiveTableSpec) HiveTableDef(com.sap.hadoop.windowing.query2.definition.HiveTableDef) TableFuncDef(com.sap.hadoop.windowing.query2.definition.TableFuncDef)

Aggregations

WindowingException (com.sap.hadoop.windowing.WindowingException)15 QueryDef (com.sap.hadoop.windowing.query2.definition.QueryDef)7 TableFuncDef (com.sap.hadoop.windowing.query2.definition.TableFuncDef)7 InputInfo (com.sap.hadoop.windowing.query2.translate.QueryTranslationInfo.InputInfo)7 ColumnDef (com.sap.hadoop.windowing.query2.definition.ColumnDef)6 QueryInputDef (com.sap.hadoop.windowing.query2.definition.QueryInputDef)6 ExprNodeEvaluator (org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator)6 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)6 OrderColumnDef (com.sap.hadoop.windowing.query2.definition.OrderColumnDef)5 QuerySpec (com.sap.hadoop.windowing.query2.specification.QuerySpec)5 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)5 TableFunctionEvaluator (com.sap.hadoop.windowing.functions2.TableFunctionEvaluator)4 ArgDef (com.sap.hadoop.windowing.query2.definition.ArgDef)4 OrderDef (com.sap.hadoop.windowing.query2.definition.OrderDef)4 LocalExecutorTest (com.sap.hadoop.windowing.testutils.LocalExecutorTest)4 Test (org.junit.Test)4 HiveQueryDef (com.sap.hadoop.windowing.query2.definition.HiveQueryDef)3 HiveTableDef (com.sap.hadoop.windowing.query2.definition.HiveTableDef)3 HiveTableSpec (com.sap.hadoop.windowing.query2.specification.HiveTableSpec)3 ArrayList (java.util.ArrayList)3