Merge lp:~mmcm/akiban-sql-parser/simple-case into lp:~akiban-technologies/akiban-sql-parser/trunk

Proposed by Mike McMahon
Status: Merged
Approved by: Mike McMahon
Approved revision: 304
Merged at revision: 305
Proposed branch: lp:~mmcm/akiban-sql-parser/simple-case
Merge into: lp:~akiban-technologies/akiban-sql-parser/trunk
Diff against target: 513 lines (+323/-64)
9 files modified
pom.xml (+1/-1)
src/main/java/com/akiban/sql/parser/NodeFactoryImpl.java (+18/-15)
src/main/java/com/akiban/sql/parser/NodeNames.java (+17/-15)
src/main/java/com/akiban/sql/parser/NodeTypes.java (+16/-15)
src/main/java/com/akiban/sql/parser/SimpleCaseNode.java (+163/-0)
src/main/java/com/akiban/sql/unparser/NodeToString.java (+19/-0)
src/main/javacc/SQLGrammar.jj (+87/-18)
src/test/resources/com/akiban/sql/unparser/select-16.expected (+1/-0)
src/test/resources/com/akiban/sql/unparser/select-16.sql (+1/-0)
To merge this branch: bzr merge lp:~mmcm/akiban-sql-parser/simple-case
Reviewer Review Type Date Requested Status
Mike McMahon Approve
Thomas Jones-Low Needs Fixing
Nathan Williams Approve
Review via email: mp+177936@code.launchpad.net

Description of the change

Add parsing of simple CASE.

Also restore the version number, which got messed up due to some changes only in /build.

To post a comment you must log in.
Revision history for this message
Nathan Williams (nwilliams) wrote :

As described.

review: Approve
Revision history for this message
Thomas Jones-Low (tjoneslo) wrote :

There was one failure during build/test:

* during merge: merge conflicts while merging lp:~mmcm/akiban-sql-parser/simple-case

review: Needs Fixing
305. By Mike McMahon

Merge from trunk.

Revision history for this message
Mike McMahon (mmcm) wrote :

