Skip to content

Commit 3b5312c

Browse files
committed
npath SymbolFn and ResultExpr testing
1 parent 243d7bb commit 3b5312c

File tree

2 files changed

+30
-80
lines changed

2 files changed

+30
-80
lines changed

windowing/src/main/java/com/sap/hadoop/windowing/functions2/table/npath/ResultExpressionParser.java

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.Iterator;
55

6-
import org.antlr.runtime.ANTLRStringStream;
7-
import org.antlr.runtime.CommonTokenStream;
8-
import org.antlr.runtime.tree.CommonTree;
9-
import org.antlr.runtime.tree.CommonTreeNodeStream;
106
import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator;
117
import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluatorFactory;
128
import org.apache.hadoop.hive.ql.metadata.HiveException;
@@ -20,9 +16,7 @@
2016

2117
import com.sap.hadoop.HiveUtils;
2218
import com.sap.hadoop.windowing.WindowingException;
23-
import com.sap.hadoop.windowing.parser.QSpecBuilder2;
24-
import com.sap.hadoop.windowing.parser.Windowing2Lexer;
25-
import com.sap.hadoop.windowing.parser.Windowing2Parser;
19+
import com.sap.hadoop.windowing.parser.ParseUtils;
2620
import com.sap.hadoop.windowing.query2.specification.SelectSpec;
2721
import com.sap.hadoop.windowing.query2.translate.TranslateUtils;
2822

@@ -128,64 +122,7 @@ private void fixResultExprString()
128122

129123
private void parse() throws WindowingException
130124
{
131-
Windowing2Lexer lexer;
132-
CommonTokenStream tokens;
133-
Windowing2Parser parser = null;
134-
CommonTree t;
135-
CommonTreeNodeStream nodes;
136-
QSpecBuilder2 qSpecBldr = null;
137-
String err;
138-
139-
try
140-
{
141-
lexer = new Windowing2Lexer(new ANTLRStringStream(resultExprString));
142-
tokens = new CommonTokenStream(lexer);
143-
parser = new Windowing2Parser(tokens);
144-
parser.setTreeAdaptor(TranslateUtils.adaptor);
145-
t = (CommonTree) parser.select().getTree();
146-
147-
err = parser.getWindowingParseErrors();
148-
if ( err != null )
149-
{
150-
throw new WindowingException(err);
151-
}
152-
}
153-
catch(Throwable te)
154-
{
155-
err = parser.getWindowingParseErrors();
156-
if ( err != null )
157-
{
158-
throw new WindowingException(err);
159-
}
160-
throw new WindowingException("Parse Error:" + te.toString(), te);
161-
}
162-
163-
TranslateUtils.unescapeStringLiterals((ASTNode) t);
164-
165-
try
166-
{
167-
168-
nodes = new CommonTreeNodeStream(t);
169-
nodes.setTokenStream(tokens);
170-
qSpecBldr = new QSpecBuilder2(nodes);
171-
selectSpec = qSpecBldr.select();
172-
173-
err = qSpecBldr.getWindowingParseErrors();
174-
if ( err != null )
175-
{
176-
throw new WindowingException(err);
177-
}
178-
179-
}
180-
catch(Throwable te)
181-
{
182-
err = qSpecBldr.getWindowingParseErrors();
183-
if ( err != null )
184-
{
185-
throw new WindowingException(err);
186-
}
187-
throw new WindowingException("Parse Error:" + te.toString(), te);
188-
}
125+
selectSpec = ParseUtils.parseSelect(resultExprString);
189126
}
190127

191128
private void validateSelectExpr() throws WindowingException

windowing/src/main/java/com/sap/hadoop/windowing/functions2/table/npath/SymbolFunction.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,22 @@ public SymbolFunction()
3434
result = new SymbolFunctionResult();
3535
}
3636

37-
public abstract SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException;
37+
public static SymbolFunctionResult match(SymbolFunction syFn, Object row, PartitionIterator<Object> pItr) throws WindowingException
38+
{
39+
int resetToIdx = pItr.getIndex() - 1;
40+
try
41+
{
42+
return syFn.match(row, pItr);
43+
}
44+
finally
45+
{
46+
pItr.resetToIndex(resetToIdx);
47+
}
48+
}
49+
50+
protected abstract SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException;
3851

39-
public abstract boolean isOptional();
52+
protected abstract boolean isOptional();
4053

4154
public static class Symbol extends SymbolFunction
4255
{
@@ -51,12 +64,12 @@ public Symbol(ExprNodeEvaluator symbolExprEval, ObjectInspector symbolOI)
5164
PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
5265
}
5366

54-
public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
67+
protected SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
5568
{
5669
Object val = null;
5770
try
5871
{
59-
symbolExprEval.evaluate(row);
72+
val = symbolExprEval.evaluate(row);
6073
}
6174
catch(HiveException he)
6275
{
@@ -69,7 +82,7 @@ public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) th
6982
return result;
7083
}
7184

72-
public boolean isOptional()
85+
protected boolean isOptional()
7386
{
7487
return false;
7588
}
@@ -84,7 +97,7 @@ public Star(SymbolFunction symbolFn)
8497
this.symbolFn = symbolFn;
8598
}
8699

87-
public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
100+
protected SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
88101
{
89102
result.matches = true;
90103
SymbolFunctionResult rowResult = symbolFn.match(row, pItr);
@@ -95,11 +108,11 @@ public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) th
95108
rowResult = symbolFn.match(row, pItr);
96109
}
97110

98-
result.nextRow = pItr.getIndex();
111+
result.nextRow = pItr.getIndex() - 1;
99112
return result;
100113
}
101114

102-
public boolean isOptional()
115+
protected boolean isOptional()
103116
{
104117
return true;
105118
}
@@ -114,14 +127,14 @@ public Plus(SymbolFunction symbolFn)
114127
this.symbolFn = symbolFn;
115128
}
116129

117-
public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
130+
protected SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
118131
{
119132
SymbolFunctionResult rowResult = symbolFn.match(row, pItr);
120133

121134
if ( !rowResult.matches )
122135
{
123136
result.matches = false;
124-
result.nextRow = pItr.getIndex();
137+
result.nextRow = pItr.getIndex() - 1;
125138
return result;
126139
}
127140

@@ -132,11 +145,11 @@ public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) th
132145
rowResult = symbolFn.match(row, pItr);
133146
}
134147

135-
result.nextRow = pItr.getIndex();
148+
result.nextRow = pItr.getIndex() - 1;
136149
return result;
137150
}
138151

139-
public boolean isOptional()
152+
protected boolean isOptional()
140153
{
141154
return false;
142155
}
@@ -162,7 +175,7 @@ public Chain(ArrayList<SymbolFunction> components)
162175
* - but if we come to a non optional Symbol Fn, return false.
163176
* - if we match all Fns in the chain return true.
164177
*/
165-
public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
178+
protected SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) throws WindowingException
166179
{
167180
SymbolFunctionResult componentResult = null;
168181
for(SymbolFunction sFn : components)
@@ -176,7 +189,7 @@ public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) th
176189
result.nextRow = componentResult.nextRow;
177190
return result;
178191
}
179-
row = pItr.hasNext() ? pItr.next() : null;
192+
row = pItr.resetToIndex(componentResult.nextRow);
180193
}
181194
else
182195
{
@@ -194,7 +207,7 @@ public SymbolFunctionResult match(Object row, PartitionIterator<Object> pItr) th
194207
return result;
195208
}
196209

197-
public boolean isOptional()
210+
protected boolean isOptional()
198211
{
199212
return false;
200213
}

0 commit comments

Comments
 (0)