|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -suite("condition_cache_eligibility") { |
19 | | - sql "DROP TABLE IF EXISTS condition_cache_eligibility" |
20 | | - sql """ |
21 | | - CREATE TABLE condition_cache_eligibility ( |
22 | | - k BIGINT, |
23 | | - a ARRAY<INT>, |
24 | | - dt1 DATETIME, |
25 | | - dt2 DATETIME |
26 | | - ) DUPLICATE KEY(k) |
27 | | - DISTRIBUTED BY HASH(k) BUCKETS 1 |
28 | | - PROPERTIES ("replication_num" = "1") |
29 | | - """ |
30 | | - sql """ |
31 | | - INSERT INTO condition_cache_eligibility |
32 | | - SELECT number, [1,2,3,4,5,6,7], '2026-09-30 12:00:00', '2026-09-30 12:00:00' |
33 | | - FROM numbers("number" = "100000") |
34 | | - """ |
| 18 | +import org.apache.doris.regression.action.ProfileAction |
35 | 19 |
|
36 | | - // Keep seeded random sequences stable across scans; isolate condition cache from result caches. |
37 | | - sql "set enable_parallel_scan = false" |
38 | | - sql "set enable_sql_cache = false" |
39 | | - sql "set enable_query_cache = false" |
40 | | - sql "set enable_condition_cache = false" |
41 | | - order_qt_rand_baseline """ |
42 | | - SELECT count(*) FROM condition_cache_eligibility WHERE k < 0 OR rand(1) < 0.0001 |
43 | | - """ |
44 | | - order_qt_shuffle_baseline """ |
45 | | - SELECT count(*) FROM condition_cache_eligibility WHERE array_shuffle(a, 1) = [1,2,3,4,5,6,7] |
46 | | - """ |
| 20 | +// Isolate the global lookup metric from other condition-cache queries. |
| 21 | +suite("condition_cache_eligibility", "nonConcurrent") { |
| 22 | + // Internal statistics queries use new sessions and would otherwise pollute the global metric. |
| 23 | + setGlobalVarTemporary([enable_condition_cache: false]) { |
| 24 | + sql "DROP TABLE IF EXISTS condition_cache_eligibility" |
| 25 | + sql """ |
| 26 | + CREATE TABLE condition_cache_eligibility ( |
| 27 | + k BIGINT, |
| 28 | + a ARRAY<INT>, |
| 29 | + dt1 DATETIME, |
| 30 | + dt2 DATETIME |
| 31 | + ) DUPLICATE KEY(k) |
| 32 | + DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 33 | + PROPERTIES ("replication_num" = "1") |
| 34 | + """ |
| 35 | + sql """ |
| 36 | + INSERT INTO condition_cache_eligibility |
| 37 | + SELECT number, [1,2,3,4,5,6,7], '2026-09-30 12:00:00', '2026-09-30 12:00:00' |
| 38 | + FROM numbers("number" = "100000") |
| 39 | + """ |
47 | 40 |
|
48 | | - sql "set enable_condition_cache = true" |
49 | | - order_qt_rand_first """ |
50 | | - SELECT count(*) FROM condition_cache_eligibility WHERE k < 0 OR rand(1) < 0.0001 |
51 | | - """ |
52 | | - order_qt_rand_second """ |
53 | | - SELECT count(*) FROM condition_cache_eligibility WHERE k < 0 OR rand(1) < 0.0001 |
54 | | - """ |
55 | | - order_qt_shuffle_first """ |
56 | | - SELECT count(*) FROM condition_cache_eligibility WHERE array_shuffle(a, 1) = [1,2,3,4,5,6,7] |
57 | | - """ |
58 | | - order_qt_shuffle_second """ |
59 | | - SELECT count(*) FROM condition_cache_eligibility WHERE array_shuffle(a, 1) = [1,2,3,4,5,6,7] |
60 | | - """ |
61 | | - order_qt_shuffle_alias """ |
62 | | - SELECT count(*) FROM condition_cache_eligibility WHERE shuffle(a, 1) = [1,2,3,4,5,6,7] |
63 | | - """ |
64 | | - order_qt_time_date """ |
65 | | - SELECT count(*) FROM condition_cache_eligibility |
66 | | - WHERE CAST(TIMEDIFF(dt1, dt2) AS DATE) = CURRENT_DATE() |
67 | | - """ |
68 | | - order_qt_time_date_second """ |
69 | | - SELECT count(*) FROM condition_cache_eligibility |
70 | | - WHERE CAST(TIMEDIFF(dt1, dt2) AS DATE) = CURRENT_DATE() |
71 | | - """ |
72 | | - order_qt_deterministic_first """ |
73 | | - SELECT count(*) FROM condition_cache_eligibility WHERE k % 10000 = 0 |
74 | | - """ |
75 | | - order_qt_deterministic_second """ |
76 | | - SELECT count(*) FROM condition_cache_eligibility WHERE k % 10000 = 0 |
77 | | - """ |
| 41 | + sql "set enable_parallel_scan = false" |
| 42 | + sql "set enable_sql_cache = false" |
| 43 | + sql "set enable_query_cache = false" |
| 44 | + sql "set enable_condition_cache = true" |
| 45 | + |
| 46 | + def backendIdToIp = [:] |
| 47 | + def backendIdToHttpPort = [:] |
| 48 | + getBackendIpHttpPort(backendIdToIp, backendIdToHttpPort) |
| 49 | + def cacheLookups = { |
| 50 | + long total = 0 |
| 51 | + backendIdToIp.each { backendId, ip -> |
| 52 | + httpTest { |
| 53 | + endpoint "${ip}:${backendIdToHttpPort[backendId]}" |
| 54 | + uri "/metrics?type=json" |
| 55 | + op "get" |
| 56 | + printResponse false |
| 57 | + check { code, body -> |
| 58 | + assertEquals(200, code) |
| 59 | + def metric = parseJson(body).find { |
| 60 | + it.tags.metric == "condition_cache" && it.tags.type == "condition_cache_search" |
| 61 | + } |
| 62 | + assertNotNull(metric, "Missing condition-cache lookup metric") |
| 63 | + total += metric.value as long |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return total |
| 68 | + } |
| 69 | + |
| 70 | + // Seeded shuffle is repeatable within a block, but its count changes with block boundaries. |
| 71 | + // Check that volatile scans never look up the cache instead of recording their random counts. |
| 72 | + for (int batchSize : [1, 1024]) { |
| 73 | + sql "set batch_size = ${batchSize}" |
| 74 | + for (String predicate : [ |
| 75 | + "k < 0 OR rand(1) < 0.0001", |
| 76 | + "k < 0 OR rand() < 0.0001", |
| 77 | + "array_shuffle(a) = [1,2,3,4,5,6,7]", |
| 78 | + "array_shuffle(a, 1) = [1,2,3,4,5,6,7]", |
| 79 | + "shuffle(a, 1) = [1,2,3,4,5,6,7]", |
| 80 | + "CAST(TIMEDIFF(dt1, dt2) AS DATE) = CURRENT_DATE()", |
| 81 | + "CAST(TIMEDIFF(dt1, dt2) AS DATETIME) = CAST(CURRENT_DATE() AS DATETIME)"]) { |
| 82 | + long before = cacheLookups() |
| 83 | + 2.times { |
| 84 | + // Keep one-row batches inexpensive under ASAN while retaining the volatile scan predicate. |
| 85 | + sql "SELECT count(*) FROM condition_cache_eligibility WHERE k < 1000 AND (${predicate})" |
| 86 | + } |
| 87 | + assertEquals(before, cacheLookups(), |
| 88 | + "Volatile scan looked up condition cache: batch_size=${batchSize}, ${predicate}") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + sql "set enable_profile = false" |
| 93 | + def token = UUID.randomUUID().toString() |
| 94 | + def deterministic = """ |
| 95 | + SELECT '${token}', count(*) FROM condition_cache_eligibility WHERE k % 10000 = 0 |
| 96 | + """ |
| 97 | + long before = cacheLookups() |
| 98 | + // Execute the same SQL twice so both scans have the same digest; omit the profile token from output. |
| 99 | + quickRunTest("deterministic_first", deterministic, true, { row -> [row[1]] }) |
| 100 | + // Only the second execution records a profile, so the token cannot select the warm-up query. |
| 101 | + sql "set enable_profile = true" |
| 102 | + sql "set profile_level = 2" |
| 103 | + quickRunTest("deterministic_second", deterministic, true, { row -> [row[1]] }) |
| 104 | + assertTrue(cacheLookups() > before, "Deterministic scans did not look up condition cache") |
| 105 | + def profile = new ProfileAction(context).getProfileBySql(token, ["ConditionCacheHit"]) |
| 106 | + def hits = (profile =~ /ConditionCacheHit: (\d+)/) |
| 107 | + assertTrue(hits.findAll().any { (it[1] as long) > 0 }, |
| 108 | + "Second deterministic scan did not hit condition cache: ${profile}") |
| 109 | + |
| 110 | + order_qt_null_expressions """ |
| 111 | + SELECT count(*) FROM condition_cache_eligibility |
| 112 | + WHERE CAST(CAST(NULL AS TIME) AS DATETIME) IS NULL |
| 113 | + AND array_shuffle(CAST(NULL AS ARRAY<INT>), 1) IS NULL |
| 114 | + AND shuffle(CAST(NULL AS ARRAY<INT>)) IS NULL |
| 115 | + AND array_shuffle(a, NULL) IS NULL |
| 116 | + """ |
| 117 | + } |
78 | 118 | } |
0 commit comments