Conflicts resolved.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pom.xml'
2--- pom.xml 2013-07-31 14:50:41 +0000
3+++ pom.xml 2013-08-07 17:46:26 +0000
4@@ -13,7 +13,7 @@
5
6 <groupId>com.akiban</groupId>
7 <artifactId>akiban-sql-parser</artifactId>
8- <version>1.0.15-SNAPSHOT</version>
9+ <version>1.0.16-SNAPSHOT</version>
10 <packaging>jar</packaging>
11
12 <name>Akiban SQL Parser</name>
13
14=== modified file 'src/main/java/com/akiban/sql/parser/NodeFactoryImpl.java'
15--- src/main/java/com/akiban/sql/parser/NodeFactoryImpl.java 2013-07-31 23:47:55 +0000
16+++ src/main/java/com/akiban/sql/parser/NodeFactoryImpl.java 2013-08-07 17:46:26 +0000
17@@ -1,18 +1,18 @@
18-/**
19- * Copyright 2011-2013 Akiban Technologies, Inc.
20- *
21- * Licensed under the Apache License, Version 2.0 (the "License");
22- * you may not use this file except in compliance with the License.
23- * You may obtain a copy of the License at
24- *
25- * http://www.apache.org/licenses/LICENSE-2.0
26- *
27- * Unless required by applicable law or agreed to in writing, software
28- * distributed under the License is distributed on an "AS IS" BASIS,
29- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30- * See the License for the specific language governing permissions and
31- * limitations under the License.
32- */
33+/**
34+ * Copyright 2011-2013 Akiban Technologies, Inc.
35+ *
36+ * Licensed under the Apache License, Version 2.0 (the "License");
37+ * you may not use this file except in compliance with the License.
38+ * You may obtain a copy of the License at
39+ *
40+ * http://www.apache.org/licenses/LICENSE-2.0
41+ *
42+ * Unless required by applicable law or agreed to in writing, software
43+ * distributed under the License is distributed on an "AS IS" BASIS,
44+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45+ * See the License for the specific language governing permissions and
46+ * limitations under the License.
47+ */
48
49 /* The original from which this derives bore the following: */
50
51@@ -653,6 +653,9 @@
52 case NodeTypes.DEALLOCATE_STATEMENT_NODE:
53 return NodeNames.DEALLOCATE_STATEMENT_NODE_NAME;
54
55+ case NodeTypes.SIMPLE_CASE_NODE:
56+ return NodeNames.SIMPLE_CASE_NODE_NAME;
57+
58 case NodeTypes.PARTITION_BY_LIST:
59 return NodeNames.PARTITION_BY_LIST_NAME;
60
61
62=== modified file 'src/main/java/com/akiban/sql/parser/NodeNames.java'
63--- src/main/java/com/akiban/sql/parser/NodeNames.java 2013-07-31 23:47:55 +0000
64+++ src/main/java/com/akiban/sql/parser/NodeNames.java 2013-08-07 17:46:26 +0000
65@@ -1,18 +1,18 @@
66-/**
67- * Copyright 2011-2013 Akiban Technologies, Inc.
68- *
69- * Licensed under the Apache License, Version 2.0 (the "License");
70- * you may not use this file except in compliance with the License.
71- * You may obtain a copy of the License at
72- *
73- * http://www.apache.org/licenses/LICENSE-2.0
74- *
75- * Unless required by applicable law or agreed to in writing, software
76- * distributed under the License is distributed on an "AS IS" BASIS,
77- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78- * See the License for the specific language governing permissions and
79- * limitations under the License.
80- */
81+/**
82+ * Copyright 2011-2013 Akiban Technologies, Inc.
83+ *
84+ * Licensed under the Apache License, Version 2.0 (the "License");
85+ * you may not use this file except in compliance with the License.
86+ * You may obtain a copy of the License at
87+ *
88+ * http://www.apache.org/licenses/LICENSE-2.0
89+ *
90+ * Unless required by applicable law or agreed to in writing, software
91+ * distributed under the License is distributed on an "AS IS" BASIS,
92+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93+ * See the License for the specific language governing permissions and
94+ * limitations under the License.
95+ */
96
97 /* The original from which this derives bore the following: */
98
99@@ -327,6 +327,8 @@
100
101 static final String SET_TRANSACTION_ISOLATION_NODE_NAME = "com.akiban.sql.parser.SetTransactionIsolationNode";
102
103+ static final String SIMPLE_CASE_NODE_NAME = "com.akiban.sql.parser.SimpleCaseNode";
104+
105 static final String SIMPLE_STRING_OPERATOR_NODE_NAME = "com.akiban.sql.parser.SimpleStringOperatorNode";
106
107 static final String SPECIAL_FUNCTION_NODE_NAME = "com.akiban.sql.parser.SpecialFunctionNode";
108
109=== modified file 'src/main/java/com/akiban/sql/parser/NodeTypes.java'
110--- src/main/java/com/akiban/sql/parser/NodeTypes.java 2013-07-31 23:47:55 +0000
111+++ src/main/java/com/akiban/sql/parser/NodeTypes.java 2013-08-07 17:46:26 +0000
112@@ -1,18 +1,18 @@
113-/**
114- * Copyright 2011-2013 Akiban Technologies, Inc.
115- *
116- * Licensed under the Apache License, Version 2.0 (the "License");
117- * you may not use this file except in compliance with the License.
118- * You may obtain a copy of the License at
119- *
120- * http://www.apache.org/licenses/LICENSE-2.0
121- *
122- * Unless required by applicable law or agreed to in writing, software
123- * distributed under the License is distributed on an "AS IS" BASIS,
124- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125- * See the License for the specific language governing permissions and
126- * limitations under the License.
127- */
128+/**
129+ * Copyright 2011-2013 Akiban Technologies, Inc.
130+ *
131+ * Licensed under the Apache License, Version 2.0 (the "License");
132+ * you may not use this file except in compliance with the License.
133+ * You may obtain a copy of the License at
134+ *
135+ * http://www.apache.org/licenses/LICENSE-2.0
136+ *
137+ * Unless required by applicable law or agreed to in writing, software
138+ * distributed under the License is distributed on an "AS IS" BASIS,
139+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140+ * See the License for the specific language governing permissions and
141+ * limitations under the License.
142+ */
143
144 /* The original from which this derives bore the following: */
145
146@@ -217,6 +217,7 @@
147 public static final int GROUP_CONCAT_NODE = 167;
148 public static final int AT_RENAME_NODE = 168;
149 public static final int AT_RENAME_COLUMN_NODE = 169;
150+ public static final int SIMPLE_CASE_NODE = 170;
151 public static final int PARTITION_BY_LIST = 171;
152 public static final int PARTITION_BY_COLUMN = 172;
153 public static final int FULL_OUTER_JOIN_NODE = 173;
154
155=== added file 'src/main/java/com/akiban/sql/parser/SimpleCaseNode.java'
156--- src/main/java/com/akiban/sql/parser/SimpleCaseNode.java 1970-01-01 00:00:00 +0000
157+++ src/main/java/com/akiban/sql/parser/SimpleCaseNode.java 2013-08-07 17:46:26 +0000
158@@ -0,0 +1,163 @@
159+/**
160+ * Copyright 2011-2013 Akiban Technologies, Inc.
161+ *
162+ * Licensed under the Apache License, Version 2.0 (the "License");
163+ * you may not use this file except in compliance with the License.
164+ * You may obtain a copy of the License at
165+ *
166+ * http://www.apache.org/licenses/LICENSE-2.0
167+ *
168+ * Unless required by applicable law or agreed to in writing, software
169+ * distributed under the License is distributed on an "AS IS" BASIS,
170+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
171+ * See the License for the specific language governing permissions and
172+ * limitations under the License.
173+ */
174+
175+package com.akiban.sql.parser;
176+
177+import com.akiban.sql.StandardException;
178+
179+/**
180+ * A SimpleCaseNode represents the CASE <expr> THEN ... form.
181+ */
182+
183+public class SimpleCaseNode extends ValueNode
184+{
185+ private ValueNode operand;
186+ private ValueNodeList caseOperands, resultValues;
187+ private ValueNode elseValue;
188+
189+ /**
190+ * Initializer for a SimpleCaseNode
191+ *
192+ * @param operand The expression being compared
193+ */
194+
195+ public void init(Object operand) throws StandardException {
196+ this.operand = (ValueNode)operand;
197+ this.caseOperands = (ValueNodeList)getNodeFactory().getNode(NodeTypes.VALUE_NODE_LIST,
198+ getParserContext());
199+ this.resultValues = (ValueNodeList)getNodeFactory().getNode(NodeTypes.VALUE_NODE_LIST,
200+ getParserContext());
201+ }
202+
203+ /**
204+ * Fill this node with a deep copy of the given node.
205+ */
206+ public void copyFrom(QueryTreeNode node) throws StandardException {
207+ super.copyFrom(node);
208+
209+ SimpleCaseNode other = (SimpleCaseNode)node;
210+ this.operand = (ValueNode)
211+ getNodeFactory().copyNode(other.operand, getParserContext());
212+ this.caseOperands = (ValueNodeList)
213+ getNodeFactory().copyNode(other.caseOperands, getParserContext());
214+ this.resultValues = (ValueNodeList)
215+ getNodeFactory().copyNode(other.resultValues, getParserContext());
216+ if (other.elseValue == null)
217+ this.elseValue = null;
218+ else
219+ this.elseValue = (ValueNode)
220+ getNodeFactory().copyNode(other.elseValue, getParserContext());
221+ }
222+
223+ public ValueNode getOperand() {
224+ return operand;
225+ }
226+
227+ public ValueNodeList getCaseOperands() {
228+ return caseOperands;
229+ }
230+
231+ public ValueNodeList getResultValues() {
232+ return resultValues;
233+ }
234+
235+ public ValueNode getElseValue() {
236+ return elseValue;
237+ }
238+
239+ public void setElseValue(ValueNode elseValue) {
240+ this.elseValue = elseValue;
241+ }
242+
243+ /** The number of <code>WHEN</code> cases. */
244+ public int getNumberOfCases() {
245+ return caseOperands.size();
246+ }
247+
248+ /** The <code>WHEN</code> part. */
249+ public ValueNode getCaseOperand(int index) {
250+ return caseOperands.get(index);
251+ }
252+
253+ /** The <code>THEN</code> part. */
254+ public ValueNode getResultValue(int index) {
255+ return resultValues.get(index);
256+ }
257+
258+ public void addCase(ValueNode operand, ValueNode result) {
259+ caseOperands.add(operand);
260+ resultValues.add(result);
261+ }
262+
263+ /**
264+ * Prints the sub-nodes of this object. See QueryTreeNode.java for
265+ * how tree printing is supposed to work.
266+ *
267+ * @param depth The depth of this node in the tree
268+ */
269+
270+ public void printSubNodes(int depth) {
271+ super.printSubNodes(depth);
272+
273+ printLabel(depth, "operand: ");
274+ operand.treePrint(depth + 1);
275+
276+ for (int i = 0; i < getNumberOfCases(); i++) {
277+ printLabel(depth, "when: ");
278+ getCaseOperand(i).treePrint(depth + 1);
279+ printLabel(depth, "then: ");
280+ getResultValue(i).treePrint(depth + 1);
281+ }
282+
283+ if (elseValue != null) {
284+ printLabel(depth, "else: ");
285+ elseValue.treePrint(depth + 1);
286+ }
287+ }
288+
289+ /**
290+ * Accept the visitor for all visitable children of this node.
291+ *
292+ * @param v the visitor
293+ *
294+ * @exception StandardException on error
295+ */
296+ void acceptChildren(Visitor v) throws StandardException {
297+ super.acceptChildren(v);
298+
299+ operand = (ValueNode)operand.accept(v);
300+ caseOperands = (ValueNodeList)caseOperands.accept(v);
301+ resultValues = (ValueNodeList)resultValues.accept(v);
302+ if (elseValue != null)
303+ elseValue = (ValueNode)elseValue.accept(v);
304+ }
305+
306+ /**
307+ * {@inheritDoc}
308+ */
309+ protected boolean isEquivalent(ValueNode o) throws StandardException {
310+ if (isSameNodeType(o)) {
311+ SimpleCaseNode other = (SimpleCaseNode)o;
312+ return operand.isEquivalent(other.operand) &&
313+ caseOperands.isEquivalent(other.caseOperands) &&
314+ resultValues.isEquivalent(other.resultValues) &&
315+ ((elseValue == null) ? (other.elseValue == null) :
316+ elseValue.isEquivalent(other.elseValue));
317+ }
318+ return false;
319+ }
320+
321+}
322
323=== modified file 'src/main/java/com/akiban/sql/unparser/NodeToString.java'
324--- src/main/java/com/akiban/sql/unparser/NodeToString.java 2013-07-31 23:59:21 +0000
325+++ src/main/java/com/akiban/sql/unparser/NodeToString.java 2013-08-07 17:46:26 +0000
326@@ -198,6 +198,8 @@
327 return betweenOperatorNode((BetweenOperatorNode)node);
328 case NodeTypes.CONDITIONAL_NODE:
329 return conditionalNode((ConditionalNode)node);
330+ case NodeTypes.SIMPLE_CASE_NODE:
331+ return simpleCaseNode((SimpleCaseNode)node);
332 case NodeTypes.COALESCE_FUNCTION_NODE:
333 return coalesceFunctionNode((CoalesceFunctionNode)node);
334 case NodeTypes.AGGREGATE_NODE:
335@@ -1076,6 +1078,23 @@
336 return str.toString();
337 }
338
339+ protected String simpleCaseNode(SimpleCaseNode node) throws StandardException {
340+ StringBuilder str = new StringBuilder("CASE ");
341+ str.append(maybeParens(node.getOperand()));
342+ for (int i = 0; i < node.getNumberOfCases(); i++) {
343+ str.append(" WHEN ");
344+ str.append(maybeParens(node.getCaseOperand(i)));
345+ str.append(" THEN ");
346+ str.append(maybeParens(node.getResultValue(i)));
347+ }
348+ if (node.getElseValue() != null) {
349+ str.append(" ELSE ");
350+ str.append(maybeParens(node.getElseValue()));
351+ }
352+ str.append(" END");
353+ return str.toString();
354+ }
355+
356 protected String coalesceFunctionNode(CoalesceFunctionNode node)
357 throws StandardException {
358 return functionCall(node.getFunctionName(),
359
360=== modified file 'src/main/javacc/SQLGrammar.jj'
361--- src/main/javacc/SQLGrammar.jj 2013-08-03 18:34:52 +0000
362+++ src/main/javacc/SQLGrammar.jj 2013-08-07 17:46:26 +0000
363@@ -12551,7 +12551,7 @@
364 parserContext);
365 }
366 |
367- <CASE> value = whenThenExpression()
368+ <CASE> value = caseExpression()
369 {
370 return value;
371 }
372@@ -12563,26 +12563,16 @@
373 ValueNode expr;
374 }
375 {
376- <END>
377- {
378- ValueNode value = (ValueNode)nodeFactory.getNode(NodeTypes.CAST_NODE,
379- (ValueNode)nodeFactory.getNode(NodeTypes.UNTYPED_NULL_CONSTANT_NODE,
380- parserContext),
381- DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.CHAR, 1),
382- parserContext);
383- ((CastNode)value).setForExternallyGeneratedCASTnode();
384- return value;
385- }
386-|
387- <ELSE> expr = thenElseExpression() <END>
388- {
389- return expr;
390- }
391-|
392+ LOOKAHEAD ( { getToken(1).kind == WHEN } )
393 expr = whenThenExpression()
394 {
395 return expr;
396 }
397+|
398+ expr = simpleCaseExpression()
399+ {
400+ return expr;
401+ }
402 }
403
404 ValueNode
405@@ -12596,7 +12586,7 @@
406 <WHEN> expr = orExpression(null)
407 ( <OR> expr = orExpression(expr) )*
408 <THEN> thenExpr = thenElseExpression()
409- elseExpr = caseExpression()
410+ elseExpr = caseElseExpression()
411 {
412 ValueNodeList thenElseList = (ValueNodeList)nodeFactory.getNode(NodeTypes.VALUE_NODE_LIST, parserContext);
413 thenElseList.addValueNode(thenExpr); // then
414@@ -12634,6 +12624,85 @@
415 }
416 }
417
418+ValueNode
419+caseElseExpression() throws StandardException :
420+{
421+ ValueNode expr;
422+}
423+{
424+ <END>
425+ {
426+ ValueNode value = (ValueNode)nodeFactory.getNode(NodeTypes.CAST_NODE,
427+ (ValueNode)nodeFactory.getNode(NodeTypes.UNTYPED_NULL_CONSTANT_NODE,
428+ parserContext),
429+ DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.CHAR, 1),
430+ parserContext);
431+ ((CastNode)value).setForExternallyGeneratedCASTnode();
432+ return value;
433+ }
434+|
435+ <ELSE> expr = thenElseExpression() <END>
436+ {
437+ return expr;
438+ }
439+|
440+ expr = whenThenExpression()
441+ {
442+ return expr;
443+ }
444+}
445+
446+ValueNode
447+simpleCaseExpression() throws StandardException :
448+{
449+ ValueNode operand;
450+ SimpleCaseNode caseExpr;
451+}
452+{
453+ operand = additiveExpression()
454+ {
455+ caseExpr = (SimpleCaseNode)nodeFactory.getNode(NodeTypes.SIMPLE_CASE_NODE,
456+ operand,
457+ parserContext);
458+ }
459+ simpleCaseWhenThenExpression(caseExpr)
460+ {
461+ return caseExpr;
462+ }
463+}
464+
465+void
466+simpleCaseWhenThenExpression(SimpleCaseNode caseExpr) throws StandardException :
467+{
468+ ValueNode expr;
469+ ValueNode thenExpr;
470+ ValueNode elseExpr;
471+}
472+{
473+ <WHEN> expr = additiveExpression()
474+ <THEN> thenExpr = thenElseExpression()
475+ {
476+ caseExpr.addCase(expr, thenExpr);
477+ }
478+ simpleCaseElseExpression(caseExpr)
479+}
480+
481+void
482+simpleCaseElseExpression(SimpleCaseNode caseExpr) throws StandardException :
483+{
484+ ValueNode expr;
485+}
486+{
487+ <END>
488+|
489+ <ELSE> expr = thenElseExpression() <END>
490+ {
491+ caseExpr.setElseValue(expr);
492+ }
493+|
494+ simpleCaseWhenThenExpression(caseExpr)
495+}
496+
497 void
498 tableConstraintDefinition(TableElementList tableElementList) throws StandardException :
499 {
500
501=== added file 'src/test/resources/com/akiban/sql/unparser/select-16.expected'
502--- src/test/resources/com/akiban/sql/unparser/select-16.expected 1970-01-01 00:00:00 +0000
503+++ src/test/resources/com/akiban/sql/unparser/select-16.expected 2013-08-07 17:46:26 +0000
504@@ -0,0 +1,1 @@
505+SELECT (CASE x WHEN y THEN x WHEN z THEN y ELSE w END) FROM t
506\ No newline at end of file
507
508=== added file 'src/test/resources/com/akiban/sql/unparser/select-16.sql'
509--- src/test/resources/com/akiban/sql/unparser/select-16.sql 1970-01-01 00:00:00 +0000
510+++ src/test/resources/com/akiban/sql/unparser/select-16.sql 2013-08-07 17:46:26 +0000
511@@ -0,0 +1,1 @@
512+SELECT CASE x WHEN y THEN x WHEN z THEN y ELSE w END FROM t
513\ No newline at end of file

Subscribers

People subscribed via source and target branches