phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anoopsamj...@apache.org
Subject git commit: PHOENIX-1075 Mathematical order of operations are improperly evaluated. (Kyle Buzsaki)
Date Sat, 12 Jul 2014 04:40:33 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master 97423a2b9 -> 0ef0b41cc


PHOENIX-1075 Mathematical order of operations are improperly evaluated. (Kyle Buzsaki)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/0ef0b41c
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/0ef0b41c
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/0ef0b41c

Branch: refs/heads/master
Commit: 0ef0b41cc35e6d7ad5ade4b2fd8e75436ba981c8
Parents: 97423a2
Author: anoopsjohn <anoopsamjohn@gmail.com>
Authored: Sat Jul 12 10:10:05 2014 +0530
Committer: anoopsjohn <anoopsamjohn@gmail.com>
Committed: Sat Jul 12 10:10:05 2014 +0530

----------------------------------------------------------------------
 .../phoenix/end2end/ArithmeticQueryIT.java      | 145 +++++++++++++++++++
 phoenix-core/src/main/antlr3/PhoenixSQL.g       |  19 +--
 2 files changed, 155 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/0ef0b41c/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArithmeticQueryIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArithmeticQueryIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArithmeticQueryIT.java
index 4874174..62a1639 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArithmeticQueryIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArithmeticQueryIT.java
@@ -591,4 +591,149 @@ public class ArithmeticQueryIT extends BaseHBaseManagedTimeIT {
         }
     }
     
+    private void initIntegerTable(Connection conn) throws SQLException {
+        String ddl = "CREATE TABLE ARITHMETIC_TEST (six INTEGER PRIMARY KEY, four INTEGER,
three INTEGER)";
+        conn.createStatement().execute(ddl);
+        String dml = "UPSERT INTO ARITHMETIC_TEST VALUES(6, 4, 3)";
+        conn.createStatement().execute(dml);
+        conn.commit();        
+    }
+    
+    @Test
+    public void testOrderOfOperationsAdditionSubtraction() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 + 4 - 3
+        // 10 - 3
+        // 7
+        rs = conn.createStatement().executeQuery("SELECT six + four - three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(7, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 - 3 + 6
+        // 1 + 6
+        // 7
+        rs = conn.createStatement().executeQuery("SELECT four - three + six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(7, rs.getLong(1));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testOrderOfOperationsAdditionMultiplication() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 + 4 * 3
+        // 6 + 12
+        // 18
+        rs = conn.createStatement().executeQuery("SELECT six + four * three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(18, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 * 3 + 6
+        // 12 * 6     
+        // 18
+        rs = conn.createStatement().executeQuery("SELECT four * three + six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(18, rs.getLong(1));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testOrderOfOperationsAdditionDivision() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 + 4 / 3
+        // 6 + 1
+        // 7
+        rs = conn.createStatement().executeQuery("SELECT six + four / three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(7, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 / 3 + 6
+        // 1 + 6     
+        // 7
+        rs = conn.createStatement().executeQuery("SELECT four / three + six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(7, rs.getLong(1));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testOrderOfOperationsSubtrationMultiplication() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 - 4 * 3
+        // 6 - 12
+        // -6
+        rs = conn.createStatement().executeQuery("SELECT six - four * three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(-6, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 * 3 - 6
+        // 12 - 6     
+        // 6
+        rs = conn.createStatement().executeQuery("SELECT four * three - six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(6, rs.getLong(1));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testOrderOfOperationsSubtractionDivision() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 - 4 / 3
+        // 6 - 1     (integer division)
+        // 5
+        rs = conn.createStatement().executeQuery("SELECT six - four / three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(5, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 / 3 - 6
+        // 1 - 6     (integer division)
+        // -5
+        rs = conn.createStatement().executeQuery("SELECT four / three - six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(-5, rs.getLong(1));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testOrderOfOperationsMultiplicationDivision() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        initIntegerTable(conn);
+        ResultSet rs;
+        
+        // 6 * 4 / 3
+        // 24 / 3
+        // 8
+        rs = conn.createStatement().executeQuery("SELECT six * four / three FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(8, rs.getLong(1));
+        assertFalse(rs.next());
+        
+        // 4 / 3 * 6
+        // 1 * 6     (integer division)
+        // 6
+        rs = conn.createStatement().executeQuery("SELECT four / three * six FROM ARITHMETIC_TEST");
+        assertTrue(rs.next());
+        assertEquals(6, rs.getLong(1));
+        assertFalse(rs.next());
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/0ef0b41c/phoenix-core/src/main/antlr3/PhoenixSQL.g
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g
index 73dc98f..0f7aba0 100644
--- a/phoenix-core/src/main/antlr3/PhoenixSQL.g
+++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g
@@ -731,17 +731,18 @@ subtract_expression returns [ParseNode ret]
 
 concat_expression returns [ParseNode ret]
 @init{List<ParseNode> l = new ArrayList<ParseNode>(4); }
-    :   i=multiply_expression {l.add(i);} (CONCAT i=multiply_expression {l.add(i);})* { $ret
= l.size() == 1 ? l.get(0) : factory.concat(l); }
+    :   i=multiply_divide_expression {l.add(i);} (CONCAT i=multiply_divide_expression {l.add(i);})*
{ $ret = l.size() == 1 ? l.get(0) : factory.concat(l); }
     ;
 
-multiply_expression returns [ParseNode ret]
-@init{List<ParseNode> l = new ArrayList<ParseNode>(4); }
-    :   i=divide_expression {l.add(i);} (ASTERISK i=divide_expression {l.add(i);})* { $ret
= l.size() == 1 ? l.get(0) : factory.multiply(l); }
-    ;
-
-divide_expression returns [ParseNode ret]
-@init{List<ParseNode> l = new ArrayList<ParseNode>(4); }
-    :   i=negate_expression {l.add(i);} (DIVIDE i=negate_expression {l.add(i);})* { $ret
= l.size() == 1 ? l.get(0) : factory.divide(l); }
+multiply_divide_expression returns [ParseNode ret]
+@init{ParseNode lhs = null; List<ParseNode> l;}
+    :   i=negate_expression {lhs = i;} 
+        (op=(ASTERISK | DIVIDE) rhs=negate_expression {
+            l = Arrays.asList(lhs, rhs); 
+            lhs = (op.getType() == ASTERISK ? factory.multiply(l) : factory.divide(l) );
+            }
+        )*
+        { $ret = lhs; }
     ;
 
 negate_expression returns [ParseNode ret]


Mime
View raw message