1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// This source file is part of the Pylon open source project.
//
// Copyright (c) 2026 Jaldis B.V.
//
// Licensed under the MIT OR Apache-2.0 license (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//! One canonical answer to "is this the same query?".
//!
//! Several places need to identify a query independently of the values bound
//! to it — metric labels, the compile cache, and migration tooling that wants
//! to talk about a query without quoting it. Each deriving its own key would
//! mean three subtly different notions of sameness; this is the one they
//! share.
//!
//! **What it hashes:** the *compiled* SQL, which carries `$1`/`$2`
//! placeholders rather than values, plus the result shape. So the same query
//! run a million times with different parameters collapses to a single id —
//! which is the whole point, since this is what bounds metric cardinality.
//!
//! **What it does not hash:** parameter values, and the raw PyQL source.
//! Leaving the source out means two spellings that compile identically
//! (whitespace, a `with` binding that inlines away) are correctly recognised
//! as the same query.
use ;
/// Length of the hex id. 16 hex chars is 64 bits — far more than enough to
/// keep a realistic number of distinct query shapes collision-free, and short
/// enough to read in a metric label or a log line.
const ID_HEX_LEN: usize = 16;
/// The stable identifier for a compiled query shape.
///
/// `sql` must be the compiled SQL (placeholders, not values). `shape_repr` is
/// the result shape's own stable rendering — passing it separately keeps two
/// queries with identical SQL but different result shapes from colliding.