tkhurana commented on code in PR #2656:
URL: https://github.com/apache/phoenix/pull/2656#discussion_r4485458142


##########
phoenix-core-server/src/main/java/org/apache/phoenix/index/GlobalIndexChecker.java:
##########
@@ -237,6 +254,63 @@ private boolean shouldCreateUnverifiedRowFilter(Filter 
delegateFilter) {
       return delegateFilter != null && !indexMaintainer.isUncovered();
     }
 
+    /**
+     * Builds a new filter chain for a repair scan of one row. A filter object 
that two scanners
+     * share gets state from both scanners. For example, a skip scan filter 
can then issue a seek
+     * hint behind its previous hint. The new chain has the same structure as 
the filter chain of
+     * the main scan.
+     */
+    private Filter newSingleRowIndexScanFilter() throws IOException {
+      if (clientFilter != null && clientFilterProto == null) {
+        // The serialized form holds only the filter configuration, not the 
scan position. Thus
+        // one serialized form serves all repair scans. Repair is rare, so one 
parse for each
+        // repair scan is acceptable.
+        clientFilterProto = ProtobufUtil.toFilter(clientFilter);
+      }
+      Filter filter = null;
+      if (clientFilterProto != null) {
+        // The serialized form does not hold the scan direction. The shared 
distinct prefix filter
+        // has the direction of the main scan, and a filter list needs one 
direction for all of
+        // its filters.
+        filter = ProtobufUtil.toFilter(clientFilterProto);
+        filter.setReversed(clientFilter.isReversed());

Review Comment:
   The direction fixes look right to me. Could we add a reverse-scan 
read-repair test, e.g. `SELECT DISTINCT val1 ... ORDER BY val1 DESC` with an 
unverified row? The two direction fixes aren't covered by a test yet.



##########
phoenix-core-server/src/main/java/org/apache/phoenix/index/GlobalIndexChecker.java:
##########
@@ -505,6 +579,7 @@ private void repairIndexRows(byte[] indexRowKey, long ts, 
List<Cell> row) throws
         singleRowIndexScan.withStartRow(indexRowKey, true);
         singleRowIndexScan.withStopRow(indexRowKey, true);
         singleRowIndexScan.setTimeRange(minTimestamp, ts);
+        singleRowIndexScan.setFilter(newSingleRowIndexScanFilter());

Review Comment:
   Nit: the comment at the `row.isEmpty()` check just below ("This can happen 
if the unverified row matches the filter but after repair the verified row 
doesn't match the filter") could mention that the shared distinct prefix filter 
can also make the result empty.



##########
phoenix-core-server/src/main/java/org/apache/phoenix/index/GlobalIndexChecker.java:
##########
@@ -237,6 +254,63 @@ private boolean shouldCreateUnverifiedRowFilter(Filter 
delegateFilter) {
       return delegateFilter != null && !indexMaintainer.isUncovered();
     }
 
+    /**
+     * Builds a new filter chain for a repair scan of one row. A filter object 
that two scanners
+     * share gets state from both scanners. For example, a skip scan filter 
can then issue a seek
+     * hint behind its previous hint. The new chain has the same structure as 
the filter chain of
+     * the main scan.
+     */
+    private Filter newSingleRowIndexScanFilter() throws IOException {
+      if (clientFilter != null && clientFilterProto == null) {
+        // The serialized form holds only the filter configuration, not the 
scan position. Thus
+        // one serialized form serves all repair scans. Repair is rare, so one 
parse for each
+        // repair scan is acceptable.
+        clientFilterProto = ProtobufUtil.toFilter(clientFilter);
+      }
+      Filter filter = null;
+      if (clientFilterProto != null) {
+        // The serialized form does not hold the scan direction. The shared 
distinct prefix filter
+        // has the direction of the main scan, and a filter list needs one 
direction for all of
+        // its filters.
+        filter = ProtobufUtil.toFilter(clientFilterProto);
+        filter.setReversed(clientFilter.isReversed());
+        filter = shareDistinctPrefixFilter(filter);
+        if (shouldCreateUnverifiedRowFilter(filter)) {
+          filter = new UnverifiedRowFilter(filter, emptyCF, emptyCQ);
+        }
+      }
+      if (hasPagingFilter) {

Review Comment:
   Nit: the single-row scan reads exactly one row, so I don't think it needs a 
`PagingFilter`. `PagingRegionScanner` already drops paging for point lookups. 
Removing it would drop `hasPagingFilter` too.



##########
phoenix-core/src/it/java/org/apache/phoenix/end2end/index/GlobalIndexCheckerIT.java:
##########
@@ -1419,6 +1419,141 @@ public void testUnverifiedIndexRowWithSkipScanFilter() 
throws Exception {
     }
   }
 
+  @Test
+  public void testUnverifiedIndexRowRepairWithSkipScanHint() throws Exception {
+    if (async) {
+      // No need to run the same test twice one for async = true and the other 
for async = false
+      return;
+    }
+    try (Connection conn = DriverManager.getConnection(getUrl())) {
+      String dataTableName = generateUniqueName();
+      String indexName = generateUniqueName();
+      conn.createStatement()
+        .execute("create table " + dataTableName
+          + " (id varchar(10) not null primary key, val1 varchar(10), val2 
varchar(10))"
+          + tableDDLOptions);
+      // The index covers no column, so each index row holds only the empty 
column. The scanner
+      // sees the empty column first, so UnverifiedRowFilter decides on a row 
before the skip scan
+      // filter sees the row.
+      conn.createStatement().execute(
+        "CREATE INDEX " + indexName + " on " + dataTableName + " (val1)" + 
this.indexDDLOptions);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k1', 'a', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('v', 'c', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k2', 'd', 'x')");
+      conn.commit();
+      // Fail phase 2. The index row of 'u' keeps its key and gets a newer 
unverified version
+      // over the older verified version.
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(true);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'y')");
+      commitWithException(conn);
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(false);
+
+      // The index rows of 'u' and 'v' sort between the two skip scan keys. 
The read repair of
+      // 'u' must not change the skip scan state of the main scanner.
+      String selectSql = "SELECT id, val1 from " + dataTableName + " WHERE 
val1 IN ('a', 'd')";
+      assertPlan(conn, selectSql).scanType("SKIP SCAN ON 2 
KEYS").table(indexName);
+      List<String> actualIDs = Lists.newArrayList();
+      try (ResultSet rs = conn.createStatement().executeQuery(selectSql)) {
+        while (rs.next()) {
+          actualIDs.add(rs.getString("id"));
+        }
+      }
+      assertEquals(Lists.newArrayList("k1", "k2"), actualIDs);
+    }
+  }
+
+  @Test
+  public void testUnverifiedIndexRowRepairWithDistinctPrefixLimit() throws 
Exception {
+    // The repair scan must share the distinct prefix state of the main scan. 
A private copy of
+    // the distinct prefix filter makes this query return too few rows.
+    if (async) {
+      // No need to run the same test twice one for async = true and the other 
for async = false
+      return;
+    }
+    try (Connection conn = DriverManager.getConnection(getUrl())) {
+      String dataTableName = generateUniqueName();
+      String indexName = generateUniqueName();
+      conn.createStatement()
+        .execute("create table " + dataTableName
+          + " (id varchar(10) not null primary key, val1 varchar(10), val2 
varchar(10))"
+          + tableDDLOptions);
+      conn.createStatement().execute(
+        "CREATE INDEX " + indexName + " on " + dataTableName + " (val1)" + 
this.indexDDLOptions);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k1', 'a', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('w', 'b', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k2', 'c', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k3', 'd', 'x')");
+      conn.commit();
+      // Fail phase 2. The index row of 'u' keeps its key and gets a newer 
unverified version
+      // over the older verified version.
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(true);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'y')");
+      commitWithException(conn);
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(false);
+
+      // Read repair returns the older version of 'u'. The distinct prefix 
filter of the main
+      // scanner must then skip 'w', because the server row limit counts each 
returned row.
+      String selectSql = "SELECT DISTINCT val1 from " + dataTableName + " 
LIMIT 3";
+      ExplainPlanAttributes attributes = getExplainAttributes(conn, selectSql);
+      assertPlan(attributes).table(indexName);
+      assertNotNull(attributes.getServerDistinctFilter());
+      List<String> actualValues = Lists.newArrayList();
+      try (ResultSet rs = conn.createStatement().executeQuery(selectSql)) {
+        while (rs.next()) {
+          actualValues.add(rs.getString(1));
+        }
+      }
+      assertEquals(Lists.newArrayList("a", "b", "c"), actualValues);
+    }
+  }
+
+  @Test
+  public void testUnverifiedIndexRowRepairWithSkipScanAndDistinctPrefix() 
throws Exception {
+    if (async) {
+      // No need to run the same test twice one for async = true and the other 
for async = false
+      return;
+    }
+    try (Connection conn = DriverManager.getConnection(getUrl())) {
+      String dataTableName = generateUniqueName();
+      String indexName = generateUniqueName();
+      conn.createStatement()
+        .execute("create table " + dataTableName
+          + " (id varchar(10) not null primary key, val1 varchar(10), val2 
varchar(10))"
+          + tableDDLOptions);
+      conn.createStatement().execute(
+        "CREATE INDEX " + indexName + " on " + dataTableName + " (val1)" + 
this.indexDDLOptions);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k1', 'a', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k2', 'a', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('v', 'c', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k3', 'd', 'x')");
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('k4', 'd', 'x')");
+      conn.commit();
+      // Fail phase 2. The index row of 'u' keeps its key and gets a newer 
unverified version
+      // over the older verified version.
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(true);
+      conn.createStatement().execute("upsert into " + dataTableName + " values 
('u', 'b', 'y')");
+      commitWithException(conn);
+      IndexRegionObserver.setFailDataTableUpdatesForTesting(false);
+
+      // The filter chain has a skip scan filter and a distinct prefix filter. 
The repair scan
+      // of 'u' gets a new skip scan filter and shares the distinct prefix 
filter.
+      String selectSql = "SELECT DISTINCT val1 from " + dataTableName + " 
WHERE val1 IN ('a', 'd')";

Review Comment:
   Here the skip scan rejects the repaired row 'b', so this test doesn't check 
that the distinct prefix filter is shared. A case where the repaired row 
matches a skip key and shares a prefix with the next row, plus a LIMIT, would 
cover it. For example rows (a), (d unverified), (d), (e) with `IN ('a','d','e') 
LIMIT 3`, expecting [a, d, e].



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to