Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -52,9 +52,12 @@
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
Expand DownExpand Up@@ -86,6 +89,8 @@ public class ParameterizedIndexUpgradeToolIT extends BaseTest {
private static Map<String, String> serverProps = Maps.newHashMapWithExpectedSize(1),
clientProps = Maps.newHashMapWithExpectedSize(1);

public static final String VERIFY_COUNT_ASSERT_MESSAGE = "view-index count in system table doesn't match";

private final boolean mutable;
private final boolean upgrade;
private final boolean isNamespaceEnabled;
Expand DownExpand Up@@ -168,7 +173,7 @@ private void prepareSetup() throws SQLException {
conn.createStatement().execute(createTblStr);
conn.createStatement().execute("CREATE TABLE TRANSACTIONAL_TABLE(id bigint NOT NULL "
+ "PRIMARY KEY, a.name varchar, sal bigint, address varchar) "
+ " TRANSACTIONAL=true "//", TRANSACTION_PROVIDER='TEPHRA' "
+ " TRANSACTIONAL=true "
+ ((tableDDLOptions.trim().length() > 0) ? "," : "") + tableDDLOptions);

//views
Expand DownExpand Up@@ -346,6 +351,34 @@ public void testDryRunAndFailures() throws Exception {
}
}

@Test
public void verifyViewAndViewIndexes() throws SQLException {
Comment thread
swaroopak marked this conversation as resolved.
String tableName = "MOCK1";
String schemaName = "TEST";
String viewQuery = iut.getViewSql(tableName, schemaName);
ResultSet rs = conn.createStatement().executeQuery(viewQuery);
int countViews = 0;
List<String> views = new ArrayList<>();
List<Integer> indexCount = new ArrayList<>();
while (rs.next()) {
views.add(rs.getString(1));
countViews++;
}
Assert.assertEquals("view count in system table doesn't match", 2, countViews);
for (int i=0; i < views.size(); i++) {
String viewName = SchemaUtil.getTableNameFromFullName(views.get(i));
String viewIndexQuery = iut.getViewIndexesSql(viewName, schemaName, null);
rs = conn.createStatement().executeQuery(viewIndexQuery);
int indexes = 0;
while (rs.next()) {
indexes++;
}
indexCount.add(indexes);
}
Assert.assertEquals(VERIFY_COUNT_ASSERT_MESSAGE, 1, (int) indexCount.get(0));
Assert.assertEquals(VERIFY_COUNT_ASSERT_MESSAGE, 2, (int) indexCount.get(1));
}

@After
public void cleanup() throws IOException, SQLException {
//TEST.MOCK1,TEST1.MOCK2,TEST.MOCK3
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -648,17 +648,12 @@ private HashMap<String, IndexInfo> prepareToRebuildIndexes(Connection conn,
}

if (hasViewIndex) {
String viewSql = "SELECT DISTINCT TABLE_NAME, TENANT_ID FROM "
+ "SYSTEM.CATALOG "
+ "WHERE COLUMN_FAMILY = \'" + dataTableFullName + "\' "
+ (!StringUtil.EMPTY_STRING.equals(schemaName) ? "AND TABLE_SCHEM = \'"
+ schemaName + "\' " : "")
+ "AND LINK_TYPE = "
+ PTable.LinkType.PHYSICAL_TABLE.getSerializedValue();
String viewSql = getViewSql(tableName, schemaName);

ResultSet rs = conn.createStatement().executeQuery(viewSql);
while (rs.next()) {
String viewName = rs.getString(1);
String viewFullName = rs.getString(1);
Comment thread
swaroopak marked this conversation as resolved.
String viewName = SchemaUtil.getTableNameFromFullName(viewFullName);
String tenantId = rs.getString(2);
ArrayList<String> viewIndexes = findViewIndexes(conn, schemaName, viewName,
tenantId);
Expand All@@ -673,16 +668,21 @@ private HashMap<String, IndexInfo> prepareToRebuildIndexes(Connection conn,
return indexInfos;
}

@VisibleForTesting
public String getViewSql(String tableName, String schemaName) {
return "SELECT DISTINCT COLUMN_FAMILY, TENANT_ID FROM "
+ "SYSTEM.CHILD_LINK "
+ "WHERE TABLE_NAME = \'" + tableName + "\'"
+ (!Strings.isNullOrEmpty(schemaName) ? " AND TABLE_SCHEM = \'"
+ schemaName + "\'" : "")
+ " AND LINK_TYPE = "
+ PTable.LinkType.CHILD_TABLE.getSerializedValue();
}

private ArrayList<String> findViewIndexes(Connection conn, String schemaName, String viewName,
String tenantId) throws SQLException {

String viewIndexesSql = "SELECT DISTINCT COLUMN_FAMILY FROM "
+ "SYSTEM.CATALOG "
+ "WHERE TABLE_NAME = \'" + viewName + "\'"
+ (!StringUtil.EMPTY_STRING.equals(schemaName) ? "AND TABLE_SCHEM = \'"
+ schemaName + "\' " : "")
+ "AND LINK_TYPE = " + PTable.LinkType.INDEX_TABLE.getSerializedValue()
+ (tenantId != null ? " AND TENANT_ID = \'" + tenantId + "\'" : "");
String viewIndexesSql = getViewIndexesSql(viewName, schemaName, tenantId);
ArrayList<String> viewIndexes = new ArrayList<>();
ResultSet rs = conn.createStatement().executeQuery(viewIndexesSql);
while (rs.next()) {
Expand All@@ -692,6 +692,17 @@ private ArrayList<String> findViewIndexes(Connection conn, String schemaName, St
return viewIndexes;
}

@VisibleForTesting
public String getViewIndexesSql(String viewName, String schemaName, String tenantId) {
return "SELECT DISTINCT COLUMN_FAMILY FROM "
+ "SYSTEM.CATALOG "
+ "WHERE TABLE_NAME = \'" + viewName + "\'"
+ (!Strings.isNullOrEmpty(schemaName) ? " AND TABLE_SCHEM = \'"
+ schemaName + "\'" : "")
+ " AND LINK_TYPE = " + PTable.LinkType.INDEX_TABLE.getSerializedValue()
+ (tenantId != null ? " AND TENANT_ID = \'" + tenantId + "\'" : "");
}

private class IndexInfo {
final private String schemaName;
final private String baseTable;
Expand Down