diff --git a/docs/core.html b/docs/core.html index 0a26773..70db96c 100644 --- a/docs/core.html +++ b/docs/core.html @@ -2003,6 +2003,53 @@

reformat_too_long_line<
+
+
+
assert_and_print(
+    reformat_too_long_line(
+        "select case when asdf in ('1234', '2345', '1234', '2354', '2345', '2352345', '245623462', '124123412', '124312341234') then 1",
+        max_len=40
+    ),
+"""
+select case when asdf in ('1234', '2345',
+                          '1234', '2354',
+                          '2345', '2352345',
+                          '245623462', '124123412',
+                          '124312341234') then 1
+""".strip()
+)
+
+ +
+
+
+ +
+
+ +
+ +
+
select case when asdf in ('1234', '2345',
+                          '1234', '2354',
+                          '2345', '2352345',
+                          '245623462', '124123412',
+                          '124312341234') then 1
+
+
+
+ +
+
+ +
+ {% endraw %} + + {% raw %} + + {% endraw %} @@ -3770,20 +3842,6 @@

format_on -
- -
- -
-
        on a.asdf = b.asdf /* some comment */
-
-
-
- -
- - {% endraw %} @@ -3807,21 +3865,6 @@

format_on -
- -
- -
-
        on a.asdf = b.asdf and
-           a.qwer = b.qwer
-
-
-
- -
- - {% endraw %} @@ -3846,22 +3889,6 @@

format_on -
- -
- -
-
        on a.asdf = b.asdf and
-           a.qwer = b.qwer or
-           a.qwer2 = b.qwer2
-
-
-
- -
- - {% endraw %} @@ -3893,22 +3920,6 @@

format_on -
- -
- -
-
        on a.asdf = b.asdf and -- some comment
-           a.qwer = b.qwer or /* another comment */
-           a.qwer2 = b.qwer2
-
-
-
- -
- - {% endraw %} @@ -3968,22 +3979,6 @@

format_where -
- -
- -
-
WHERE  asdf = 1
-   and qwer = 1
-    or blabla = 'asdf'
-
-
-
- -
- - {% endraw %} @@ -4005,22 +4000,6 @@

format_where -
- -
- -
-
WHERE  asdf = 1 -- and some comment
-   and qwer = 1
-    or blabla = 'asdf'
-
-
-
- -
- - {% endraw %} @@ -4042,22 +4021,6 @@

format_where -
- -
- -
-
WHERE  asdf = 1 -- and some comment
-   and qwer = 1
-    or blabla = 'asdf'
-
-
-
- -
- - {% endraw %} @@ -4118,21 +4081,6 @@

format_statement_line -
-
- -
- -
-
select asdf,
-       qwer
-
-
-
- -
-
- {% endraw %} @@ -4153,20 +4101,6 @@

format_statement_line -
-
- -
- -
-
    left join table1 as abc
-
-
-
- -
-
- {% endraw %} @@ -4191,23 +4125,6 @@

format_statement_line -
-
- -
- -
-
where  asdf = 1
-   and qwer = 'things'
-   and blabla = 0
-    or stuff = -1
-
-
-
- -
-
- {% endraw %} @@ -4264,22 +4181,6 @@

format_statements -
-
- -
- -
-
select asdf,
-       qwer
-from   table1
-
-
-
- -
-
- {% endraw %} @@ -4386,23 +4287,6 @@

add_semicolon -
- -
- -
-
CREATE OR REPLACE TABLE my_table AS
-SELECT asdf,
-       qwer
-FROM   table1; /* something */
-
-
-
- -
- - {% endraw %} @@ -4433,23 +4317,6 @@

add_semicolon -
- -
- -
-
CREATE OR REPLACE TABLE my_table AS
-SELECT asdf,
-       qwer
-FROM   table1; -- some thing
-
-
-
- -
- - {% endraw %} @@ -4480,23 +4347,6 @@

add_semicolon -
- -
- -
-
CREATE OR REPLACE TABLE my_table AS
-SELECT asdf,
-       qwer
-FROM   table1;
-
-
-
- -
- - {% endraw %} @@ -4556,45 +4406,6 @@

