phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestay...@apache.org
Subject [1/2] phoenix git commit: PHOENIX-2934 Checking a coerce expression at top level should not be necessary for Union All query (Alicia Ying Shu)
Date Sun, 12 Jun 2016 23:07:52 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master 951dd7a78 -> 1af51ed81


PHOENIX-2934 Checking a coerce expression at top level should not be necessary for Union All
query (Alicia Ying Shu)


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

Branch: refs/heads/master
Commit: 1af51ed81414d4f1bbea9e0fe6a5dd2fb017edf7
Parents: d538938
Author: James Taylor <jamestaylor@apache.org>
Authored: Sun Jun 12 16:03:25 2016 -0700
Committer: James Taylor <jamestaylor@apache.org>
Committed: Sun Jun 12 16:10:17 2016 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/UnionAllIT.java  | 85 ++++++++++++++++++++
 .../org/apache/phoenix/schema/types/PChar.java  |  4 +
 .../phoenix/compile/QueryCompilerTest.java      | 33 ++++++++
 3 files changed, 122 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/1af51ed8/phoenix-core/src/it/java/org/apache/phoenix/end2end/UnionAllIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UnionAllIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UnionAllIT.java
index 8b60c3f..57ba858 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UnionAllIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UnionAllIT.java
@@ -952,4 +952,89 @@ public class UnionAllIT extends BaseOwnClusterHBaseManagedTimeIT {
             conn.close();
         }
     }
