no-mistakes-postgres-sql 0.109.125

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use super::query_is_correlated;
use crate::parse_postgres_sql;
use crate::statements::extract_sql_statement_facts;
use sqlparser::ast::Statement;

fn exists_ops(sql: &str) -> Vec<(bool, bool)> {
    extract_sql_statement_facts(sql)
        .selects
        .into_iter()
        .flat_map(|select| select.exists_set_operations)
        .map(|exists| (exists.restricted, exists.correlated))
        .collect()
}

#[test]
fn uncorrelated_union_is_not_correlated() {
    let flags = exists_ops(
        "SELECT 1 WHERE EXISTS (
            SELECT 1 FROM topics
            UNION ALL
            SELECT 1 FROM topics
         )",
    );
    assert_eq!(flags, vec![(false, false)], "{flags:?}");
}

#[test]
fn outer_qualifier_is_correlated() {
    let flags = exists_ops(
        "SELECT * FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         )",
    );
    assert_eq!(flags, vec![(false, true)], "{flags:?}");
}

#[test]
fn inner_restriction_does_not_clear_correlation() {
    let flags = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id AND topics.id = $1
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id AND tags.id = $1
         )",
    );
    assert_eq!(flags, vec![(true, true)], "{flags:?}");
}

#[test]
fn select_list_probe_is_uncorrelated() {
    let flags = exists_ops(
        "SELECT EXISTS (
            SELECT 1 FROM topics
            UNION ALL
            SELECT 1 FROM topics
         ) AS has_any",
    );
    assert_eq!(flags, vec![(false, false)], "{flags:?}");
}

#[test]
fn select_list_and_having_outer_refs_are_correlated() {
    let projection = exists_ops(
        "SELECT EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         ) FROM posts",
    );
    assert_eq!(projection, vec![(false, true)], "{projection:?}");
    let having = exists_ops(
        "SELECT 1 FROM posts GROUP BY posts.id HAVING EXISTS (
            SELECT * FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT * FROM tags WHERE tags.post_id = posts.id
         )",
    );
    assert_eq!(having, vec![(false, true)], "{having:?}");
}

#[test]
fn alias_cte_join_and_wrapper_shapes() {
    let aliased = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics t WHERE t.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         )",
    );
    assert_eq!(aliased, vec![(false, true)]);
    let cte = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            WITH x AS (SELECT 1 FROM topics WHERE topics.post_id = posts.id)
            SELECT 1 FROM x
            UNION ALL
            SELECT 1 FROM x
         )",
    );
    assert_eq!(cte, vec![(false, true)]);
    let wrapped = exists_ops(
        "SELECT 1 WHERE EXISTS (
            (SELECT 1 FROM topics UNION ALL SELECT 1 FROM topics)
         )",
    );
    assert_eq!(wrapped, vec![(false, false)]);
    let derived = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM (
                SELECT 1 FROM topics UNION ALL SELECT 1 FROM tags
            ) candidate
         )",
    );
    assert!(derived.is_empty(), "{derived:?}");
}

#[test]
fn join_cast_in_subquery_and_values_arms() {
    let join = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics JOIN tags ON tags.post_id = posts.id
            UNION ALL
            SELECT 1 FROM topics LEFT OUTER JOIN tags ON tags.post_id = posts.id
         )",
    );
    assert_eq!(join, vec![(false, true)]);
    let outer = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics LEFT JOIN tags ON tags.post_id = posts.id
            UNION ALL
            SELECT 1 FROM topics RIGHT OUTER JOIN tags ON tags.post_id = posts.id
         )",
    );
    assert_eq!(outer, vec![(false, true)]);
    let casted = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = CAST(posts.id AS int)
            UNION ALL
            SELECT 1 FROM tags WHERE NOT (tags.post_id = posts.id)
         )",
    );
    assert_eq!(casted, vec![(false, true)]);
    let nested_in = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.id IN (SELECT posts.id)
            UNION ALL
            SELECT 1 FROM tags
         )",
    );
    assert_eq!(nested_in, vec![(false, true)]);
    let values = exists_ops("SELECT 1 WHERE EXISTS (VALUES (1) UNION ALL VALUES (2))");
    assert_eq!(values, vec![(false, false)], "{values:?}");
    let on_exists = exists_ops(
        "SELECT 1 FROM posts JOIN extra ON EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         )",
    );
    assert_eq!(on_exists, vec![(false, true)]);
}