format_simple_sql -
-
- -
- -
-
CREATE OR REPLACE TABLE mytable AS -- Mytable example
-/* multi line
-   comment */
-SELECT a.asdf,
-       -- some line comment
-       b.qwer, -- some comment here
-       /* and here is a line comment inside select */
-       substr(c.asdf, 1, 2) as substr_asdf,
-       /* some commenT
-          there */
-       case when a.asdf = 1 then 'b' /* here a case comment */
-            when b.qwer = 2 then 'c'
-            else 'd' end as new_field, -- Some comment
-       /* and here some inline comment */
-       b.asdf2
-FROM   table1 as a
-    LEFT JOIN table2 as b -- and here a comment
-        ON a.asdf = b.asdf /* joiN this way */
-    INNER JOIN table3 as c
-        ON a.asdf = c.asdf and
-           a.qwer = b.qwer
-WHERE  a.asdf = 1 -- comment this
-   and b.qwer = 2
-   and a.asdf <= 1 --comment that
-    or b.qwer >= 5
-GROUP BY a.asdf
-
-
-
- -
-
- {% endraw %} @@ -4629,24 +4440,6 @@

format_simple_sql -
-
- -
- -
-
CREATE OR REPLACE TABLE first_table AS -- my first table
-SELECT car_id,
-       avg(price) as avg_price
-FROM   first_view
-GROUP BY car_id
-
-
-
- -
-
- {% endraw %} @@ -4782,32 +4575,6 @@

format_sql -
- -
- -
-
SELECT asdf,
-       cast(qwer as numeric), -- some comment
-       substr(qwer1, 3, 2) as substr_qwer /* some field */
-FROM   (SELECT asdf,
-               qwer /* some nice field */
-        FROM   table1
-        WHERE  asdf = 1) as a
-    LEFT JOIN (SELECT asdf,
-                      qwer2
-               FROM   table2
-               WHERE  qwer2 = 1) as b
-        ON a.asdf = b.asdf
-WHERE  qwer1 >= 0
-
-
-
- -
- - {% endraw %} @@ -4835,45 +4602,6 @@

format_sql -
- -
- -
-
CREATE OR REPLACE TABLE mytable AS -- Mytable example
-/* multi line
-   comment */
-SELECT a.asdf,
-       -- some line comment
-       b.qwer, -- some comment here
-       /* and here is a line comment inside select */
-       substr(c.asdf, 1, 2) as substr_asdf,
-       /* some commenT
-          there */
-       case when a.asdf = 1 then 'b' /* here a case comment */
-            when b.qwer = 2 then 'c'
-            else 'd' end as new_field, -- Some comment
-       /* and here some inline comment */
-       b.asdf2
-FROM   table1 as a
-    LEFT JOIN table2 as b -- and here a comment
-        ON a.asdf = b.asdf /* joiN this way */
-    INNER JOIN table3 as c
-        ON a.asdf = c.asdf and
-           a.qwer = b.qwer
-WHERE  a.asdf = 1 -- comment this
-   and b.qwer = 2
-   and a.asdf <= 1 --comment that
-    or b.qwer >= 5
-GROUP BY a.asdf
-
-
-
- -
- - {% endraw %} @@ -4954,32 +4682,6 @@

Nested subqueries -
- -
- -
-
SELECT asdf,
-       qwer
-FROM   (SELECT a.asdf,
-               lead(a.substr_qwer) OVER (PARTITION BY a.asdf,
-                                                      asdf2
-                                         ORDER BY qwer) as lead_qwerty
-        FROM   (SELECT asdf,
-                       substr(qwer, 3, 2) as substr_qwer
-                FROM   table2) as a
-            INNER JOIN (SELECT asdf,
-                               qwer
-                        FROM   table3) as b
-                ON a.qwer = b.qwer)
-
-
-
- -
- - {% endraw %} @@ -5016,24 +4718,6 @@

With SELECT DISTINCT -
- -
- -
-
SELECT asdf,
-       qwer
-FROM   (SELECT DISTINCT asdf,
-                        qwer
-        FROM   table1)
-
-
-
- -
- - {% endraw %} @@ -5114,29 +4798,6 @@

More convoluted nested subquery -
-
- -
- -
-
SELECT asdf
-FROM   (SELECT asdf,
-               qwer /* some comment */
-        FROM   (SELECT a.asdf,
-                       b.qwer --some comment
-                FROM   (SELECT asdf
-                        FROM   table1) as a
-                    RIGHT JOIN (SELECT qwer
-                                FROM   table2) as b
-                        ON a.asdf = b.asdf))
-
-
-
- -
-
- {% endraw %} @@ -5175,28 +4836,6 @@

