1
1
package com .sap .hadoop .windowing .functions2 .table .npath ;
2
2
3
+ import java .util .ArrayList ;
4
+
5
+ import org .apache .hadoop .hive .ql .exec .ExprNodeEvaluator ;
6
+ import org .apache .hadoop .hive .ql .metadata .HiveException ;
7
+ import org .apache .hadoop .hive .serde2 .objectinspector .ConstantObjectInspector ;
8
+ import org .apache .hadoop .hive .serde2 .objectinspector .ObjectInspector ;
9
+ import org .apache .hadoop .hive .serde2 .objectinspector .ObjectInspectorUtils ;
10
+ import org .apache .hadoop .hive .serde2 .objectinspector .PrimitiveObjectInspector ;
11
+ import org .apache .hadoop .hive .serde2 .objectinspector .StructObjectInspector ;
12
+
3
13
import com .sap .hadoop .windowing .WindowingException ;
4
14
import com .sap .hadoop .windowing .functions2 .TableFunctionEvaluator ;
15
+ import com .sap .hadoop .windowing .functions2 .TableFunctionResolver ;
16
+ import com .sap .hadoop .windowing .functions2 .table .npath .SymbolFunction .SymbolFunctionResult ;
17
+ import com .sap .hadoop .windowing .query2 .definition .ArgDef ;
18
+ import com .sap .hadoop .windowing .query2 .definition .QueryDef ;
19
+ import com .sap .hadoop .windowing .query2 .definition .TableFuncDef ;
5
20
import com .sap .hadoop .windowing .runtime2 .Partition ;
21
+ import com .sap .hadoop .windowing .runtime2 .PartitionIterator ;
22
+ import com .sap .hadoop .windowing .runtime2 .RuntimeUtils ;
23
+
24
+ import static com .sap .hadoop .Utils .sprintf ;
6
25
7
26
/**
8
27
* return rows that meet a specified pattern. Use symbols to specify a list of expressions to match.
23
42
*/
24
43
public class NPath extends TableFunctionEvaluator
25
44
{
45
+ private transient String patternStr ;
46
+ private transient SymbolsInfo symInfo ;
47
+ private transient String resultExprStr ;
48
+ private transient SymbolFunction syFn ;
49
+ private transient ArrayList <ExprNodeEvaluator > resultExprEvals ;
50
+
51
+
52
+
53
+ @ Override
54
+ public void execute (Partition iPart , Partition outP ) throws WindowingException
55
+ {
56
+ PartitionIterator <Object > pItr = iPart .iterator ();
57
+ RuntimeUtils .connectLeadLagFunctionsToPartition (getQueryDef (), pItr );
58
+ while (pItr .hasNext ())
59
+ {
60
+ Object iRow = pItr .next ();
61
+
62
+ SymbolFunctionResult syFnRes = SymbolFunction .match (syFn , iRow , pItr );
63
+ if (syFnRes .matches )
64
+ {
65
+ int sz = syFnRes .nextRow - (pItr .getIndex () - 1 );
66
+ Object selectListInput = NPathUtils .getSelectListInput (iRow , tDef .getInput ().getOI (), pItr , sz );
67
+ ArrayList <Object > oRow = new ArrayList <Object >();
68
+ for (ExprNodeEvaluator resExprEval : resultExprEvals )
69
+ {
70
+ try
71
+ {
72
+ oRow .add (resExprEval .evaluate (selectListInput ));
73
+ }
74
+ catch (HiveException he )
75
+ {
76
+ throw new WindowingException (he );
77
+ }
78
+ }
79
+ outP .append (oRow );
80
+ }
81
+ }
82
+ }
83
+
26
84
/**
27
85
* <ul>
28
86
* <li> check structure of Arguments:
@@ -40,12 +98,137 @@ public class NPath extends TableFunctionEvaluator
40
98
@ Override
41
99
public void setupOI () throws WindowingException
42
100
{
101
+ ArrayList <ArgDef > args = tDef .getArgs ();
102
+ int argsNum = args == null ? 0 : args .size ();
103
+
104
+ if ( argsNum < 4 )
105
+ {
106
+ throwErrorWithSignature ("at least 4 arguments required" );
107
+ }
108
+
109
+ /*
110
+ * validate and setup patternStr
111
+ */
112
+ ArgDef symboPatternArg = args .get (0 );
113
+ ObjectInspector symbolPatternArgOI = symboPatternArg .getOI ();
114
+
115
+ if ( !ObjectInspectorUtils .isConstantObjectInspector (symbolPatternArgOI ) ||
116
+ (symbolPatternArgOI .getCategory () != ObjectInspector .Category .PRIMITIVE ) ||
117
+ ((PrimitiveObjectInspector )symbolPatternArgOI ).getPrimitiveCategory () != PrimitiveObjectInspector .PrimitiveCategory .STRING )
118
+ {
119
+ throwErrorWithSignature ("Currently the symbol Pattern must be a Constant String." );
120
+ }
121
+
122
+ patternStr = ((ConstantObjectInspector )symbolPatternArgOI ).getWritableConstantValue ().toString ();
123
+
124
+ /*
125
+ * validate and setup SymbolInfo
126
+ */
127
+ int symbolArgsSz = argsNum - 2 ;
128
+ if ( symbolArgsSz % 2 != 0 )
129
+ {
130
+ throwErrorWithSignature ("Symbol Name, Expression need to be specified in pairs: there are odd number of symbol args" );
131
+ }
132
+
133
+ symInfo = new SymbolsInfo (symbolArgsSz /2 );
134
+ for (int i =1 ; i <= symbolArgsSz ; i += 2 )
135
+ {
136
+ ArgDef symbolNameArg = args .get (i );
137
+ ObjectInspector symbolNameArgOI = symbolNameArg .getOI ();
138
+
139
+ if ( !ObjectInspectorUtils .isConstantObjectInspector (symbolNameArgOI ) ||
140
+ (symbolNameArgOI .getCategory () != ObjectInspector .Category .PRIMITIVE ) ||
141
+ ((PrimitiveObjectInspector )symbolNameArgOI ).getPrimitiveCategory () != PrimitiveObjectInspector .PrimitiveCategory .STRING )
142
+ {
143
+ throwErrorWithSignature (sprintf ("Currently a Symbol Name(%s) must be a Constant String" , symbolNameArg .getExpression ().toStringTree ()));
144
+ }
145
+ String symbolName = ((ConstantObjectInspector )symbolNameArgOI ).getWritableConstantValue ().toString ();
146
+
147
+ ArgDef symolExprArg = args .get (i +1 );
148
+ ObjectInspector symolExprArgOI = symolExprArg .getOI ();
149
+ if ( (symolExprArgOI .getCategory () != ObjectInspector .Category .PRIMITIVE ) ||
150
+ ((PrimitiveObjectInspector )symolExprArgOI ).getPrimitiveCategory () != PrimitiveObjectInspector .PrimitiveCategory .BOOLEAN )
151
+ {
152
+ throwErrorWithSignature (sprintf ("Currently a Symbol Expression(%s) must be a boolean expression" , symolExprArg .getExpression ().toStringTree ()));
153
+ }
154
+ symInfo .add (symbolName , symolExprArg );
155
+ }
156
+
157
+ /*
158
+ * validate and setup resultExprStr
159
+ */
160
+ ArgDef resultExprArg = args .get (argsNum - 1 );
161
+ ObjectInspector resultExprArgOI = resultExprArg .getOI ();
162
+
163
+ if ( !ObjectInspectorUtils .isConstantObjectInspector (resultExprArgOI ) ||
164
+ (resultExprArgOI .getCategory () != ObjectInspector .Category .PRIMITIVE ) ||
165
+ ((PrimitiveObjectInspector )resultExprArgOI ).getPrimitiveCategory () != PrimitiveObjectInspector .PrimitiveCategory .STRING )
166
+ {
167
+ throwErrorWithSignature ("Currently the result Expr parameter must be a Constant String." );
168
+ }
169
+
170
+ resultExprStr = ((ConstantObjectInspector )resultExprArgOI ).getWritableConstantValue ().toString ();
171
+
172
+ /*
173
+ * setup SymbolFunction chain.
174
+ */
175
+ SymbolParser syP = new SymbolParser (patternStr , symInfo .symbolExprsNames , symInfo .symbolExprsEvaluators , symInfo .symbolExprsOIs );
176
+ syP .parse ();
177
+ syFn = syP .getSymbolFunction ();
178
+
179
+ /*
180
+ * setup OI for input to resultExpr select list
181
+ */
182
+ StructObjectInspector selectListInputOI = (StructObjectInspector ) NPathUtils .createSelectListInputOI (tDef .getInput ().getOI ());
183
+
184
+ /*
185
+ * parse ResultExpr Str and setup OI.
186
+ */
187
+ ResultExpressionParser resultExprParser = new ResultExpressionParser (resultExprStr , selectListInputOI );
188
+ resultExprParser .translate ();
189
+ resultExprEvals = resultExprParser .getSelectListExprEvaluators ();
190
+ OI = resultExprParser .getSelectListOutputOI ();
191
+ }
192
+
193
+ private void throwErrorWithSignature (String message ) throws WindowingException
194
+ {
195
+ throw new WindowingException (sprintf (
196
+ "NPath signature is: SymbolPattern, one or more SymbolName, expression pairs, the result expression as a select list. Error %s" ,
197
+ message ));
198
+ }
199
+
200
+ public static class NPathResolver extends TableFunctionResolver
201
+ {
202
+
203
+ @ Override
204
+ protected TableFunctionEvaluator createEvaluator (QueryDef qDef , TableFuncDef tDef )
205
+ {
206
+
207
+ return new NPath ();
208
+ }
43
209
44
210
}
45
211
46
- @ Override
47
- public void execute (Partition iPart , Partition outP ) throws WindowingException
212
+ static class SymbolsInfo
48
213
{
214
+ int sz ;
215
+ ArrayList <ExprNodeEvaluator > symbolExprsEvaluators ;
216
+ ArrayList <ObjectInspector > symbolExprsOIs ;
217
+ ArrayList <String > symbolExprsNames ;
218
+
219
+ SymbolsInfo (int sz )
220
+ {
221
+ this .sz = sz ;
222
+ symbolExprsEvaluators = new ArrayList <ExprNodeEvaluator >(sz );
223
+ symbolExprsOIs = new ArrayList <ObjectInspector >(sz );
224
+ symbolExprsNames = new ArrayList <String >(sz );
225
+ }
49
226
227
+ void add (String name , ArgDef arg )
228
+ {
229
+ symbolExprsNames .add (name );
230
+ symbolExprsEvaluators .add (arg .getExprEvaluator ());
231
+ symbolExprsOIs .add (arg .getOI ());
232
+ }
50
233
}
51
234
}
0 commit comments