Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/sqlancer/clickhouse/ClickHouseFailureDiagnostics.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static void logTableState(ClickHouseGlobalState state, String tableName)
+ " WHERE database = currentDatabase() AND table = " + literal + " ORDER BY mutation_id");
}

public static void logRowPolicies(ClickHouseGlobalState state, String tableName) {
logRows(state, "row policies of " + tableName,
"SELECT short_name, select_filter, is_restrictive, apply_to_all, apply_to_list FROM system.row_policies"
+ " WHERE database = currentDatabase() AND table = " + asStringLiteral(tableName)
+ " ORDER BY short_name");
}

private static void logRows(ClickHouseGlobalState state, String label, String query) {
StringBuilder sb = new StringBuilder();
sb.append("-- diagnostics: ").append(label);
Expand Down Expand Up @@ -80,7 +87,7 @@ private static String asIdentifier(String name) {
return "`" + name.replace("`", "``") + "`";
}

private static String asStringLiteral(String name) {
public static String asStringLiteral(String name) {
return "'" + name.replace("\\", "\\\\").replace("'", "\\'") + "'";
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package sqlancer.clickhouse.oracle.rowpolicy;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import sqlancer.ComparatorHelper;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.clickhouse.ClickHouseErrors;
import sqlancer.clickhouse.ClickHouseFailureDiagnostics;
import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState;
import sqlancer.clickhouse.ClickHouseSchema;
import sqlancer.clickhouse.ClickHouseSchema.ClickHouseTable;
Expand Down Expand Up @@ -56,6 +60,20 @@ public void check() throws SQLException {
throw new IgnoreMeException();
}

String fqTable = state.getDatabaseName() + "." + table.getName();
List<String> existingPolicies = rowPoliciesOn(table.getName());
if (!existingPolicies.isEmpty()) {
for (String name : existingPolicies) {
try {
new SQLQueryAdapter("DROP ROW POLICY IF EXISTS " + name + " ON " + fqTable, errors, false)
.execute(state);
} catch (SQLException ignored) {

}
}
throw new IgnoreMeException();
}

ClickHouseExpressionGenerator gen = new ClickHouseExpressionGenerator(state).allowAggregates(false);
gen.addColumns(columns);
ClickHouseExpression predicate = gen.generatePredicate();
Expand All @@ -75,7 +93,6 @@ public void check() throws SQLException {
}

String policyName = "rp_" + table.getName() + "_" + POLICY_COUNTER.incrementAndGet();
String fqTable = state.getDatabaseName() + "." + table.getName();
String predicateSql = ClickHouseVisitor.asString(predicate);
String createPolicy = "CREATE ROW POLICY " + policyName + " ON " + fqTable + " USING (" + predicateSql
+ ") TO CURRENT_USER";
Expand Down Expand Up @@ -107,8 +124,13 @@ public void check() throws SQLException {
String filteredQuery = ClickHouseVisitor.asString(filtered);
List<String> filteredRows = ComparatorHelper.getResultSetFirstColumnAsString(filteredQuery, errors, state);

ComparatorHelper.assumeResultSetsAreEqual(baselineRows, filteredRows, baselineQuery, List.of(filteredQuery),
state);
try {
ComparatorHelper.assumeResultSetsAreEqual(baselineRows, filteredRows, baselineQuery,
List.of(filteredQuery), state);
} catch (AssertionError e) {
logMismatchDiagnostics(table.getName(), filteredQuery);
throw e;
}
} finally {

try {
Expand All @@ -119,4 +141,31 @@ public void check() throws SQLException {
}
}

private List<String> rowPoliciesOn(String tableName) {
List<String> names = new ArrayList<>();
String query = "SELECT short_name FROM system.row_policies WHERE database = "
+ ClickHouseFailureDiagnostics.asStringLiteral(state.getDatabaseName()) + " AND table = "
+ ClickHouseFailureDiagnostics.asStringLiteral(tableName);
try (Statement s = state.getConnection().createStatement(); ResultSet rs = s.executeQuery(query)) {
while (rs.next()) {
names.add(rs.getString(1));
}
} catch (SQLException e) {
throw new IgnoreMeException();
}
return names;
}

private void logMismatchDiagnostics(String tableName, String filteredQuery) {
ClickHouseFailureDiagnostics.logRowPolicies(state, tableName);
ClickHouseFailureDiagnostics.logTableState(state, tableName);
String reread;
try {
reread = ComparatorHelper.getResultSetFirstColumnAsString(filteredQuery, errors, state).size() + " rows";
} catch (IgnoreMeException | SQLException | AssertionError e) {
reread = "unavailable";
}
state.getState().getLocalState().log("-- diagnostics: filtered query re-read: " + reread);
}

}
Loading