More convoluted nested subquery -
-
- -
- -
-
SELECT asdf,
-       qwer
-FROM   table1
-UNION
-SELECT qwer,
-       asdf,
-       asdf2
-FROM   table3
-WHERE  asdf2 >= 2
-
-
-
- -
-
- {% endraw %} @@ -5237,25 +4876,6 @@

More convoluted nested subquery -
-
- -
- -
-
CREATE OR REPLACE TABLE asdf AS
-SELECT asdf,
-       qwer OVER (PARTITION BY asdf,
-                               qwer
-                  ORDER BY qwerty)
-FROM   table1
-
-
-
- -
-
- {% endraw %} @@ -5301,28 +4921,6 @@

More convoluted nested subquery -
-
- -
- -
-
CREATE OR REPLACE TABLE asdf AS
-SELECT asdf,
-       case when asdf in (123412341234, 12341234123412, 123412341234, 512351235132, 123412341,
-                          1234) then 1
-            else 0 end as qwerty,
-       qwer OVER (PARTITION BY asdf,
-                               qwer
-                  ORDER BY qwerty)
-FROM   table1
-
-
-
- -
-
- {% endraw %} diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 4622c04..3fdfbac 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -1406,9 +1406,9 @@ " first_append = True\n", " lcol = 0 # line code column\n", " for i, s in enumerate(li):\n", - " if \"in (\" in li[i-4:i] and not quote_open1 and not quote_open2 and not in_in:\n", + " if \"in (\" in li[i-3:i+1] and not quote_open1 and not quote_open2 and not in_in:\n", " in_in = True\n", - " indentation = i\n", + " indentation = i + 1\n", " elif s == \")\" and not quote_open1 and not quote_open2 and in_in:\n", " in_in = False\n", " elif s == \",\" and in_in and not quote_open1 and not quote_open2:\n", @@ -1575,6 +1575,39 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "select case when asdf in ('1234', '2345',\n", + " '1234', '2354',\n", + " '2345', '2352345',\n", + " '245623462', '124123412',\n", + " '124312341234') then 1\n" + ] + } + ], + "source": [ + "assert_and_print(\n", + " reformat_too_long_line(\n", + " \"select case when asdf in ('1234', '2345', '1234', '2354', '2345', '2352345', '245623462', '124123412', '124312341234') then 1\",\n", + " max_len=40\n", + " ),\n", + "\"\"\"\n", + "select case when asdf in ('1234', '2345',\n", + " '1234', '2354',\n", + " '2345', '2352345',\n", + " '245623462', '124123412',\n", + " '124312341234') then 1\n", + "\"\"\".strip()\n", + ")" + ] + }, { "cell_type": "code", "execution_count": null, @@ -2703,6 +2736,45 @@ ")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Long line with `in (...)` and quotes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "select asdf,\n", + " case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234',\n", + " '12341234', '1234') then 1\n", + " else 0 end as qwer,\n", + " asdf2\n" + ] + } + ], + "source": [ + "assert_and_print(\n", + " format_select(\n", + " \"select asdf, case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234', '12341234', '1234') then 1 else 0 end as qwer, asdf2\"\n", + " ),\n", + "\"\"\"\n", + "select asdf,\n", + " case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234',\n", + " '12341234', '1234') then 1\n", + " else 0 end as qwer,\n", + " asdf2\n", + "\"\"\".strip()\n", + ")" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/sql_formatter/core.py b/sql_formatter/core.py index c4e98ea..0826d55 100644 --- a/sql_formatter/core.py +++ b/sql_formatter/core.py @@ -232,9 +232,9 @@ def reformat_too_long_line(li, max_len=82): first_append = True lcol = 0 # line code column for i, s in enumerate(li): - if "in (" in li[i-4:i] and not quote_open1 and not quote_open2 and not in_in: + if "in (" in li[i-3:i+1] and not quote_open1 and not quote_open2 and not in_in: in_in = True - indentation = i + indentation = i + 1 elif s == ")" and not quote_open1 and not quote_open2 and in_in: in_in = False elif s == "," and in_in and not quote_open1 and not quote_open2: