|
10 | 10 | LeadingWildcardLike, |
11 | 11 | OrAcrossColumns, |
12 | 12 | ScalarUdfInWhere, |
| 13 | + SelectDistinctSuspicious, |
13 | 14 | TruncateTable, |
14 | 15 | ) |
15 | 16 |
|
@@ -387,3 +388,85 @@ def test_w022_does_not_flag_cross_join_inside_trailing_comment(): |
387 | 388 | ) |
388 | 389 | is None |
389 | 390 | ) |
| 391 | + |
| 392 | + |
| 393 | +# W024 select-distinct-suspicious --------------------------------------------- |
| 394 | + |
| 395 | + |
| 396 | +def test_w024_flags_distinct_with_inner_join(): |
| 397 | + rule = SelectDistinctSuspicious() |
| 398 | + finding = _stmt( |
| 399 | + rule, |
| 400 | + "SELECT DISTINCT c.id, c.name FROM customers c JOIN orders o ON c.id = o.customer_id;", |
| 401 | + ) |
| 402 | + assert finding is not None |
| 403 | + assert finding.rule_id == "W024" |
| 404 | + assert finding.severity == "warning" |
| 405 | + |
| 406 | + |
| 407 | +def test_w024_flags_distinct_with_left_join(): |
| 408 | + rule = SelectDistinctSuspicious() |
| 409 | + sql = "SELECT DISTINCT a.id FROM a LEFT JOIN b ON a.id = b.a_id;" |
| 410 | + assert _stmt(rule, sql) is not None |
| 411 | + |
| 412 | + |
| 413 | +def test_w024_flags_distinct_with_join_multiline(): |
| 414 | + rule = SelectDistinctSuspicious() |
| 415 | + sql = ( |
| 416 | + "SELECT DISTINCT\n c.id, c.name\nFROM customers c\nJOIN orders o ON c.id = o.customer_id;" |
| 417 | + ) |
| 418 | + assert _stmt(rule, sql) is not None |
| 419 | + |
| 420 | + |
| 421 | +def test_w024_case_insensitive(): |
| 422 | + rule = SelectDistinctSuspicious() |
| 423 | + assert _stmt(rule, "select distinct a from x join y on x.id = y.id;") is not None |
| 424 | + |
| 425 | + |
| 426 | +def test_w024_does_not_flag_distinct_alone(): |
| 427 | + # Single-table DISTINCT is fine -- no JOIN cardinality blow-up to mask. |
| 428 | + rule = SelectDistinctSuspicious() |
| 429 | + assert _stmt(rule, "SELECT DISTINCT country FROM customers;") is None |
| 430 | + |
| 431 | + |
| 432 | +def test_w024_does_not_flag_count_distinct_with_join(): |
| 433 | + # Aggregate-DISTINCT is a different pattern; the regex anchors on |
| 434 | + # "SELECT DISTINCT" directly, not "COUNT(DISTINCT ...)". |
| 435 | + rule = SelectDistinctSuspicious() |
| 436 | + sql = "SELECT COUNT(DISTINCT c.id) FROM customers c JOIN orders o ON c.id = o.customer_id;" |
| 437 | + assert _stmt(rule, sql) is None |
| 438 | + |
| 439 | + |
| 440 | +def test_w024_does_not_flag_sum_distinct_with_join(): |
| 441 | + rule = SelectDistinctSuspicious() |
| 442 | + sql = "SELECT SUM(DISTINCT amount) FROM payments p JOIN customers c ON p.cust_id = c.id;" |
| 443 | + assert _stmt(rule, sql) is None |
| 444 | + |
| 445 | + |
| 446 | +def test_w024_does_not_flag_join_without_distinct(): |
| 447 | + rule = SelectDistinctSuspicious() |
| 448 | + assert _stmt(rule, "SELECT a.id FROM a JOIN b ON a.id = b.a_id;") is None |
| 449 | + |
| 450 | + |
| 451 | +def test_w024_does_not_flag_distinct_inside_string_literal(): |
| 452 | + rule = SelectDistinctSuspicious() |
| 453 | + sql = "INSERT INTO log(msg) SELECT 'SELECT DISTINCT x JOIN y' FROM t JOIN u ON t.id = u.id;" |
| 454 | + # Outer SELECT does not have DISTINCT; the literal mentions it. |
| 455 | + assert _stmt(rule, sql) is None |
| 456 | + |
| 457 | + |
| 458 | +def test_w024_does_not_flag_distinct_inside_comment(): |
| 459 | + rule = SelectDistinctSuspicious() |
| 460 | + sql = "-- SELECT DISTINCT x FROM y JOIN z\nSELECT id FROM t;" |
| 461 | + assert _stmt(rule, sql) is None |
| 462 | + |
| 463 | + |
| 464 | +def test_w024_message_mentions_join_or_grouping(): |
| 465 | + rule = SelectDistinctSuspicious() |
| 466 | + finding = _stmt( |
| 467 | + rule, |
| 468 | + "SELECT DISTINCT a FROM x JOIN y ON x.id = y.id;", |
| 469 | + ) |
| 470 | + assert finding is not None |
| 471 | + msg = finding.message.lower() |
| 472 | + assert "join" in msg or "grouping" in msg |
0 commit comments