+
+    @Test
+    public void testCoerceExpr() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+
+        String ddl = "create table user ( id integer not null primary key desc, " +
+                "firstname char(8), lastname varchar, sales double)";
+        createTestTable(getUrl(), ddl);
+        String dml = "upsert into user values (?, ?, ?, ?)";
+        PreparedStatement  stmt = conn.prepareStatement(dml);
+        stmt.setInt(1, 1);
+        stmt.setString(2, "sam");
+        stmt.setString(3, "johnson");
+        stmt.setDouble(4, 100.6798);
+        stmt.execute();
+        stmt.setInt(1, 2);
+        stmt.setString(2, "ann");
+        stmt.setString(3, "wiely");
+        stmt.setDouble(4, 10.67);
+        stmt.execute();
+        conn.commit();
+
+        ddl = "create table person (id bigint not null primary key, " +
+                "firstname char(10), lastname varchar(10), sales decimal)";
+        createTestTable(getUrl(), ddl);
+        dml = "upsert into person values (?, ?, ?, ?)";
+        stmt = conn.prepareStatement(dml);
+        stmt.setInt(1, 1);
+        stmt.setString(2, "john");
+        stmt.setString(3, "doe");
+        stmt.setBigDecimal(4, BigDecimal.valueOf(467.894745));
+        stmt.execute();
+        stmt.setInt(1, 2);
+        stmt.setString(2, "jane");
+        stmt.setString(3, "doe");
+        stmt.setBigDecimal(4, BigDecimal.valueOf(88.89474501));
+        stmt.execute();
+        conn.commit();
+
+        String query = "select id, cast('foo' as char(10)) firstname, lastname, sales " +
+                "from person union all select * from user";
+        try {
+            PreparedStatement pstmt = conn.prepareStatement(query);
+            ResultSet rs = pstmt.executeQuery();
+            assertTrue(rs.next());
+            assertEquals(1, rs.getInt(1));
+            assertEquals("foo", rs.getString(2).trim());
+            assertEquals("doe", rs.getString(3).trim());
+            assertEquals(BigDecimal.valueOf(467.894745), rs.getBigDecimal(4));
+            assertTrue(rs.next());
+            assertEquals(2, rs.getInt(1));
+            assertEquals("foo", rs.getString(2).trim());
+            assertEquals("doe", rs.getString(3).trim());
+            assertEquals(BigDecimal.valueOf(88.89474501), rs.getBigDecimal(4));
+            assertTrue(rs.next());
+            assertEquals(2, rs.getInt(1));
+            assertEquals("ann", rs.getString(2).trim());
+            assertEquals("wiely", rs.getString(3).trim());
+            assertEquals(BigDecimal.valueOf(10.67), rs.getBigDecimal(4));
+            assertTrue(rs.next());
+            assertEquals(1, rs.getInt(1));
+            assertEquals("sam", rs.getString(2).trim());
+            assertEquals("johnson", rs.getString(3).trim());
+            assertEquals(BigDecimal.valueOf(100.6798), rs.getBigDecimal(4));
+            assertFalse(rs.next());
+
+            query = "select id, cast('foo' as char(10)) firstname, lastname, sales from person";
+            pstmt = conn.prepareStatement(query);
+            rs = pstmt.executeQuery();
+            assertTrue(rs.next());
+            assertEquals(1, rs.getInt(1));
+            assertEquals("foo", rs.getString(2).trim());
+            assertEquals("doe", rs.getString(3));
+            assertEquals(BigDecimal.valueOf(467.894745), rs.getBigDecimal(4));
+            assertTrue(rs.next());
+            assertEquals(2, rs.getInt(1));
+            assertEquals("foo", rs.getString(2).trim());
+            assertEquals("doe", rs.getString(3));
+            assertEquals(BigDecimal.valueOf(88.89474501), rs.getBigDecimal(4));
+            assertFalse(rs.next());
+        } finally {
+            conn.close();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1af51ed8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
index 4170253..2853bc4 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
@@ -145,6 +145,10 @@ public class PChar extends PDataType<String> {
         throw newIllegalDataException("CHAR types may only contain single byte characters
(" + o + ")");
       }
       super.coerceBytes(ptr, o, actualType, actualMaxLength, actualScale, actualModifier,
desiredMaxLength, desiredScale, expectedModifier);
+      if (ptr.getLength() > 0 && desiredMaxLength != null &&
+              desiredMaxLength > ptr.getLength()) {
+          pad(ptr, desiredMaxLength, expectedModifier);
+      }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1af51ed8/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
index e67a0e3..133eb37 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
@@ -48,8 +48,10 @@ import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.compile.OrderByCompiler.OrderBy;
 import org.apache.phoenix.coprocessor.BaseScannerRegionObserver;
 import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.expression.CoerceExpression;
 import org.apache.phoenix.expression.Expression;
 import org.apache.phoenix.expression.LiteralExpression;
+import org.apache.phoenix.expression.ProjectedColumnExpression;
 import org.apache.phoenix.expression.aggregator.Aggregator;
 import org.apache.phoenix.expression.aggregator.CountAggregator;
 import org.apache.phoenix.expression.aggregator.ServerAggregators;
@@ -67,6 +69,10 @@ import org.apache.phoenix.schema.ColumnAlreadyExistsException;
 import org.apache.phoenix.schema.ColumnNotFoundException;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PTableKey;
+import org.apache.phoenix.schema.types.PChar;
+import org.apache.phoenix.schema.types.PDecimal;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PVarchar;
 import org.apache.phoenix.util.ByteUtil;
 import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.PropertiesUtil;
@@ -2315,4 +2321,31 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest
{
             conn.close();
         }
     }
+
+    @Test
+    public void testColumnProjectionUnionAll() throws SQLException {
+        Connection conn = DriverManager.getConnection(getUrl());
+        try {
+            conn.createStatement().execute("CREATE TABLE t1(k INTEGER PRIMARY KEY,"+
+                    " col1 CHAR(8), col2 VARCHAR(10), col3 decimal(10,2))");
+            conn.createStatement().execute("CREATE TABLE t2(k TINYINT PRIMARY KEY," +
+                    " col1 CHAR(20), col2 CHAR(30), col3 double)");
+            QueryPlan plan = getQueryPlan("SELECT * from t1 union all select * from t2",
+                Collections.emptyList());
+            RowProjector rowProj = plan.getProjector();
+            assertTrue(rowProj.getColumnProjector(0).getExpression().getDataType()
+                instanceof PInteger);
+            assertTrue(rowProj.getColumnProjector(1).getExpression().getDataType()
+                instanceof PChar);
+            assertTrue(rowProj.getColumnProjector(1).getExpression().getMaxLength() == 20);
+            assertTrue(rowProj.getColumnProjector(2).getExpression().getDataType()
+                instanceof PVarchar);
+            assertTrue(rowProj.getColumnProjector(2).getExpression().getMaxLength() == 30);
+            assertTrue(rowProj.getColumnProjector(3).getExpression().getDataType()
+                instanceof PDecimal);
+            assertTrue(rowProj.getColumnProjector(3).getExpression().getScale() == 2);
+        } finally {
+            conn.close();
+        }
+    }
 }


Mime
View raw message