|
| 1 | +"""Dropping the comments that trail a statement. |
| 2 | +
|
| 3 | +sqlparse cannot do this for PostgreSQL: it follows MySQL and reads ``#`` as a |
| 4 | +comment marker, while in PostgreSQL ``#`` is the bitwise XOR operator. That is |
| 5 | +dbcli/pgcli issue #1646 and andialbrecht/sqlparse issue #539: `select 17 # 5` |
| 6 | +silently returns 17 instead of 20. |
| 7 | +""" |
| 8 | + |
| 9 | +from pgcli.packages.parseutils import strip_trailing_comments |
| 10 | + |
| 11 | + |
| 12 | +def test_hash_is_an_operator_not_a_comment(): |
| 13 | + """The reported bug: everything after # was dropped.""" |
| 14 | + assert strip_trailing_comments("select 17 # 5") == "select 17 # 5" |
| 15 | + assert strip_trailing_comments("select 17 # 5;") == "select 17 # 5;" |
| 16 | + |
| 17 | + |
| 18 | +def test_hash_keeps_its_own_trailing_comment_separate(): |
| 19 | + assert strip_trailing_comments("select 17 # 5; -- note") == "select 17 # 5;" |
| 20 | + |
| 21 | + |
| 22 | +def test_trailing_line_comment_is_dropped(): |
| 23 | + """What #1559 fixed: rstrip(';') needs the comment gone first.""" |
| 24 | + assert strip_trailing_comments("vacuum freeze verbose t; -- 82% towards emergency") == "vacuum freeze verbose t;" |
| 25 | + |
| 26 | + |
| 27 | +def test_trailing_block_comment_is_dropped(): |
| 28 | + assert strip_trailing_comments("select 1; /* done */") == "select 1;" |
| 29 | + |
| 30 | + |
| 31 | +def test_nested_block_comments(): |
| 32 | + """PostgreSQL block comments nest, unlike C.""" |
| 33 | + assert strip_trailing_comments("select 1; /* a /* b */ c */") == "select 1;" |
| 34 | + |
| 35 | + |
| 36 | +def test_several_trailing_comments(): |
| 37 | + assert strip_trailing_comments("select 1;\n-- one\n/* two */\n-- three") == "select 1;" |
| 38 | + |
| 39 | + |
| 40 | +def test_comments_in_the_middle_are_kept(): |
| 41 | + """Only trailing comments go: the server should see the rest as written.""" |
| 42 | + for sql in ("select 1, /* keep */ 2;", "select 1\n-- keep\n, 2;"): |
| 43 | + assert strip_trailing_comments(sql) == sql |
| 44 | + |
| 45 | + |
| 46 | +def test_comment_markers_inside_strings_are_text(): |
| 47 | + for sql in ("select '-- not a comment';", "select 'a /* b */ c';", "select 'a # b';"): |
| 48 | + assert strip_trailing_comments(sql) == sql |
| 49 | + |
| 50 | + |
| 51 | +def test_doubled_quotes_inside_strings(): |
| 52 | + sql = "select 'it''s -- fine';" |
| 53 | + assert strip_trailing_comments(sql) == sql |
| 54 | + |
| 55 | + |
| 56 | +def test_quoted_identifiers(): |
| 57 | + sql = 'select 1 as "-- odd name";' |
| 58 | + assert strip_trailing_comments(sql) == sql |
| 59 | + |
| 60 | + |
| 61 | +def test_dollar_quoted_body_is_left_alone(): |
| 62 | + """A function body holds its own comments and must survive intact.""" |
| 63 | + sql = "do $$ begin -- inside\n perform 1; end $$;" |
| 64 | + assert strip_trailing_comments(sql) == sql |
| 65 | + tagged = "create function f() returns int as $body$ select 1; -- inner\n$body$ language sql;" |
| 66 | + assert strip_trailing_comments(tagged) == tagged |
| 67 | + |
| 68 | + |
| 69 | +def test_only_a_comment_leaves_nothing(): |
| 70 | + assert strip_trailing_comments("-- just a note") == "" |
| 71 | + assert strip_trailing_comments("/* just a note */") == "" |
| 72 | + |
| 73 | + |
| 74 | +def test_empty_and_whitespace(): |
| 75 | + assert strip_trailing_comments("") == "" |
| 76 | + assert strip_trailing_comments(" \n ") == "" |
| 77 | + |
| 78 | + |
| 79 | +def test_unterminated_block_comment_is_kept(): |
| 80 | + """PostgreSQL rejects it ("unterminated /* comment"). Dropping it would run |
| 81 | + a different statement and return an answer where the server gave an error: |
| 82 | + `select 4/*2` would quietly say 4.""" |
| 83 | + for sql in ("select 1; /* never closed", "select 4/*2"): |
| 84 | + assert strip_trailing_comments(sql) == sql |
| 85 | + |
| 86 | + |
| 87 | +def test_unterminated_string_does_not_hang(): |
| 88 | + assert strip_trailing_comments("select 'never closed") == "select 'never closed" |
| 89 | + |
| 90 | + |
| 91 | +# --- PostgreSQL operators that look like comment markers ------------------- |
| 92 | +# |
| 93 | +# Every expectation below was run against PostgreSQL 17 first; the value in the |
| 94 | +# comment is what the server answers for that expression. |
| 95 | + |
| 96 | + |
| 97 | +def test_hash_operators_are_left_alone(): |
| 98 | + """# starts several operators, none of them a comment.""" |
| 99 | + for sql in ( |
| 100 | + "select 17 # 5", # 20, bitwise XOR |
| 101 | + """select '{"a":{"b":7}}'::jsonb #> '{a,b}'""", # 7, path extract |
| 102 | + """select '{"a":1}'::jsonb #>> '{a}'""", # 1, path extract as text |
| 103 | + """select '{"a":1,"b":2}'::jsonb #- '{a}'""", # {"b": 2}, delete path |
| 104 | + "select point '(0,0)' ## lseg '((1,1),(2,2))'", # (1,1), closest point |
| 105 | + "select box '((0,0),(2,2))' # box '((1,1),(3,3))'", # intersection |
| 106 | + "select # path '((1,1),(2,2),(3,3))'", # 3, number of points |
| 107 | + ): |
| 108 | + assert strip_trailing_comments(sql) == sql, sql |
| 109 | + |
| 110 | + |
| 111 | +def test_operators_built_from_dashes_and_slashes(): |
| 112 | + for sql in ( |
| 113 | + "select 5 - -3", # 8, unary minus after an operator |
| 114 | + "select @ -5", # 5, absolute value |
| 115 | + """select '{"a":1}'::json ->> 'a'""", # 1 |
| 116 | + "select 1 << 3", # 8 |
| 117 | + "select 'abc' ~ 'b'", # t, regex match |
| 118 | + """select '{"a":1}'::jsonb @> '{"a":1}'""", # t, contains |
| 119 | + "select 'a' || 'b'", # ab |
| 120 | + ): |
| 121 | + assert strip_trailing_comments(sql) == sql, sql |
| 122 | + |
| 123 | + |
| 124 | +def test_double_dash_without_spaces_is_a_comment_to_postgres_too(): |
| 125 | + """`select 5--3` answers 5, not 8: the server reads --3 as a comment, so |
| 126 | + dropping it here matches what the server would have done.""" |
| 127 | + assert strip_trailing_comments("select 5--3") == "select 5" |
| 128 | + |
| 129 | + |
| 130 | +def test_hash_inside_a_trailing_comment_is_still_a_comment(): |
| 131 | + assert strip_trailing_comments("select 1; -- see issue #1646") == "select 1;" |
0 commit comments