-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdigest.h
More file actions
301 lines (265 loc) · 11.6 KB
/
Copy pathdigest.h
File metadata and controls
301 lines (265 loc) · 11.6 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#ifndef SQL_PARSER_DIGEST_H
#define SQL_PARSER_DIGEST_H
#include "sql_parser/common.h"
#include "sql_parser/arena.h"
#include "sql_parser/tokenizer.h"
#include "sql_parser/ast.h"
#include "sql_parser/emitter.h"
#include "sql_parser/string_builder.h"
#include <cstdint>
namespace sql_parser {
struct DigestResult {
StringRef normalized; // "SELECT * FROM t WHERE id = ?"
uint64_t hash; // 64-bit FNV-1a hash
};
// FNV-1a 64-bit hash -- simple, fast, no external dependency
struct FnvHash {
static constexpr uint64_t FNV_OFFSET_BASIS = 14695981039346656037ULL;
static constexpr uint64_t FNV_PRIME = 1099511628211ULL;
uint64_t state = FNV_OFFSET_BASIS;
void update(const char* data, size_t len) {
for (size_t i = 0; i < len; ++i) {
state ^= static_cast<uint64_t>(static_cast<uint8_t>(data[i]));
state *= FNV_PRIME;
}
}
void update_char(char c) {
state ^= static_cast<uint64_t>(static_cast<uint8_t>(c));
state *= FNV_PRIME;
}
uint64_t finish() const { return state; }
};
template <Dialect D>
class Digest {
public:
explicit Digest(Arena& arena) : arena_(arena) {}
// From a parsed AST (Tier 1) -- uses Emitter in DIGEST mode
DigestResult compute(const AstNode* ast) {
Emitter<D> emitter(arena_, EmitMode::DIGEST);
emitter.emit(ast);
StringRef normalized = emitter.result();
FnvHash hasher;
hasher.update(normalized.ptr, normalized.len);
return DigestResult{normalized, hasher.finish()};
}
// From raw SQL (works for any statement) -- uses token-level fallback
DigestResult compute(const char* sql, size_t len) {
return compute_token_level(sql, len);
}
private:
Arena& arena_;
// Helper: check if a token type is a keyword (not an identifier, literal, or operator)
static bool is_keyword_token(TokenType type) {
return static_cast<uint16_t>(type) >= static_cast<uint16_t>(TokenType::TK_SELECT) &&
static_cast<uint16_t>(type) <= static_cast<uint16_t>(TokenType::TK_RECURSIVE);
}
// Helper: check if a token type is a literal value that should become ?
static bool is_literal_token(TokenType type) {
return type == TokenType::TK_INTEGER ||
type == TokenType::TK_FLOAT ||
type == TokenType::TK_STRING ||
type == TokenType::TK_HEX_LITERAL ||
type == TokenType::TK_BIT_LITERAL;
}
// Helper: uppercase a character
static char to_upper(char c) {
return (c >= 'a' && c <= 'z') ? (c - 32) : c;
}
// Append token text uppercased to StringBuilder
static void append_upper(StringBuilder& sb, const char* ptr, uint32_t len) {
for (uint32_t i = 0; i < len; ++i) {
sb.append_char(to_upper(ptr[i]));
}
}
// Determine if we need a space before this token given the previous token type
static bool needs_space_before(TokenType prev, TokenType cur) {
// Never space after ( or before )
if (prev == TokenType::TK_LPAREN) return false;
if (cur == TokenType::TK_RPAREN) return false;
// No space before or after dot
if (prev == TokenType::TK_DOT || cur == TokenType::TK_DOT) return false;
// No space before comma
if (cur == TokenType::TK_COMMA) return false;
// No space after @ or @@
if (prev == TokenType::TK_AT || prev == TokenType::TK_DOUBLE_AT) return false;
// No space before @
if (cur == TokenType::TK_AT) return false;
return true;
}
// Emit a single token to the string builder, uppercasing keywords, replacing literals with ?
void emit_token(StringBuilder& sb, const Token& t, TokenType prev) {
bool quoted_user_after_account = t.type == TokenType::TK_USER_VARIABLE &&
t.source.len >= 2 &&
(t.source.ptr[1] == '\'' || t.source.ptr[1] == '"' || t.source.ptr[1] == '`') &&
(prev == TokenType::TK_STRING || prev == TokenType::TK_QUESTION);
bool space = (prev != TokenType::TK_EOF) &&
!quoted_user_after_account && needs_space_before(prev, t.type);
if (space) sb.append_char(' ');
if (is_literal_token(t.type)) {
sb.append_char('?');
} else if (is_keyword_token(t.type)) {
append_upper(sb, t.text.ptr, t.text.len);
} else if (t.type == TokenType::TK_IDENTIFIER) {
sb.append(t.text.ptr, t.text.len);
} else if (t.type == TokenType::TK_QUESTION) {
sb.append_char('?');
} else if (t.type == TokenType::TK_USER_VARIABLE) {
if (t.source.len >= 2 &&
(t.source.ptr[1] == '\'' || t.source.ptr[1] == '"' || t.source.ptr[1] == '`')) {
sb.append("@?", 2);
} else {
sb.append(t.source);
}
} else if (t.type == TokenType::TK_COMMA) {
sb.append(",", 1);
} else {
// All other tokens: emit as-is
sb.append(t.text.ptr, t.text.len);
}
}
// Skip tokens inside parentheses until matching close paren. Returns last token type consumed.
void skip_paren_contents(Tokenizer<D>& tok) {
int depth = 1;
while (depth > 0) {
Token inner = tok.next_token();
if (inner.type == TokenType::TK_EOF) break;
if (inner.type == TokenType::TK_LPAREN) depth++;
if (inner.type == TokenType::TK_RPAREN) depth--;
}
}
// Token-level digest: walk tokens, normalize, hash
DigestResult compute_token_level(const char* sql, size_t len) {
Tokenizer<D> tok;
tok.reset(sql, len);
StringBuilder sb(arena_);
TokenType prev = TokenType::TK_EOF;
// We collect tokens into a small buffer for lookahead patterns
// Main loop: read token, check for special patterns, emit
Token t = tok.next_token();
while (t.type != TokenType::TK_EOF && t.type != TokenType::TK_SEMICOLON) {
// Pattern: IN (...) -> collapse to IN (?)
if (t.type == TokenType::TK_IN) {
emit_token(sb, t, prev);
prev = t.type;
Token next = tok.next_token();
if (next.type == TokenType::TK_LPAREN) {
// Emit " ("
emit_token(sb, next, prev);
prev = next.type;
// Collapse contents to single ?
bool emitted_q = false;
int depth = 1;
while (depth > 0) {
Token inner = tok.next_token();
if (inner.type == TokenType::TK_EOF) break;
if (inner.type == TokenType::TK_LPAREN) { depth++; continue; }
if (inner.type == TokenType::TK_RPAREN) {
depth--;
if (depth == 0) {
sb.append_char(')');
prev = TokenType::TK_RPAREN;
break;
}
continue;
}
if (!emitted_q) {
sb.append_char('?');
prev = TokenType::TK_QUESTION;
emitted_q = true;
}
}
t = tok.next_token();
continue;
} else {
// IN not followed by ( -- process next token normally
t = next;
continue;
}
}
// Pattern: VALUES (...), (...), ... -> collapse to VALUES (?, ?, ...)
if (t.type == TokenType::TK_VALUES) {
emit_token(sb, t, prev);
prev = t.type;
Token next = tok.next_token();
if (next.type == TokenType::TK_LPAREN) {
// Emit the opening paren
emit_token(sb, next, prev);
prev = next.type;
// Emit first row contents with ? for each value slot
int depth = 1;
while (depth > 0) {
Token inner = tok.next_token();
if (inner.type == TokenType::TK_EOF) break;
if (inner.type == TokenType::TK_LPAREN) {
depth++;
continue;
}
if (inner.type == TokenType::TK_RPAREN) {
depth--;
if (depth == 0) {
sb.append_char(')');
prev = TokenType::TK_RPAREN;
break;
}
continue;
}
if (inner.type == TokenType::TK_COMMA && depth == 1) {
sb.append(", ", 2);
prev = TokenType::TK_COMMA;
continue;
}
// Emit ? for literals and existing placeholders
if (is_literal_token(inner.type) || inner.type == TokenType::TK_QUESTION) {
// Only emit ? once per value slot (skip if prev already emitted one)
if (prev == TokenType::TK_LPAREN || prev == TokenType::TK_COMMA) {
sb.append_char('?');
prev = TokenType::TK_QUESTION;
}
}
}
// Skip additional rows: , (...)
while (true) {
Token peek = tok.next_token();
if (peek.type == TokenType::TK_COMMA) {
Token peek2 = tok.next_token();
if (peek2.type == TokenType::TK_LPAREN) {
// Skip this entire row
skip_paren_contents(tok);
continue;
} else {
// Comma but not followed by ( -- it's not another row
// Emit the comma and continue with peek2
sb.append(",", 1);
prev = TokenType::TK_COMMA;
t = peek2;
goto emit_normal;
}
} else {
// Not a comma - done with VALUES rows
t = peek;
goto emit_normal;
}
}
} else {
// VALUES not followed by ( -- process next normally
t = next;
continue;
}
}
emit_normal:
// Check if we've reached the end (can happen after VALUES/IN lookahead)
if (t.type == TokenType::TK_EOF || t.type == TokenType::TK_SEMICOLON) break;
emit_token(sb, t, prev);
prev = t.type;
// For literal tokens, record as TK_QUESTION since we emitted ?
if (is_literal_token(t.type)) prev = TokenType::TK_QUESTION;
t = tok.next_token();
}
StringRef normalized = sb.finish();
FnvHash hasher;
hasher.update(normalized.ptr, normalized.len);
return DigestResult{normalized, hasher.finish()};
}
};
} // namespace sql_parser
#endif // SQL_PARSER_DIGEST_H