PHOENIX-3178 Row count incorrect for UPSERT SELECT when auto commit is false


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

Branch: refs/heads/4.x-cdh5.15
Commit: d218505026a35d4575a51bb5ca7a45a0311e19b4
Parents: edd00bd
Author: s.kadam <s.ka...@gus.com>
Authored: Tue Aug 28 23:44:56 2018 +0100
Committer: Pedro Boado <pbo...@apache.org>
Committed: Wed Oct 17 22:49:38 2018 +0100

----------------------------------------------------------------------
 .../end2end/UpsertSelectAutoCommitIT.java       | 31 ++++++++++++++++++--
 .../apache/phoenix/compile/UpsertCompiler.java  | 10 +++++--
 2 files changed, 37 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/d2185050/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertSelectAutoCommitIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertSelectAutoCommitIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertSelectAutoCommitIT.java
index 38d48d6..3966f15 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertSelectAutoCommitIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertSelectAutoCommitIT.java
@@ -151,8 +151,7 @@ public class UpsertSelectAutoCommitIT extends 
ParallelStatsDisabledIT {
         stmt.executeUpdate();
         conn.commit();
     }
-    
-    
+
     @Test
     public void testUpsertSelectDoesntSeeUpsertedData() throws Exception {
         Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
@@ -201,4 +200,32 @@ public class UpsertSelectAutoCommitIT extends 
ParallelStatsDisabledIT {
         connection.close();
     }
 
+    @Test
+    public void testRowCountWithNoAutoCommitOnUpsertSelect() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(QueryServices.MUTATE_BATCH_SIZE_ATTRIB, 
Integer.toString(3));
+        props.setProperty(QueryServices.SCAN_CACHE_SIZE_ATTRIB, 
Integer.toString(3));
+        props.setProperty(QueryServices.SCAN_RESULT_CHUNK_SIZE, 
Integer.toString(3));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(false);
+        String tableName = generateUniqueName();
+
+        conn.createStatement().execute("CREATE SEQUENCE "+ tableName);
+        conn.createStatement().execute(
+                "CREATE TABLE " + tableName + " (pk INTEGER PRIMARY KEY, val 
INTEGER)");
+
+        conn.createStatement().execute(
+                "UPSERT INTO " + tableName + " VALUES (NEXT VALUE FOR 
keys,1)");
+        conn.commit();
+        for (int i=0; i<6; i++) {
+            Statement stmt = conn.createStatement();
+            int upsertCount = stmt.executeUpdate(
+                    "UPSERT INTO " + tableName + " SELECT NEXT VALUE FOR keys, 
val FROM "
+                            + tableName);
+            conn.commit();
+            assertEquals((int)Math.pow(2, i), upsertCount);
+        }
+        conn.close();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/d2185050/phoenix-core/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java
index 9d75bba..d0dd2cf 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java
@@ -185,6 +185,7 @@ public class UpsertCompiler {
                     QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE_BYTES);
         int batchSize = Math.min(connection.getMutateBatchSize(), maxSize);
         boolean isAutoCommit = connection.getAutoCommit();
+        int sizeOffset = 0;
         int numSplColumns =
                 (tableRef.getTable().isMultiTenant() ? 1 : 0)
                         + (tableRef.getTable().getViewIndexId() != null ? 1 : 
0);
@@ -249,8 +250,13 @@ public class UpsertCompiler {
                     mutation.clear();
                 }
             }
-            // If auto commit is true, this last batch will be committed upon 
return
-            return new MutationState(tableRef, mutation, rowCount / batchSize 
* batchSize, maxSize, maxSizeBytes, connection);
+
+            if (isAutoCommit) {
+                // If auto commit is true, this last batch will be committed 
upon return
+                sizeOffset = rowCount / batchSize * batchSize;
+            }
+            return new MutationState(tableRef, mutation, sizeOffset, maxSize,
+                    maxSizeBytes, connection);
         }
     }
 

Reply via email to