PHOENIX-1814 Exponential notation parsing and tests
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/d1474231 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/d1474231 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/d1474231 Branch: refs/heads/calcite Commit: d147423137af487c0582234c12cc00b0e8f6be98 Parents: 007361b Author: Brian <besserl...@salesforce.com> Authored: Tue Apr 14 13:49:24 2015 -0700 Committer: Thomas D'Silva <twdsi...@gmail.com> Committed: Wed Apr 15 11:09:12 2015 -0700 ---------------------------------------------------------------------- .../phoenix/end2end/ArithmeticQueryIT.java | 60 ++++++++++++++++++++ phoenix-core/src/main/antlr3/PhoenixSQL.g | 16 +++++- 2 files changed, 74 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/d1474231/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 72eb016..f56c965 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,64 @@ public class ArithmeticQueryIT extends BaseHBaseManagedTimeIT { assertTrue(rs.next()); 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}; + 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 http://git-wip-us.apache.org/repos/asf/phoenix/blob/d1474231/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 9f60424..f57c5cc 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);} @@ -967,9 +970,18 @@ NUMBER : POSINTEGER ; -// Exponential format is not supported. DECIMAL - : POSINTEGER? '.' POSINTEGER + : POSINTEGER? '.' POSINTEGER + ; + +DOUBLE + : '.' POSINTEGER Exponent + | POSINTEGER '.' Exponent + | POSINTEGER ('.' (POSINTEGER (Exponent)?)? | Exponent) + ; + +Exponent + : ('e' | 'E') ( PLUS | MINUS )? POSINTEGER ; DOUBLE_QUOTE