#[test]
fn nested_join_cross_join_and_full_join_are_walked() {
    let nested = exists_ops(
        "SELECT 1 FROM posts CROSS JOIN extra WHERE EXISTS (
            SELECT 1 FROM (topics CROSS JOIN tags)
            WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM generate_series(1, 2) AS g
         )",
    );
    assert_eq!(nested, vec![(false, true)], "{nested:?}");
    let full = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics FULL JOIN tags ON tags.post_id = posts.id
            UNION ALL
            SELECT 1 FROM topics RIGHT JOIN tags ON tags.post_id = posts.id
         )",
    );
    assert_eq!(full, vec![(false, true)]);
}

#[test]
fn query_is_correlated_reads_direct_select() {
    let Statement::Query(query) = parse_postgres_sql(
        "SELECT 1 FROM topics WHERE topics.post_id = posts.id
         UNION ALL
         SELECT 1 FROM tags WHERE tags.post_id = posts.id",
    )
    .unwrap()
    .pop()
    .unwrap() else {
        panic!("query");
    };
    assert!(query_is_correlated(&query));
}

#[test]
fn coalesce_case_between_and_schema_qualified_refs() {
    let wrapped = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = COALESCE(posts.id, 0)
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id BETWEEN posts.id AND posts.id
         )",
    );
    assert_eq!(wrapped, vec![(false, true)], "{wrapped:?}");
    let distinct = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id IS DISTINCT FROM posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id IS NOT NULL
         )",
    );
    assert_eq!(distinct, vec![(false, true)], "{distinct:?}");
    let schema_local = exists_ops(
        "SELECT 1 WHERE EXISTS (
            SELECT 1 FROM public.topics WHERE public.topics.id = 1
            UNION ALL
            SELECT 1 FROM public.tags WHERE public.tags.id = 2
         )",
    );
    assert_eq!(schema_local, vec![(true, false)], "{schema_local:?}");
}

#[test]
fn aliased_inner_table_hides_base_name() {
    let flags = exists_ops(
        "SELECT 1 FROM topics WHERE EXISTS (
            SELECT 1 FROM topics AS inner_topics
            WHERE inner_topics.parent_id = topics.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.topic_id = topics.id
         )",
    );
    assert_eq!(flags, vec![(false, true)], "{flags:?}");
}

#[test]
fn select_list_case_wrapped_exists_is_collected() {
    let flags = exists_ops(
        "SELECT CASE WHEN EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         ) THEN 1 END FROM posts",
    );
    assert_eq!(flags, vec![(false, true)], "{flags:?}");
}

#[test]
fn function_inlist_star_and_natural_join_shapes() {
    let coalesced = exists_ops(
        "SELECT COALESCE(EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         ), false) FROM posts",
    );
    assert_eq!(coalesced, vec![(false, true)], "{coalesced:?}");
    let listed = exists_ops(
        "SELECT * FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id IN (posts.id, 0)
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id IS TRUE
         )",
    );
    assert_eq!(listed, vec![(false, true)], "{listed:?}");
    let natural = exists_ops(
        "SELECT 1 WHERE EXISTS (
            SELECT 1 FROM topics NATURAL JOIN tags
            UNION ALL
            SELECT 1 FROM topics
         )",
    );
    assert_eq!(natural, vec![(false, false)], "{natural:?}");
}

#[test]
fn like_in_subquery_table_args_group_by_and_quoted_idents() {
    let liked = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE topics.name LIKE posts.pattern
            UNION ALL
            SELECT 1 FROM tags WHERE tags.name LIKE posts.pattern
         )",
    );
    assert_eq!(liked, vec![(false, true)], "{liked:?}");
    let in_left = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM topics WHERE posts.id IN (SELECT post_id FROM allowed)
            UNION ALL
            SELECT 1 FROM tags WHERE posts.id IN (SELECT post_id FROM allowed)
         )",
    );
    assert_eq!(in_left, vec![(false, true)], "{in_left:?}");
    let table_args = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT 1 FROM jsonb_array_elements(posts.tags) AS tag
            UNION ALL
            SELECT 1 FROM jsonb_array_elements(posts.tags) AS tag
         )",
    );
    assert_eq!(table_args, vec![(false, true)], "{table_args:?}");
    let grouped = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT count(*) FROM topics GROUP BY posts.id
            UNION ALL
            SELECT count(*) FROM tags GROUP BY posts.id
         )",
    );
    assert_eq!(grouped, vec![(false, true)], "{grouped:?}");
    let quoted = exists_ops(
        r#"SELECT 1 FROM "Posts" WHERE EXISTS (
            SELECT 1 FROM topics AS posts WHERE topics.post_id = "Posts".id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = "Posts".id
         )"#,
    );
    assert_eq!(quoted, vec![(false, true)], "{quoted:?}");
    let values = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            VALUES (posts.id)
            UNION ALL
            VALUES (1)
         )",
    );
    assert_eq!(values, vec![(false, true)], "{values:?}");
    let grouped_exists = exists_ops(
        "SELECT 1 FROM posts GROUP BY EXISTS (
            SELECT 1 FROM topics WHERE topics.post_id = posts.id
            UNION ALL
            SELECT 1 FROM tags WHERE tags.post_id = posts.id
         )",
    );
    assert_eq!(grouped_exists, vec![(false, true)], "{grouped_exists:?}");
    let wildcard = exists_ops(
        "SELECT 1 FROM posts WHERE EXISTS (
            SELECT posts.* FROM topics
            UNION ALL
            SELECT posts.* FROM tags
         )",
    );
    assert_eq!(wildcard, vec![(false, true)], "{wildcard:?}");
}

