ptfDesc.setFuncDef(wdwTFnDef);
PTFQueryInputSpec inpSpec = new PTFQueryInputSpec();
inpSpec.setType(PTFQueryInputType.WINDOWING);
wdwTFnDef.setInput(translate(inpSpec, 0));
ShapeDetails inpShape = wdwTFnDef.getInput().getOutputShape();
WindowingTableFunctionResolver tFn = (WindowingTableFunctionResolver)
FunctionRegistry.getTableFunctionResolver(FunctionRegistry.WINDOWING_TABLE_FUNCTION);
if (tFn == null) {
throw new SemanticException(String.format("INternal Error: Unknown Table Function %s",
FunctionRegistry.WINDOWING_TABLE_FUNCTION));
}
wdwTFnDef.setName(FunctionRegistry.WINDOWING_TABLE_FUNCTION);
wdwTFnDef.setResolverClassName(tFn.getClass().getName());
wdwTFnDef.setAlias("ptf_" + 1 );
wdwTFnDef.setExpressionTreeString(null);
wdwTFnDef.setTransformsRawInput(false);
tFn.initialize(hCfg, ptfDesc, wdwTFnDef);
TableFunctionEvaluator tEval = tFn.getEvaluator();
wdwTFnDef.setTFunction(tEval);
wdwTFnDef.setCarryForwardNames(tFn.carryForwardNames());
wdwTFnDef.setRawInputShape(inpShape);
PartitioningSpec partiSpec = wdwSpec.getQueryPartitioningSpec();
if ( partiSpec == null ) {
throw new SemanticException(
"Invalid use of Windowing: there is no Partitioning associated with Windowing");
}
PartitionDef partDef = translate(inpShape, wdwSpec.getQueryPartitionSpec());
OrderDef ordDef = translate(inpShape, wdwSpec.getQueryOrderSpec(), partDef);
wdwTFnDef.setPartition(partDef);
wdwTFnDef.setOrder(ordDef);
/*
* process Wdw functions
*/
ArrayList<WindowFunctionDef> windowFunctions = new ArrayList<WindowFunctionDef>();
if ( wdwSpec.getWindowExpressions() != null ) {
for(WindowExpressionSpec expr : wdwSpec.getWindowExpressions()) {
if ( expr instanceof WindowFunctionSpec) {
WindowFunctionDef wFnDef = translate(wdwTFnDef, (WindowFunctionSpec)expr);
windowFunctions.add(wFnDef);
}
}
wdwTFnDef.setWindowFunctions(windowFunctions);
}
/*
* set outputFromWdwFnProcessing
*/
if ( windowFunctions.size() > 0 ) {
ArrayList<String> aliases = new ArrayList<String>();
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
for(WindowFunctionDef wFnDef : windowFunctions) {
aliases.add(wFnDef.getAlias());
if ( wFnDef.isPivotResult() ) {
fieldOIs.add(((ListObjectInspector)wFnDef.getOI()).getListElementObjectInspector());
} else {
fieldOIs.add(wFnDef.getOI());
}
}
PTFTranslator.addInputColumnsToList(inpShape, aliases, fieldOIs);
StructObjectInspector wdwOutOI = ObjectInspectorFactory.getStandardStructObjectInspector(
aliases, fieldOIs);
tFn.setWdwProcessingOutputOI(wdwOutOI);
RowResolver wdwOutRR = buildRowResolverForWindowing(wdwTFnDef, false);
ShapeDetails wdwOutShape = setupShape(wdwOutOI, null, wdwOutRR);
wdwTFnDef.setOutputFromWdwFnProcessing(wdwOutShape);
}
else {
wdwTFnDef.setOutputFromWdwFnProcessing(inpShape);
}
/*
* process Wdw expressions
*/
ShapeDetails wdwOutShape = wdwTFnDef.getOutputFromWdwFnProcessing();
ArrayList<WindowExpressionDef> windowExpressions = new ArrayList<WindowExpressionDef>();
if ( wdwSpec.getWindowExpressions() != null ) {
for(WindowExpressionSpec expr : wdwSpec.getWindowExpressions()) {
if ( !(expr instanceof WindowFunctionSpec) ) {
try {
PTFExpressionDef eDef = buildExpressionDef(wdwOutShape, expr.getExpression());
WindowExpressionDef wdwEDef = new WindowExpressionDef(eDef);
wdwEDef.setAlias(expr.getAlias());
windowExpressions.add(wdwEDef);
}
catch(HiveException he) {
throw new SemanticException(he);
}
}
}
wdwTFnDef.setWindowExpressions(windowExpressions);
}
/*
* set outputOI
*/
if ( windowExpressions.size() > 0 ) {
ArrayList<String> aliases = new ArrayList<String>();
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
for(WindowExpressionDef wEDef : windowExpressions) {
aliases.add(wEDef.getAlias());
fieldOIs.add(wEDef.getOI());
}
PTFTranslator.addInputColumnsToList(wdwOutShape, aliases, fieldOIs);
StructObjectInspector outOI = ObjectInspectorFactory.getStandardStructObjectInspector(
aliases, fieldOIs);
RowResolver outRR = buildRowResolverForWindowing(wdwTFnDef, true);
ShapeDetails outShape = setupShape(outOI, null, outRR);
wdwTFnDef.setOutputShape(outShape);
}
else {
wdwTFnDef.setOutputShape(copyShape(wdwOutShape));
}