Repository: phoenix
Updated Branches:
  refs/heads/master 6dcc88262 -> 3c2997cda


PHOENIX-2205 Group by a divided value (e.g., time/10) returns NULL


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

Branch: refs/heads/master
Commit: 3c2997cda557cb9b6cb3bd36c67ed0b4a3f89f0e
Parents: 6dcc882
Author: Dumindu Buddhika <dumindukarunathil...@gmail.com>
Authored: Mon Sep 21 16:37:50 2015 +0530
Committer: Dumindu Buddhika <dumindukarunathil...@gmail.com>
Committed: Mon Sep 21 16:37:50 2015 +0530

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/GroupByIT.java   | 72 ++++++++++++++++++++
 .../expression/RowKeyColumnExpression.java      |  2 +-
 2 files changed, 73 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/3c2997cd/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
index 31a55a0..51ab070 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
@@ -574,4 +574,76 @@ public class GroupByIT extends BaseQueryIT {
         conn.close();
     }
 
+
+    @Test
+    public void testGroupByWithIntegerDivision1() throws Exception {
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table test1(time integer not null, hostname 
varchar not null,usage float,period integer constraint pk PRIMARY KEY(time, 
hostname))";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into test1 
values(1439853462,'qa9',8.27,1439853462)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853461,'qa9',8.27,1439853362)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853461,'qa9',5.27,1439853461)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853451,'qa9',4.27,1439853451)");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select time/10 as tm, hostname, 
avg(usage) from test1 group by hostname, tm");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+        assertEquals(143985345, rs.getInt(1));
+        assertEquals(4.2699, rs.getDouble(3), 0.1);
+        assertTrue(rs.next());
+        assertEquals(143985346, rs.getInt(1));
+        assertEquals(6.77, rs.getDouble(3), 0.1);
+    }
+
+    @Test
+    public void testGroupByWithIntegerDivision2() throws Exception {
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table test1(time integer not null, hostname 
varchar not null,usage float,period integer constraint pk PRIMARY KEY(time, 
hostname))";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into test1 
values(1439853462,'qa9',8.27,1439853462)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853461,'qa9',8.27,1439853362)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853461,'qa9',5.27,1439853461)");
+        stmt.execute();
+        stmt = conn.prepareStatement("upsert into test1 
values(1439853451,'qa9',4.27,1439853451)");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts 
+ 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select period/10 as tm, hostname, 
avg(usage) from test1 group by hostname, tm");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+        assertEquals(143985345, rs.getInt(1));
+        assertEquals(4.2699, rs.getDouble(3), 0.1);
+        assertTrue(rs.next());
+        assertEquals(143985346, rs.getInt(1));
+        assertEquals(6.77, rs.getDouble(3), 0.1);
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3c2997cd/phoenix-core/src/main/java/org/apache/phoenix/expression/RowKeyColumnExpression.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/RowKeyColumnExpression.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/RowKeyColumnExpression.java
index e4ec438..03cc51f 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/RowKeyColumnExpression.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/RowKeyColumnExpression.java
@@ -118,7 +118,7 @@ public class RowKeyColumnExpression  extends 
ColumnExpression {
             // FIXME: fixedByteSize <= maxByteSize ? fixedByteSize : 0 
required because HBase passes bogus keys to filter to position scan (HBASE-6562)
             if (fromType.isFixedWidth()) {
                 Integer maxLength = getMaxLength();
-                byteSize = maxLength == null ? fromType.getByteSize() : 
maxLength;
+                byteSize = fromType.getByteSize() == null ? maxLength : 
fromType.getByteSize();
                 byteSize = byteSize <= maxOffset ? byteSize : 0;
             }
             int length = byteSize >= 0 ? byteSize  : 
accessor.getLength(buffer, offset, maxOffset);

Reply via email to