|
| 1 | +#standardSQL |
| 2 | +# Section: Cookies - Cookie Age |
| 3 | +# Question: How many cookies (total, hosts, pages) have negative Max-Age, Expires and real age (Max-Age has precedence over Expires) attributes? |
| 4 | +# Note: Query is expensive and slow (14TB). Query is inefficient (We create a result array of length 1 for each cookie-attribute for each cookie and then unnest it again; We could instead not use arrays and skip the unnesting). |
| 5 | +# Note: Some of the percentages are quite different to the old query; one of both might be broken (difficult to compare as both cannot operate on a shared dataset) |
| 6 | +CREATE TEMPORARY FUNCTION getCookieAgeValues(cookie_value STRING, epochOfRequest NUMERIC) |
| 7 | +RETURNS STRING DETERMINISTIC |
| 8 | +LANGUAGE js AS ''' |
| 9 | + const regexMaxAge = new RegExp(/max-age\\s*=\\s*(?<value>-*[0-9]+)/i); |
| 10 | + const regexExpires = new RegExp(/expires\\s*=\\s*(?<value>.*?)(;|$)/i); |
| 11 | + const cookieValues = [cookie_value]; |
| 12 | + const result = { |
| 13 | + "maxAge": [], |
| 14 | + "expires": [], |
| 15 | + "realAge": [] |
| 16 | + }; |
| 17 | + cookieValues.forEach(cookie => { |
| 18 | + let maxAge = null; |
| 19 | + let expires = null; |
| 20 | + if (regexMaxAge.exec(cookie)) { |
| 21 | + maxAge = Number(regexMaxAge.exec(cookie)[1]); |
| 22 | + result["maxAge"].push(maxAge); |
| 23 | + } |
| 24 | + if (regexExpires.exec(cookie)) { |
| 25 | + expires = Math.round(Number(new Date(regexExpires.exec(cookie)[1])) / 1000) - epochOfRequest; |
| 26 | + result["expires"].push(Number.isSafeInteger(expires) ? expires : null); |
| 27 | + } |
| 28 | + if (maxAge) { |
| 29 | + result["realAge"].push(maxAge); |
| 30 | + } else if (expires) { |
| 31 | + result["realAge"].push(expires); |
| 32 | + } |
| 33 | + }); |
| 34 | + return JSON.stringify(result); |
| 35 | +'''; |
| 36 | + |
| 37 | +WITH age_values AS ( |
| 38 | + SELECT |
| 39 | + client, |
| 40 | + page, |
| 41 | + NET.HOST(url) AS host, |
| 42 | + getCookieAgeValues(response_headers.value, INT64(summary.startedDateTime)) AS values |
| 43 | + FROM |
| 44 | + `httparchive.crawl.requests`, |
| 45 | + UNNEST(response_headers) AS response_headers |
| 46 | + WHERE |
| 47 | + date = '2025-07-01' AND |
| 48 | + is_root_page AND |
| 49 | + LOWER(response_headers.name) = 'set-cookie' |
| 50 | +), |
| 51 | + |
| 52 | +max_age_values AS ( |
| 53 | + SELECT |
| 54 | + client, |
| 55 | + COUNTIF(SAFE_CAST(max_age_value AS NUMERIC) <= 0) AS count_negative_max_age, |
| 56 | + SUM(COUNT(0)) OVER (PARTITION BY client) AS total_max_age_cookies, |
| 57 | + COUNT(DISTINCT IF(SAFE_CAST(max_age_value AS NUMERIC) <= 0, page, NULL)) AS num_max_age_pages, |
| 58 | + COUNT(DISTINCT page) AS total_max_age_pages, |
| 59 | + COUNT(DISTINCT IF(SAFE_CAST(max_age_value AS NUMERIC) <= 0, host, NULL)) AS num_max_age_hosts, |
| 60 | + COUNT(DISTINCT host) AS total_max_age_hosts |
| 61 | + FROM age_values, |
| 62 | + UNNEST(JSON_QUERY_ARRAY(values, '$.maxAge')) AS max_age_value |
| 63 | + GROUP BY |
| 64 | + client |
| 65 | + ORDER BY |
| 66 | + client |
| 67 | +), |
| 68 | + |
| 69 | +expires_values AS ( |
| 70 | + SELECT |
| 71 | + client, |
| 72 | + COUNTIF(SAFE_CAST(expires_value AS NUMERIC) <= 0) AS count_negative_expires, |
| 73 | + SUM(COUNT(0)) OVER (PARTITION BY client) AS total_expires_cookies, |
| 74 | + COUNT(DISTINCT IF(SAFE_CAST(expires_value AS NUMERIC) <= 0, page, NULL)) AS num_expires_pages, |
| 75 | + COUNT(DISTINCT page) AS total_expires_pages, |
| 76 | + COUNT(DISTINCT IF(SAFE_CAST(expires_value AS NUMERIC) <= 0, host, NULL)) AS num_expires_hosts, |
| 77 | + COUNT(DISTINCT host) AS total_expires_hosts |
| 78 | + FROM age_values, |
| 79 | + UNNEST(JSON_QUERY_ARRAY(values, '$.expires')) AS expires_value |
| 80 | + GROUP BY |
| 81 | + client |
| 82 | + ORDER BY |
| 83 | + client |
| 84 | +), |
| 85 | + |
| 86 | +real_age_values AS ( |
| 87 | + SELECT |
| 88 | + client, |
| 89 | + COUNTIF(SAFE_CAST(real_age_value AS NUMERIC) <= 0) AS count_negative_real_age, |
| 90 | + SUM(COUNT(0)) OVER (PARTITION BY client) AS total_real_age_cookies, |
| 91 | + COUNT(DISTINCT IF(SAFE_CAST(real_age_value AS NUMERIC) <= 0, page, NULL)) AS num_real_age_pages, |
| 92 | + COUNT(DISTINCT page) AS total_real_age_pages, |
| 93 | + COUNT(DISTINCT IF(SAFE_CAST(real_age_value AS NUMERIC) <= 0, host, NULL)) AS num_real_age_hosts, |
| 94 | + COUNT(DISTINCT host) AS total_real_age_hosts |
| 95 | + FROM age_values, |
| 96 | + UNNEST(JSON_QUERY_ARRAY(values, '$.realAge')) AS real_age_value |
| 97 | + GROUP BY |
| 98 | + client |
| 99 | + ORDER BY |
| 100 | + client |
| 101 | +) |
| 102 | + |
| 103 | +SELECT |
| 104 | + client, |
| 105 | + count_negative_max_age, |
| 106 | + count_negative_max_age / total_max_age_cookies AS pct_negative_max_age, |
| 107 | + num_max_age_pages, |
| 108 | + num_max_age_pages / total_max_age_pages AS pct_max_age_pages, |
| 109 | + num_max_age_hosts, |
| 110 | + num_max_age_hosts / total_max_age_hosts AS pct_max_age_hosts, |
| 111 | + count_negative_expires, |
| 112 | + count_negative_expires / total_expires_cookies AS pct_negative_expires, |
| 113 | + num_expires_pages, |
| 114 | + num_expires_pages / total_expires_pages AS pct_expires_pages, |
| 115 | + num_expires_hosts, |
| 116 | + num_expires_hosts / total_expires_hosts AS pct_expires_hosts, |
| 117 | + count_negative_real_age, |
| 118 | + count_negative_real_age / total_real_age_cookies AS pct_negative_real_age, |
| 119 | + num_real_age_pages, |
| 120 | + num_real_age_pages / total_real_age_pages AS pct_real_age_pages, |
| 121 | + num_real_age_hosts, |
| 122 | + num_real_age_hosts / total_real_age_hosts AS pct_real_age_hosts |
| 123 | +FROM |
| 124 | + max_age_values |
| 125 | +JOIN expires_values |
| 126 | +USING (client) |
| 127 | +JOIN real_age_values |
| 128 | +USING (client) |
| 129 | +ORDER BY |
| 130 | + client |
0 commit comments