PHOENIX-1776 The literal -1.0 (floating point) should not be converted to -1 (Integer) (Dave Hacker)
Conflicts:
phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.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 21af737..3cb4b57 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
@@ -857,4 +857,32 @@
assertTrue(rs.next());
assertEquals(1.333333333, rs.getDouble(1), 0.001);
}
+
+ @Test
+ public void testFloatingPointUpsert() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ String ddl = "CREATE TABLE test (id VARCHAR not null primary key, name VARCHAR, lat FLOAT)";
+ conn.createStatement().execute(ddl);
+ String dml = "UPSERT INTO test(id,name,lat) VALUES ('testid', 'testname', -1.00)";
+ conn.createStatement().execute(dml);
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery("SELECT lat FROM test");
+ assertTrue(rs.next());
+ assertEquals(-1.0f, rs.getFloat(1), 0.001);
+ }
+
+ @Test
+ public void testFloatingPointMultiplicationUpsert() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ String ddl = "CREATE TABLE test (id VARCHAR not null primary key, name VARCHAR, lat FLOAT)";
+ conn.createStatement().execute(ddl);
+ String dml = "UPSERT INTO test(id,name,lat) VALUES ('testid', 'testname', -1.00 * 1)";
+ conn.createStatement().execute(dml);
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery("SELECT lat FROM test");
+ assertTrue(rs.next());
+ assertEquals(-1.0f, rs.getFloat(1), 0.001);
+ }
}
\ No newline at end of file
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
index ac50285..4af0cc9 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
@@ -51,6 +51,7 @@
import org.apache.phoenix.query.DelegateConnectionQueryServices;
import org.apache.phoenix.schema.ColumnRef;
import org.apache.phoenix.schema.MetaDataClient;
+import org.apache.phoenix.schema.PDataType;
import org.apache.phoenix.schema.PDatum;
import org.apache.phoenix.schema.PMetaData;
import org.apache.phoenix.schema.PTable;
@@ -58,8 +59,6 @@
import org.apache.phoenix.schema.PTableType;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.TableRef;
-import org.apache.phoenix.schema.types.PDataType;
-import org.apache.phoenix.schema.types.PVarbinary;
import org.apache.phoenix.util.ByteUtil;
import org.apache.phoenix.util.QueryUtil;
@@ -318,7 +317,7 @@
@Override
public PDataType getDataType() {
- return PVarbinary.INSTANCE;
+ return PDataType.VARBINARY;
}
@Override
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/LiteralParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/LiteralParseNode.java
index f9bbea1..a291b2a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/LiteralParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/LiteralParseNode.java
@@ -37,6 +37,7 @@
public static final ParseNode NULL = new LiteralParseNode(null);
public static final ParseNode ZERO = new LiteralParseNode(0);
public static final ParseNode ONE = new LiteralParseNode(1);
+ public static final ParseNode MINUS_ONE = new LiteralParseNode(-1L);
private final Object value;
private final PDataType type;
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
index 9ab31b1..7d14e58 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
@@ -542,8 +542,15 @@
return new ArrayConstructorNode(upsertStmtArray);
}
- public MultiplyParseNode negate(ParseNode child) {
- return new MultiplyParseNode(Arrays.asList(child,this.literal(-1)));
+
+ public ParseNode negate(ParseNode child) {
+ // Prevents reparsing of -1 from becoming 1*-1 and 1*1*-1 with each re-parsing
+ if (LiteralParseNode.ONE.equals(child) && ((LiteralParseNode)child).getType().isCoercibleTo(
+ PDataType.LONG)) {
+ return LiteralParseNode.MINUS_ONE;
+ }
+ return new MultiplyParseNode(Arrays.asList(child,LiteralParseNode.MINUS_ONE));
+
}
public NotEqualParseNode notEqual(ParseNode lhs, ParseNode rhs) {