#[test]
fn query_is_correlated_walks_remaining_set_and_join_shapes() {
    let Statement::Query(query) = parse_postgres_sql(
        "WITH extra AS (SELECT 1)
         SELECT topics.* FROM topics
         INNER JOIN LATERAL (SELECT posts.id) nested ON nested.id = topics.id
         WHERE EXISTS (SELECT 1 FROM extra)
         GROUP BY ALL",
    )
    .unwrap()
    .pop()
    .unwrap() else {
        panic!("query");
    };
    assert!(query_is_correlated(&query));
    let Statement::Query(values) = parse_postgres_sql("VALUES (posts.id), (1)")
        .unwrap()
        .pop()
        .unwrap()
    else {
        panic!("values");
    };
    assert!(query_is_correlated(&values));
    let Statement::Query(join) = parse_postgres_sql(
        "SELECT 1 FROM topics
         JOIN tags ON tags.post_id = outer_posts.id
         LEFT OUTER JOIN extra ON extra.post_id = outer_posts.id
         RIGHT OUTER JOIN other ON extra.id = other.id
         FULL OUTER JOIN more ON more.id = outer_posts.id",
    )
    .unwrap()
    .pop()
    .unwrap() else {
        panic!("join");
    };
    assert!(query_is_correlated(&join));
    for sql in [
        "SELECT 1 FROM (SELECT posts.id) AS derived",
        "SELECT 1 FROM generate_series(1, posts.n) AS g",
        "SELECT 1 WHERE posts.id IN (SELECT id FROM topics)",
        "SELECT (SELECT posts.id)",
    ] {
        let Statement::Query(query) = parse_postgres_sql(sql).unwrap().pop().unwrap() else {
            panic!("{sql}");
        };
        assert!(query_is_correlated(&query), "{sql} should be correlated");
    }
    let Statement::Query(wrap) =
        parse_postgres_sql("SELECT 1 FROM (SELECT 1 FROM nested JOIN extra ON true) AS wrap")
            .unwrap()
            .pop()
            .unwrap()
    else {
        panic!("wrap");
    };
    assert!(!query_is_correlated(&wrap));
}

#[test]
fn query_is_correlated_covers_parser_rare_variants() {
    use sqlparser::ast::{
        Expr, GroupByExpr, Ident, JoinOperator, SelectItem, SelectItemQualifiedWildcardKind,
        SetExpr, TableFactor, WildcardAdditionalOptions,
    };

    let Statement::Query(mut query) =
        parse_postgres_sql("SELECT topics.* FROM topics JOIN tags ON tags.id = topics.id")
            .unwrap()
            .pop()
            .unwrap()
    else {
        panic!("query");
    };
    let SetExpr::Select(select) = query.body.as_mut() else {
        panic!("select");
    };
    select.group_by = GroupByExpr::All(vec![]);
    select.projection.push(SelectItem::QualifiedWildcard(
        SelectItemQualifiedWildcardKind::Expr(Expr::Identifier(Ident::new("topics"))),
        WildcardAdditionalOptions::default(),
    ));
    let inner = select.from[0].clone();
    select.from[0].relation = TableFactor::NestedJoin {
        table_with_joins: Box::new(inner.clone()),
        alias: None,
    };
    select.from.push(inner.clone());
    select.from.last_mut().unwrap().relation = TableFactor::TableFunction {
        expr: Expr::Identifier(Ident::new("topics")),
        alias: None,
    };
    if let Some(join) = select.from[0].joins.first_mut() {
        join.join_operator = JoinOperator::Inner(sqlparser::ast::JoinConstraint::On(
            Expr::Identifier(Ident::new("topics")),
        ));
    }
    assert!(!query_is_correlated(&query));

    *query.body = SetExpr::Insert(parse_postgres_sql("COMMIT").unwrap().pop().unwrap());
    assert!(!query_is_correlated(&query));
}