Permalink
Browse files

TEIID-2259 fix for condition nesting

  • Loading branch information...
1 parent 98123a6 commit af2837e5df979ecb817a8d4882bdc4659d47095a @shawkins shawkins committed Oct 11, 2012
View
@@ -1,4 +1,4 @@
-Portions Copyright (C) 2008-2011 Red Hat, Inc.
+Portions Copyright (C) 2008-2012 Red Hat, Inc.
Portions Copyright (C) 2000-2007 MetaMatrix, Inc.
Portions Copyright (c) 2000, 2003, 2008 IBM Corporation and others.
Portions Copyright (c) 1997-2000 Sun Microsystems, Inc.
@@ -450,7 +450,7 @@ public void visit(GroupBy obj) {
}
public void visit(In obj) {
- append(obj.getLeftExpression());
+ appendNested(obj.getLeftExpression());
if (obj.isNegated()) {
buffer.append(Tokens.SPACE)
.append(NOT);
@@ -509,7 +509,7 @@ public void visit(Parameter obj) {
}
public void visit(IsNull obj) {
- append(obj.getExpression());
+ appendNested(obj.getExpression());
buffer.append(Tokens.SPACE)
.append(IS)
.append(Tokens.SPACE);
@@ -519,6 +519,21 @@ public void visit(IsNull obj) {
}
buffer.append(NULL);
}
+
+ /**
+ * Condition operators have lower precedence than LIKE/SIMILAR/IS
+ * @param ex
+ */
+ private void appendNested(Expression ex) {
+ boolean useParens = ex instanceof Condition;
+ if (useParens) {
+ buffer.append(Tokens.LPAREN);
+ }
+ append(ex);
+ if (useParens) {
+ buffer.append(Tokens.RPAREN);
+ }
+ }
public void visit(Join obj) {
TableReference leftItem = obj.getLeftItem();
@@ -578,7 +593,7 @@ public void visit(Join obj) {
}
public void visit(Like obj) {
- append(obj.getLeftExpression());
+ append(obj.getLeftExpression());
if (obj.isNegated()) {
buffer.append(Tokens.SPACE)
.append(NOT);
@@ -354,7 +354,7 @@ public void visit( Drop obj ) {
public void visit( IsNullCriteria obj ) {
Expression expr = obj.getExpression();
- visitNode(expr);
+ appendNested(expr);
append(SPACE);
append(IS);
append(SPACE);
@@ -483,7 +483,7 @@ public void visit( JoinType obj ) {
}
public void visit( MatchCriteria obj ) {
- visitNode(obj.getLeftExpression());
+ visitNode(obj.getLeftExpression());
append(SPACE);
if (obj.isNegated()) {
@@ -515,7 +515,7 @@ public void visit( MatchCriteria obj ) {
append("'"); //$NON-NLS-1$
}
}
-
+
public void visit( NotCriteria obj ) {
append(NOT);
append(" ("); //$NON-NLS-1$
@@ -833,7 +833,7 @@ private void appendSourceHintValue(String sh) {
public void visit( SetCriteria obj ) {
// variable
- visitNode(obj.getExpression());
+ appendNested(obj.getExpression());
// operator and beginning of list
append(SPACE);
@@ -863,6 +863,21 @@ public void visit( SetCriteria obj ) {
}
append(")"); //$NON-NLS-1$
}
+
+ /**
+ * Condition operators have lower precedence than LIKE/SIMILAR/IS
+ * @param ex
+ */
+ private void appendNested(Expression ex) {
+ boolean useParens = ex instanceof Criteria;
+ if (useParens) {
+ append(Tokens.LPAREN);
+ }
+ visitNode(ex);
+ if (useParens) {
+ append(Tokens.RPAREN);
+ }
+ }
public void visit( SetQuery obj ) {
addCacheHint(obj.getCacheHint());
@@ -3052,7 +3052,7 @@ Token operator() :
/*
name=comparison predicate
description=A value comparison.
-example={code:sql}x = 'a'{code}
+example={code:sql}= 'a'{code}
*/
CompareCriteria compareCrit(ParseInfo info, Expression expression) :
{
@@ -295,11 +295,11 @@ private void helpTest(LanguageObject obj, String expectedStr) {
Insert insert = new Insert();
insert.setGroup(new GroupSymbol("m.g1")); //$NON-NLS-1$
- List vars = new ArrayList();
+ List<ElementSymbol> vars = new ArrayList<ElementSymbol>();
vars.add(new ElementSymbol("e1")); //$NON-NLS-1$
vars.add(new ElementSymbol("e2")); //$NON-NLS-1$
insert.setVariables(vars);
- List values = new ArrayList();
+ List<Constant> values = new ArrayList<Constant>();
values.add(new Constant(new Integer(5)));
values.add(new Constant("abc")); //$NON-NLS-1$
insert.setValues(values);
@@ -685,15 +685,15 @@ private void helpTest(LanguageObject obj, String expectedStr) {
@Test public void testSetCriteria1() {
SetCriteria sc = new SetCriteria();
sc.setExpression(new ElementSymbol("e1")); //$NON-NLS-1$
- sc.setValues(new ArrayList());
+ sc.setValues(new ArrayList<Expression>());
helpTest(sc, "e1 IN ()"); //$NON-NLS-1$
}
@Test public void testSetCriteria2() {
SetCriteria sc = new SetCriteria();
sc.setExpression(new ElementSymbol("e1")); //$NON-NLS-1$
- ArrayList values = new ArrayList();
+ ArrayList<Expression> values = new ArrayList<Expression>();
values.add(new ElementSymbol("e2")); //$NON-NLS-1$
values.add(new Constant("abc")); //$NON-NLS-1$
sc.setValues(values);
@@ -704,7 +704,7 @@ private void helpTest(LanguageObject obj, String expectedStr) {
@Test public void testSetCriteria3() {
SetCriteria sc = new SetCriteria();
sc.setExpression(new ElementSymbol("e1")); //$NON-NLS-1$
- ArrayList values = new ArrayList();
+ ArrayList<Expression> values = new ArrayList<Expression>();
values.add(null);
values.add(new Constant("b")); //$NON-NLS-1$
sc.setValues(values);
@@ -715,7 +715,7 @@ private void helpTest(LanguageObject obj, String expectedStr) {
@Test public void testSetCriteria4() {
SetCriteria sc = new SetCriteria();
sc.setExpression(new ElementSymbol("e1")); //$NON-NLS-1$
- ArrayList values = new ArrayList();
+ ArrayList<Expression> values = new ArrayList<Expression>();
values.add(new ElementSymbol("e2")); //$NON-NLS-1$
values.add(new Constant("abc")); //$NON-NLS-1$
sc.setValues(values);
@@ -1507,7 +1507,7 @@ private void helpTest(LanguageObject obj, String expectedStr) {
}
@Test public void testDynamicCommand() {
- List symbols = new ArrayList();
+ List<ElementSymbol> symbols = new ArrayList<ElementSymbol>();
ElementSymbol a1 = new ElementSymbol("a1"); //$NON-NLS-1$
a1.setType(DataTypeManager.DefaultDataClasses.STRING);
@@ -1720,5 +1720,11 @@ public Expression helpTestExpression(String sql, String expected) throws QueryPa
@Test public void testReturnStatement() throws QueryParserException {
helpTest(QueryParser.getQueryParser().parseProcedure("begin if (true) return 1; return; end", false), "CREATE VIRTUAL PROCEDURE\nBEGIN\nIF(TRUE)\nBEGIN\nRETURN 1;\nEND\nRETURN;\nEND");
}
+
+ @Test public void testConditionNesting() throws Exception {
+ String sql = "select (intkey = intnum) is null, (intkey < intnum) in (true, false) from bqt1.smalla";
+
+ helpTest(QueryParser.getQueryParser().parseCommand(sql), "SELECT (intkey = intnum) IS NULL, (intkey < intnum) IN (TRUE, FALSE) FROM bqt1.smalla"); //$NON-NLS-1$
+ }
}
@@ -135,7 +135,7 @@ private String getString(LanguageObject obj) {
@Test public void testVisitConvertFunctionOracleStyle() throws Exception {
String expected = "convert(columnA, integer)"; //$NON-NLS-1$
- List<? extends Expression> params = Arrays.asList(new ColumnReference(null, "columnA", null, String.class), new Literal("integer", String.class));
+ List<? extends Expression> params = Arrays.asList((Expression)new ColumnReference(null, "columnA", null, String.class), new Literal("integer", String.class));
Function test = new Function("convert", params, Integer.class); //$NON-NLS-1$
assertEquals(expected, getString(test));
@@ -440,5 +440,12 @@ public void substitute(Argument arg, StringBuilder builder, int index) {
String expected = "select $1 from $*1";
helpTestNativeParsing(sql, expected);
}
+
+ @Test public void testConditionNesting() throws Exception {
+ String sql = "select (intkey = intnum) is null, (intkey < intnum) in (true, false) from bqt1.smalla";
+
+ Command command = FakeTranslationFactory.getInstance().getBQTTranslationUtility().parseCommand(sql, true, true);
+ assertEquals("SELECT (g_0.IntKey = g_0.IntNum) IS NULL, (g_0.IntKey < g_0.IntNum) IN (TRUE, FALSE) FROM SmallA AS g_0", command.toString()); //$NON-NLS-1$
+ }
}

0 comments on commit af2837e

Please sign in to comment.