Return-Path: X-Original-To: apmail-phoenix-commits-archive@minotaur.apache.org Delivered-To: apmail-phoenix-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0BD6F11FE2 for ; Sat, 12 Jul 2014 04:40:34 +0000 (UTC) Received: (qmail 78118 invoked by uid 500); 12 Jul 2014 04:40:34 -0000 Delivered-To: apmail-phoenix-commits-archive@phoenix.apache.org Received: (qmail 78083 invoked by uid 500); 12 Jul 2014 04:40:33 -0000 Mailing-List: contact commits-help@phoenix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@phoenix.apache.org Delivered-To: mailing list commits@phoenix.apache.org Received: (qmail 78074 invoked by uid 99); 12 Jul 2014 04:40:33 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Jul 2014 04:40:33 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 5D1E48B7660; Sat, 12 Jul 2014 04:40:33 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: anoopsamjohn@apache.org To: commits@phoenix.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: PHOENIX-1075 Mathematical order of operations are improperly evaluated. (Kyle Buzsaki) Date: Sat, 12 Jul 2014 04:40:33 +0000 (UTC) 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 Authored: Sat Jul 12 10:10:05 2014 +0530 Committer: anoopsjohn 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 l = new ArrayList(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 l = new ArrayList(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 l = new ArrayList(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 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]