|
| 1 | +package sqlancer.clickhouse.oracle.groupby; |
| 2 | + |
| 3 | +import java.sql.SQLException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.TreeMap; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import com.clickhouse.data.ClickHouseDataType; |
| 11 | + |
| 12 | +import sqlancer.ComparatorHelper; |
| 13 | +import sqlancer.IgnoreMeException; |
| 14 | +import sqlancer.Randomly; |
| 15 | +import sqlancer.clickhouse.ClickHouseErrors; |
| 16 | +import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState; |
| 17 | +import sqlancer.clickhouse.ClickHouseSchema.ClickHouseColumn; |
| 18 | +import sqlancer.clickhouse.ClickHouseSchema.ClickHouseTable; |
| 19 | +import sqlancer.common.oracle.TestOracle; |
| 20 | +import sqlancer.common.query.ExpectedErrors; |
| 21 | + |
| 22 | +public class ClickHouseGroupingDecompositionOracle implements TestOracle<ClickHouseGlobalState> { |
| 23 | + |
| 24 | + private final ClickHouseGlobalState state; |
| 25 | + private final ExpectedErrors readErrors = new ExpectedErrors(); |
| 26 | + |
| 27 | + public ClickHouseGroupingDecompositionOracle(ClickHouseGlobalState state) { |
| 28 | + this.state = state; |
| 29 | + ClickHouseErrors.addExpectedExpressionErrors(readErrors); |
| 30 | + ClickHouseErrors.addSessionSettingsErrors(readErrors); |
| 31 | + readErrors.add("UNKNOWN_TABLE"); |
| 32 | + readErrors.add("Unknown table expression identifier"); |
| 33 | + readErrors.add("UNKNOWN_IDENTIFIER"); |
| 34 | + readErrors.add("Missing columns"); |
| 35 | + readErrors.add("(MEMORY_LIMIT_EXCEEDED)"); |
| 36 | + readErrors.add("memory limit exceeded"); |
| 37 | + readErrors.add("TIMEOUT_EXCEEDED"); |
| 38 | + readErrors.add("Timeout exceeded"); |
| 39 | + readErrors.add("Limit for result exceeded"); |
| 40 | + readErrors.add("TOO_MANY_ROWS_OR_BYTES"); |
| 41 | + readErrors.add("ILLEGAL_AGGREGATION"); |
| 42 | + readErrors.add("NOT_AN_AGGREGATE"); |
| 43 | + readErrors.add("Aggregate function GROUPING"); |
| 44 | + readErrors.add("Unknown function GROUPING"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void check() throws SQLException { |
| 49 | + if (!state.getClickHouseOptions().groupingDecompositionOracle) { |
| 50 | + throw new IgnoreMeException(); |
| 51 | + } |
| 52 | + List<ClickHouseTable> tables = state.getSchema().getRandomTableNonEmptyTables().getTables(); |
| 53 | + if (tables.isEmpty()) { |
| 54 | + throw new IgnoreMeException(); |
| 55 | + } |
| 56 | + ClickHouseTable table = Randomly.fromList(tables); |
| 57 | + if (table.isView()) { |
| 58 | + throw new IgnoreMeException(); |
| 59 | + } |
| 60 | + List<ClickHouseColumn> eligible = table.getColumns().stream() |
| 61 | + .filter(c -> !c.isAlias() && !c.isMaterialized()) |
| 62 | + .filter(c -> isNonFloatScalarKey(c.getType().getType())).collect(Collectors.toList()); |
| 63 | + if (eligible.isEmpty()) { |
| 64 | + throw new IgnoreMeException(); |
| 65 | + } |
| 66 | + |
| 67 | + String tableQ = quote(table.getName()); |
| 68 | + ClickHouseColumn keyColumn = Randomly.fromList(eligible); |
| 69 | + String keyRef = quote(keyColumn.getName()); |
| 70 | + String label = "key=" + keyColumn.getName() + " table=" + table.getName(); |
| 71 | + |
| 72 | + checkRollupDetailEqualsPlain(tableQ, keyRef, label); |
| 73 | + checkRollupSuperAggregateEqualsGrandTotal(tableQ, keyRef, label); |
| 74 | + checkPerGroupCountSumEqualsGrandTotal(tableQ, keyRef, label); |
| 75 | + } |
| 76 | + |
| 77 | + private void checkRollupDetailEqualsPlain(String tableQ, String keyRef, String label) throws SQLException { |
| 78 | + String plain = "SELECT toString(tuple(" + keyRef + ", count())) FROM " + tableQ + " GROUP BY " + keyRef; |
| 79 | + String rollupDetail = "SELECT toString(tuple(" + keyRef + ", count())) FROM " + tableQ + " GROUP BY " + keyRef |
| 80 | + + " WITH ROLLUP HAVING GROUPING(" + keyRef + ") = 0"; |
| 81 | + |
| 82 | + List<String> plainRows = ComparatorHelper.getResultSetFirstColumnAsString(plain, readErrors, state); |
| 83 | + List<String> rollupRows = ComparatorHelper.getResultSetFirstColumnAsString(rollupDetail, readErrors, state); |
| 84 | + List<String> diff = multisetDiff(plainRows, rollupRows); |
| 85 | + if (!diff.isEmpty()) { |
| 86 | + throw new AssertionError(String.format( |
| 87 | + "grouping-decomposition ROLLUP-detail vs plain multiset mismatch [%s]:%n plain (%d rows): %s%n " |
| 88 | + + "rollup-detail (%d rows): %s%n first differing entries: %s", |
| 89 | + label, plainRows.size(), plain, rollupRows.size(), rollupDetail, diff)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static List<String> multisetDiff(List<String> left, List<String> right) { |
| 94 | + Map<String, Long> counts = new TreeMap<>(); |
| 95 | + for (String s : left) { |
| 96 | + counts.merge(s == null ? "\\N" : s, 1L, Long::sum); |
| 97 | + } |
| 98 | + for (String s : right) { |
| 99 | + counts.merge(s == null ? "\\N" : s, -1L, Long::sum); |
| 100 | + } |
| 101 | + List<String> diff = new ArrayList<>(); |
| 102 | + for (Map.Entry<String, Long> e : counts.entrySet()) { |
| 103 | + if (e.getValue() == 0) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + if (diff.size() >= 20) { |
| 107 | + break; |
| 108 | + } |
| 109 | + long c = e.getValue(); |
| 110 | + diff.add(e.getKey() + " (+" + Math.abs(c) + " " + (c > 0 ? "plain" : "rollup") + ")"); |
| 111 | + } |
| 112 | + return diff; |
| 113 | + } |
| 114 | + |
| 115 | + private void checkRollupSuperAggregateEqualsGrandTotal(String tableQ, String keyRef, String label) |
| 116 | + throws SQLException { |
| 117 | + String rollupSuper = "SELECT toString(count()) FROM " + tableQ + " GROUP BY " + keyRef |
| 118 | + + " WITH ROLLUP HAVING GROUPING(" + keyRef + ") = 1"; |
| 119 | + String grand = "SELECT toString(count()) FROM " + tableQ; |
| 120 | + |
| 121 | + String superValue = readSingleValue(rollupSuper); |
| 122 | + String grandValue = readSingleValue(grand); |
| 123 | + if (!grandValue.equals(superValue)) { |
| 124 | + throw new AssertionError(String.format( |
| 125 | + "grouping-decomposition ROLLUP super-aggregate mismatch [%s]:%n super: %s -> %s%n grand: %s -> %s", |
| 126 | + label, rollupSuper, superValue, grand, grandValue)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void checkPerGroupCountSumEqualsGrandTotal(String tableQ, String keyRef, String label) |
| 131 | + throws SQLException { |
| 132 | + String perGroupSum = "SELECT toString(sum(g)) FROM (SELECT count() AS g FROM " + tableQ + " GROUP BY " + keyRef |
| 133 | + + ")"; |
| 134 | + String grand = "SELECT toString(count()) FROM " + tableQ; |
| 135 | + |
| 136 | + String sumValue = readSingleValue(perGroupSum); |
| 137 | + String grandValue = readSingleValue(grand); |
| 138 | + if (!grandValue.equals(sumValue)) { |
| 139 | + throw new AssertionError(String.format( |
| 140 | + "grouping-decomposition per-group-count-sum mismatch [%s]:%n sum: %s -> %s%n grand: %s -> %s", |
| 141 | + label, perGroupSum, sumValue, grand, grandValue)); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private String readSingleValue(String query) throws SQLException { |
| 146 | + List<String> rows = ComparatorHelper.getResultSetFirstColumnAsString(query, readErrors, state); |
| 147 | + if (rows.size() != 1) { |
| 148 | + throw new IgnoreMeException(); |
| 149 | + } |
| 150 | + return rows.get(0); |
| 151 | + } |
| 152 | + |
| 153 | + static boolean isNonFloatScalarKey(ClickHouseDataType t) { |
| 154 | + switch (t) { |
| 155 | + case Int8: |
| 156 | + case Int16: |
| 157 | + case Int32: |
| 158 | + case Int64: |
| 159 | + case Int128: |
| 160 | + case Int256: |
| 161 | + case UInt8: |
| 162 | + case UInt16: |
| 163 | + case UInt32: |
| 164 | + case UInt64: |
| 165 | + case UInt128: |
| 166 | + case UInt256: |
| 167 | + case String: |
| 168 | + case FixedString: |
| 169 | + case Date: |
| 170 | + case Date32: |
| 171 | + case DateTime: |
| 172 | + case DateTime32: |
| 173 | + case DateTime64: |
| 174 | + case UUID: |
| 175 | + return true; |
| 176 | + default: |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + static String quote(String identifier) { |
| 182 | + return "`" + identifier.replace("`", "``") + "`"; |
| 183 | + } |
| 184 | +} |
0 commit comments