From 5f7eab54d9644575afd233ee6aeec8edfdea98ff Mon Sep 17 00:00:00 2001 From: Groene AI <270696204+groeneai@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:57:26 +0000 Subject: [PATCH] fix(clickhouse): RowPolicy checks that no other row policy is on the table The RowPolicy oracle compares `SELECT c FROM t WHERE p` with `SELECT c FROM t` under `CREATE ROW POLICY ... USING (p)`. It assumed that its own policy is the only one on the table, but its cleanup is best effort: the `finally` DROP goes through the expected errors, which include transport errors, `Memory limit` and `TIMEOUT_EXCEEDED`. Row policies also survive `DROP DATABASE`, and every database of a thread reuses the same name, so a leaked permissive policy also applies to the next `t0` of that thread. With one present, the baseline sees `old AND p` and the policy read sees `old OR p`, which is reported as a row policy bypass: The size of the result sets mismatch (0 and 11)! SELECT t0.c0 FROM t0 WHERE ((t0.c0)%(t0.c0)) -> 0 SELECT t0.c0 FROM t0 -> 11 Before the baseline, list the policies on the table in system.row_policies; drop any that exist and skip the check. On a mismatch, log the policies on the table, the table state and a re-read row count of the filtered query to the reproducer, then rethrow. Co-Authored-By: Claude Opus 5.5 --- .../ClickHouseFailureDiagnostics.java | 9 ++- .../rowpolicy/ClickHouseRowPolicyOracle.java | 55 ++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/sqlancer/clickhouse/ClickHouseFailureDiagnostics.java b/src/sqlancer/clickhouse/ClickHouseFailureDiagnostics.java index 8704811e9..45f82b9f1 100644 --- a/src/sqlancer/clickhouse/ClickHouseFailureDiagnostics.java +++ b/src/sqlancer/clickhouse/ClickHouseFailureDiagnostics.java @@ -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); @@ -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("'", "\\'") + "'"; } } diff --git a/src/sqlancer/clickhouse/oracle/rowpolicy/ClickHouseRowPolicyOracle.java b/src/sqlancer/clickhouse/oracle/rowpolicy/ClickHouseRowPolicyOracle.java index 246508108..ec02f73a3 100644 --- a/src/sqlancer/clickhouse/oracle/rowpolicy/ClickHouseRowPolicyOracle.java +++ b/src/sqlancer/clickhouse/oracle/rowpolicy/ClickHouseRowPolicyOracle.java @@ -1,6 +1,9 @@ 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; @@ -8,6 +11,7 @@ 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; @@ -56,6 +60,20 @@ public void check() throws SQLException { throw new IgnoreMeException(); } + String fqTable = state.getDatabaseName() + "." + table.getName(); + List 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(); @@ -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"; @@ -107,8 +124,13 @@ public void check() throws SQLException { String filteredQuery = ClickHouseVisitor.asString(filtered); List 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 { @@ -119,4 +141,31 @@ public void check() throws SQLException { } } + private List rowPoliciesOn(String tableName) { + List 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); + } + }