Repository: phoenix
Updated Branches:
  refs/heads/master 2e9677d35 -> 1040fd05c


PHOENIX-1258 Fix RegexpSubstrFunction serialization

Correct deserialization of RegexpSubstrFunction so that it can
be used in filters and group-by statements.

Contributed by Jay Wong.


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

Branch: refs/heads/master
Commit: 1040fd05ca317035c1a4bb3e123110b1a8f678e8
Parents: 2e9677d
Author: Gabriel Reid <gabri...@ngdata.com>
Authored: Sat Sep 20 20:23:49 2014 +0200
Committer: Gabriel Reid <gabri...@ngdata.com>
Committed: Sat Sep 20 20:29:53 2014 +0200

----------------------------------------------------------------------
 .../phoenix/end2end/RegexpSubstrFunctionIT.java | 90 ++++++++++++++++++++
 .../function/RegexpSubstrFunction.java          |  8 ++
 2 files changed, 98 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/1040fd05/phoenix-core/src/it/java/org/apache/phoenix/end2end/RegexpSubstrFunctionIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/RegexpSubstrFunctionIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/RegexpSubstrFunctionIT.java
new file mode 100644
index 0000000..1121207
--- /dev/null
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/RegexpSubstrFunctionIT.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.end2end;
+
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.sql.*;
+import java.util.Properties;
+
+import static org.apache.phoenix.util.TestUtil.GROUPBYTEST_NAME;
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.*;
+
+@Category(HBaseManagedTimeTest.class)
+public class RegexpSubstrFunctionIT extends BaseHBaseManagedTimeIT {
+
+    private int id;
+
+    @Before
+    public void doBeforeTestSetup() throws Exception {
+        ensureTableCreated(getUrl(), GROUPBYTEST_NAME);
+        Connection conn = DriverManager.getConnection(getUrl());
+        insertRow(conn, "Report1?1", 10);
+        insertRow(conn, "Report1?2", 10);
+        insertRow(conn, "Report2?1", 30);
+        insertRow(conn, "Report3?2", 30);
+        conn.commit();
+        conn.close();
+    }
+
+    private void insertRow(Connection conn, String uri, int appcpu) throws 
SQLException {
+        PreparedStatement statement = conn.prepareStatement("UPSERT INTO " + 
GROUPBYTEST_NAME + "(id, uri, appcpu) values (?,?,?)");
+        statement.setString(1, "id" + id);
+        statement.setString(2, uri);
+        statement.setInt(3, appcpu);
+        statement.executeUpdate();
+        id++;
+    }
+
+    @Test
+    public void testGroupByScanWithRegexpSubstr() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        Statement stmt = conn.createStatement();
+        ResultSet rs = stmt.executeQuery("select REGEXP_SUBSTR(uri, 
'[^\\\\?]+') suburi, sum(appcpu) sumcpu from " + GROUPBYTEST_NAME
+            + " group by suburi");
+        assertTrue(rs.next());
+        assertEquals(rs.getString("suburi"), "Report1");
+        assertEquals(rs.getInt("sumcpu"), 20);
+        assertTrue(rs.next());
+        assertEquals(rs.getString("suburi"), "Report2");
+        assertEquals(rs.getInt("sumcpu"), 30);
+        assertTrue(rs.next());
+        assertEquals(rs.getString("suburi"), "Report3");
+        assertEquals(rs.getInt("sumcpu"), 30);
+        assertFalse(rs.next());
+        conn.close();
+    }
+
+    @Test
+    public void testFilterWithRegexSubstr() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        ResultSet rs = conn.createStatement().executeQuery(
+                "select id from " + GROUPBYTEST_NAME + " where 
REGEXP_SUBSTR(uri, '[^\\\\?]+') = 'Report1'");
+        assertTrue(rs.next());
+        assertEquals("id0", rs.getString(1));
+        assertTrue(rs.next());
+        assertEquals("id1", rs.getString(1));
+        assertFalse(rs.next());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1040fd05/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RegexpSubstrFunction.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RegexpSubstrFunction.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RegexpSubstrFunction.java
index b75c2dc..840e3d7 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RegexpSubstrFunction.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RegexpSubstrFunction.java
@@ -17,6 +17,8 @@
  */
 package org.apache.phoenix.expression.function;
 
+import java.io.DataInput;
+import java.io.IOException;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -147,6 +149,12 @@ public class RegexpSubstrFunction extends PrefixFunction {
     }
 
     @Override
+    public void readFields(DataInput input) throws IOException {
+        super.readFields(input);
+        init();
+    }
+
+    @Override
     public int getKeyFormationTraversalIndex() {
         return preservesOrder() == OrderPreserving.NO ? NO_TRAVERSAL : 0;
     }

Reply via email to