From fd7084d34a7903c0337e8e8394de5e8366e4f5b8 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 14 Apr 2015 13:49:24 -0700 Subject: [PATCH 1/3] Exponential notation parsing and tests --- .../phoenix/end2end/ArithmeticQueryIT.java | 46 +++++++++++++++++++ phoenix-core/src/main/antlr3/PhoenixSQL.g | 9 +++- 2 files changed, 53 insertions(+), 2 deletions(-) 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 72eb01671a9..71741b85c80 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 @@ -985,4 +985,50 @@ public void testFloatingPointMultiplicationUpsert() throws Exception { assertTrue(rs.next()); assertEquals(-1.0f, rs.getFloat(1), 0.001); } + + @Test + public void testFloatingPointWithExponentialNotation() throws Exception { + Float[] expected = {1.5E7f, 1.5E-7f, -1.5E-7f, 12E-5f, -.12E+34f}; + String[] values = {"1.5e7", "1.5e-7", "-1.5e-7", "12E-5", "-.12E+34"}; + ResultSet rs = createTableWithValues(values, "FLOAT"); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], rs.getFloat(i+1), 0.001); + } + } + + @Test + public void testDoubleWithExponentialNotation() throws Exception { + Double[] expected = {1.5E7d, 1.5E-7d, -1.5E-7d, 12E-5d, -.654E-321d, .1234E+56d}; + String[] values = {"1.5e7", "1.5e-7", "-1.5e-7", "12E-5", "-.654E-321", ".1234E+56"}; + ResultSet rs = createTableWithValues(values, "DOUBLE"); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], rs.getDouble(i+1), 0.001); + } + } + + private ResultSet createTableWithValues(String[] values, String valueType) throws SQLException { + Connection conn = DriverManager.getConnection(getUrl()); + StringBuilder ddl = new StringBuilder("CREATE TABLE test (id VARCHAR not null primary key"); + StringBuilder dmll = new StringBuilder("UPSERT INTO test(id,"); + StringBuilder dmlr = new StringBuilder(") VALUES ('testid'"); + StringBuilder select = new StringBuilder("SELECT"); + for(int i = 0; i < values.length; i++) { + ddl.append(", num").append(i).append(" ").append(valueType); + dmll.append("num").append(i).append(","); + dmlr.append(", ").append(values[i]); + select.append(" num").append(i).append(","); + } + ddl.append(")"); + dmlr.append(")"); + dmll.deleteCharAt(dmll.length()-1); + select.deleteCharAt(select.length()-1); + select.append(" FROM test"); + conn.createStatement().execute(ddl.toString()); + conn.createStatement().execute(dmll.toString() + dmlr.toString()); + conn.commit(); + + ResultSet rs = conn.createStatement().executeQuery(select.toString()); + rs.next(); + return rs; + } } \ No newline at end of file diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g index 9f604249b09..09c07191ad3 100644 --- a/phoenix-core/src/main/antlr3/PhoenixSQL.g +++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g @@ -967,9 +967,14 @@ NUMBER : POSINTEGER ; -// Exponential format is not supported. DECIMAL - : POSINTEGER? '.' POSINTEGER + : '.' POSINTEGER (Exponent)? + | POSINTEGER '.' Exponent + | POSINTEGER ('.' (POSINTEGER (Exponent)?)? | Exponent) + ; + +Exponent + : ('e' | 'E') ( PLUS | MINUS )? POSINTEGER ; DOUBLE_QUOTE From fce7bed23b4621d04746dcbbcd66a1fbc568d989 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 14 Apr 2015 15:10:59 -0700 Subject: [PATCH 2/3] Created new DOUBLE literal in lexer --- phoenix-core/src/main/antlr3/PhoenixSQL.g | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g index 09c07191ad3..f57c5cc94f6 100644 --- a/phoenix-core/src/main/antlr3/PhoenixSQL.g +++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g @@ -903,6 +903,9 @@ literal returns [LiteralParseNode ret] | d=DECIMAL { ret = factory.realNumber(d.getText()); } + | dbl=DOUBLE { + ret = factory.literal(Double.valueOf(dbl.getText())); + } | NULL {ret = factory.literal(null);} | TRUE {ret = factory.literal(Boolean.TRUE);} | FALSE {ret = factory.literal(Boolean.FALSE);} @@ -968,7 +971,11 @@ NUMBER ; DECIMAL - : '.' POSINTEGER (Exponent)? + : POSINTEGER? '.' POSINTEGER + ; + +DOUBLE + : '.' POSINTEGER Exponent | POSINTEGER '.' Exponent | POSINTEGER ('.' (POSINTEGER (Exponent)?)? | Exponent) ; From f1cd42b0a2556b02d3c2a8804ed1b8576329e569 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 14 Apr 2015 16:17:30 -0700 Subject: [PATCH 3/3] Added test querying SYSTEM.CATALOG --- .../apache/phoenix/end2end/ArithmeticQueryIT.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 71741b85c80..f56c965a685 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 @@ -986,6 +986,20 @@ public void testFloatingPointMultiplicationUpsert() throws Exception { assertEquals(-1.0f, rs.getFloat(1), 0.001); } + @Test + public void testSystemTableHasDoubleForExponentialNumber() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String ddl = "CREATE TABLE test (id VARCHAR not null primary key, num FLOAT)"; + conn.createStatement().execute(ddl); + String dml = "UPSERT INTO test(id,num) VALUES ('testid', 1.2E3)"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs = conn.createStatement().executeQuery("SELECT 1.2E3 FROM SYSTEM.CATALOG LIMIT 1"); + assertTrue(rs.next()); + assertTrue(rs.getObject(1) instanceof Double); + } + @Test public void testFloatingPointWithExponentialNotation() throws Exception { Float[] expected = {1.5E7f, 1.5E-7f, -1.5E-7f, 12E-5f, -.12E+34f};