Skip to content
Browse files

DERBY-5972: Grammar doesn't accept OR operator without parentheses

Update grammar to allow full value expressions where it previously
only allowed conjunctions of boolean primaries.

git-svn-id: https://svn.apache.org/repos/asf/db/derby/code/trunk@1408136 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 4ecfb0e commit eca1f7c6be455ce07bbd6f08f7a5c393817139a5 @kahatlen kahatlen committed Nov 11, 2012
View
11 java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
@@ -5334,7 +5334,7 @@ derivedColumn(ResultColumnList resultColumns) throws StandardException :
String columnName = null;
}
{
- columnExpression = orExpression( null )
+ columnExpression = valueExpression()
[ columnName = asClause() ]
{
/*
@@ -6090,7 +6090,7 @@ methodParameter(Vector parameterList) throws StandardException :
ValueNode parameter;
}
{
- parameter = orExpression(null)
+ parameter = valueExpression()
{
parameterList.addElement(parameter);
}
@@ -8575,7 +8575,7 @@ updateSource(String columnName) throws StandardException :
ValueNode valueNode;
}
{
- valueNode = orExpression(null)
+ valueNode = valueExpression()
{
return valueNode;
}
@@ -8830,7 +8830,7 @@ rowValueConstructorElement(ResultColumnList resultColumns) throws StandardExcept
ValueNode value;
}
{
- value = orExpression(null)
+ value = valueExpression()
{
resultColumns.addResultColumn(
(ResultColumn) nodeFactory.getNode(
@@ -12738,8 +12738,7 @@ whenThenExpression() throws StandardException :
ValueNode elseExpr;
}
{
- <WHEN> expr = orExpression(null)
- (<OR> expr = orExpression(expr) )*
+ <WHEN> expr = valueExpression()
<THEN> thenExpr = thenElseExpression()
elseExpr = caseExpression()
{
View
61 java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
@@ -59,6 +59,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
private static final String ILLEGAL_INSERT = "42821";
private static final String BAD_DEFAULT = "42894";
private static final String ILLEGAL_UPDATE = "XCL12";
+ private static final String NON_BOOLEAN_OPERAND = "42Y94";
///////////////////////////////////////////////////////////////////////////////////
//
@@ -2077,6 +2078,66 @@ public void test_5918() throws Exception
false
);
}
+
+ /**
+ * Verify that you can use AND and OR expressions as BOOLEAN values.
+ * Regression test case for DERBY-5972.
+ */
+ public void test_5972() throws SQLException {
+ // Disable auto-commit so that tearDown() can roll back any changes.
+ setAutoCommit(false);
+
+ Statement s = createStatement();
+
+ // Test boolean expressions in select list. Used to fail with syntax
+ // errors with OR expressions.
+ JDBC.assertSingleValueResultSet(
+ s.executeQuery("select true and false from sysibm.sysdummy1"),
+ "false");
+ JDBC.assertSingleValueResultSet(
+ s.executeQuery("select true or false from sysibm.sysdummy1"),
+ "true");
+ assertCompileError(NON_BOOLEAN_OPERAND,
+ "select 1 and 2 from sysibm.sysdummy1");
+ assertCompileError(NON_BOOLEAN_OPERAND,
+ "select 1 or 2 from sysibm.sysdummy1");
+
+ // Test boolean expressions in VALUES statements. Used to fail with
+ // syntax errors with OR expressions.
+ JDBC.assertSingleValueResultSet(
+ s.executeQuery("values true and false"), "false");
+ JDBC.assertSingleValueResultSet(
+ s.executeQuery("values true or false"), "true");
+ assertCompileError(NON_BOOLEAN_OPERAND, "values 1 and 2");
+ assertCompileError(NON_BOOLEAN_OPERAND, "values 1 or 2");
+
+ // Test boolean expressions as parameters in function calls. Used to
+ // result in syntax errors with OR expressions.
+ JDBC.assertSingleValueResultSet(s.executeQuery(
+ "values booleanValue(true and false)"), "False value");
+ JDBC.assertSingleValueResultSet(s.executeQuery(
+ "values booleanValue(true or false)"), "True value");
+ assertCompileError(NON_BOOLEAN_OPERAND, "values booleanValue(1 and 2)");
+ assertCompileError(NON_BOOLEAN_OPERAND, "values booleanValue(1 or 2)");
+
+ // Test boolean expressions in UPDATE statements. Used to fail with
+ // syntax errors with OR expressions.
+ s.execute("create table d5972(b boolean)");
+ assertUpdateCount(s, 0, "update d5972 set b = true and false");
+ assertUpdateCount(s, 0, "update d5972 set b = true or false");
+ assertCompileError(NON_BOOLEAN_OPERAND, "update d5972 set b = 1 and 2");
+ assertCompileError(NON_BOOLEAN_OPERAND, "update d5972 set b = 1 or 2");
+
+ // Used to work correctly. Verify for completeness.
+ JDBC.assertSingleValueResultSet(s.executeQuery(
+ "values case when true and false then 1 else 0 end"), "0");
+ JDBC.assertSingleValueResultSet(s.executeQuery(
+ "values case when true or false then 1 else 0 end"), "1");
+ assertCompileError(NON_BOOLEAN_OPERAND,
+ "values case when 1 and 2 then 1 else 0 end");
+ assertCompileError(NON_BOOLEAN_OPERAND,
+ "values case when 1 or 2 then 1 else 0 end");
+ }
///////////////////////////////////////////////////////////////////////////////////
//

0 comments on commit eca1f7c

Please sign in to comment.
Something went wrong with that request. Please try again.