Repository: tobymao/sqlglot

Issue:
Formatting with long condition chain in `CASE`
Hi, this is a feature request. I have an SQL query resembling 

```
SELECT
a, 
CASE WHEN a = 1 OR a = 4 OR (a > 6 AND a < 10) OR a % 21 = 1 OR a = 124908 or a = 49876512 or a = 1097589162340 or a = 120498105 or a = 222229929 THEN 'Yes'
ELSE 'No'
END
FROM fruits
```

Even though I specify `pretty=True`, the formatted result is:

```
SELECT
  a,
  CASE
    WHEN a = 1 OR a = 4 OR (a > 6 AND a < 10) OR a % 21 = 1 OR a = 124908 OR a = 49876512 OR a = 1097589162340 OR a = 120498105 OR a = 222229929 
THEN 'Yes'
    ELSE 'No'
  END
FROM fruits
```
Here I would like the condition to break into several lines rather than have one massive line.

Is it possible to customise the formatting in this case?


Discussion:
this would be possible to implement now since sqlglot has better understanding of predicates would need to figure out the formatting rule for this

Resolving patch:
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py
--- a/sqlglot/expressions.py
+++ b/sqlglot/expressions.py
@@ -219,7 +219,7 @@ def unnest_operands(self):
         """
         return [arg.unnest() for arg in self.args.values() if arg]
 
-    def flatten(self):
+    def flatten(self, unnest=True):
         """
         Returns a generator which yields child nodes who's parents are the same class.
 
@@ -229,7 +229,7 @@ def flatten(self):
             prune=lambda n, p, *_: p and not isinstance(n, self.__class__)
         ):
             if not isinstance(node, self.__class__):
-                yield node.unnest()
+                yield node.unnest() if unnest else node
 
     def __str__(self):
         return self.sql()
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -61,7 +61,6 @@ class Generator:
         "unsupported_level",
         "unsupported_messages",
         "_indent",
-        "_level",
     )
 
     def __init__(
@@ -77,7 +76,7 @@ def __init__(
         quote=None,
         escape=None,
         pad=2,
-        indent=4,
+        indent=2,
         index_offset=0,
         unsupported_level=ErrorLevel.WARN,
     ):
@@ -100,7 +99,6 @@ def __init__(
         self.unsupported_level = unsupported_level
         self.unsupported_messages = []
         self._indent = indent
-        self._level = 0
 
     def generate(self, expression):
         """
@@ -128,17 +126,11 @@ def generate(self, expression):
     def unsupported(self, message):
         self.unsupported_messages.append(message)
 
-    def indent(self, sql, level=None, pad=0):
-        level = self._level if level is None else level
-        if self.pretty:
-            sql = f"{' ' * (level * self._indent + pad)}{sql}"
-        return sql
-
     def sep(self, sep=" "):
         return f"{sep.strip()}\n" if self.pretty else sep
 
-    def seg(self, sql, sep=" ", level=None, pad=0):
-        return f"{self.sep(sep)}{self.indent(sql, level=level, pad=pad)}"
+    def seg(self, sql, sep=" "):
+        return f"{self.sep(sep)}{sql}"
 
     def properties(self, name, expression):
         if expression.args["expressions"]:
@@ -146,22 +138,15 @@ def properties(self, name, expression):
         return ""
 
     def wrap(self, expression):
-        self._level += 1
         this_sql = self.indent(
             self.sql(expression)
             if isinstance(expression, exp.Select)
-            else self.sql(expression, "this")
+            else self.sql(expression, "this"),
+            level=1,
+            pad=0,
         )
-        self._level -= 1
         return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"
 
-    def no_format(self, func):
-        original = self.pretty
-        self.pretty = False
-        result = func()
-        self.pretty = original
-        return result
-
     def no_identify(self, func):
         original = self.identify
         self.identify = False
@@ -169,12 +154,16 @@ def no_identify(self, func):
         self.identify = original
         return result
 
-    def indent_newlines(self, sql, skip_first=False):
+    def indent(self, sql, level=0, pad=None, skip_first=False):
         if not self.pretty:
             return sql
 
+        pad = self.pad if pad is None else pad
+
         return "\n".join(
-            line if skip_first and i == 0 else self.indent(line, pad=self.pad)
+            line
+            if skip_first and i == 0
+            else f"{' ' * (level * self._indent + pad)}{line}"
             for i, line in enumerate(sql.split("\n"))
         )
 
@@ -299,7 +288,7 @@ def create_sql(self, expression):
     def prepend_ctes(self, expression, sql):
         with_ = self.sql(expression, "with")
         if with_:
-            sql = f"{with_}{self.sep()}{self.indent(sql)}"
+            sql = f"{with_}{self.sep()}{sql}"
         return sql
 
     def with_sql(self, expression):
@@ -461,7 +450,7 @@ def group_sql(self, expression):
         return self.op_expressions("GROUP BY", expression)
 
     def having_sql(self, expression):
-        this = self.indent_newlines(self.sql(expression, "this"))
+        this = self.indent(self.sql(expression, "this"))
         return f"{self.seg('HAVING')}{self.sep()}{this}"
 
     def join_sql(self, expression):
@@ -471,8 +460,9 @@ def join_sql(self, expression):
         on_sql = self.sql(expression, "on")
 
         if on_sql:
-            on_sql = self.indent_newlines(on_sql, skip_first=True)
-            on_sql = f"{self.seg('ON', pad=self.pad)} {on_sql}"
+            on_sql = self.indent(on_sql, skip_first=True)
+            space = self.seg(" " * self.pad) if self.pretty else " "
+            on_sql = f"{space}ON {on_sql}"
 
         expression_sql = self.sql(expression, "expression")
         this_sql = self.sql(expression, "this")
@@ -562,7 +552,7 @@ def subquery_sql(self, expression):
         return f"({self.sql(expression, 'this')}){alias}"
 
     def qualify_sql(self, expression):
-        this = self.indent_newlines(self.sql(expression, "this"))
+        this = self.indent(self.sql(expression, "this"))
         return f"{self.seg('QUALIFY')}{self.sep()}{this}"
 
     def union_sql(self, expression):
@@ -583,7 +573,7 @@ def unnest_sql(self, expression):
         return f"UNNEST({args}){ordinality}{alias}"
 
     def where_sql(self, expression):
-        this = self.indent_newlines(self.sql(expression, "this"))
+        this = self.indent(self.sql(expression, "this"))
         return f"{self.seg('WHERE')}{self.sep()}{this}"
 
     def window_sql(self, expression):
@@ -631,25 +621,20 @@ def bracket_sql(self, expression):
         return f"{self.sql(expression, 'this')}[{expressions}]"
 
     def case_sql(self, expression):
-        pad = self.pad + 2
-
-        this = self.sql(expression, "this")
+        this = self.indent(self.sql(expression, "this"), skip_first=True)
         this = f" {this}" if this else ""
+        ifs = []
 
-        ifs = [
-            f"WHEN {self.sql(e, 'this')} THEN {self.sql(e, 'true')}"
-            for e in expression.args["ifs"]
-        ]
+        for e in expression.args["ifs"]:
+            ifs.append(f"WHEN {self.sql(e, 'this')}")
+            ifs.append(f"THEN {self.sql(e, 'true')}")
 
         if expression.args.get("default") is not None:
             ifs.append(f"ELSE {self.sql(expression, 'default')}")
 
-        original = self.pretty
-        self.pretty = self.configured_pretty
-        ifs = "".join(self.seg(e, pad=pad) for e in ifs)
-        case = f"CASE{this}{ifs}{self.seg('END', pad=self.pad)}"
-        self.pretty = original
-        return case
+        ifs = "".join(self.seg(self.indent(e, skip_first=True)) for e in ifs)
+        statement = f"CASE{this}{ifs}{self.seg('END')}"
+        return statement
 
     def decimal_sql(self, expression):
         args = ", ".join(
@@ -687,7 +672,8 @@ def anonymous_sql(self, expression):
     def paren_sql(self, expression):
         if isinstance(expression.unnest(), exp.Select):
             return self.wrap(expression)
-        return self.no_format(lambda: f"({self.sql(expression, 'this')})")
+        sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
+        return f"({sql}{self.seg(')', sep='')}"
 
     def neg_sql(self, expression):
         return f"-{self.sql(expression, 'this')}"
@@ -707,7 +693,13 @@ def add_sql(self, expression):
         return self.binary(expression, "+")
 
     def and_sql(self, expression):
-        return self.binary(expression, "AND", newline=self.pretty)
+        return self.connector_sql(expression, "AND")
+
+    def connector_sql(self, expression, op):
+        if not self.pretty:
+            return self.binary(expression, op)
+
+        return f"\n{op} ".join(self.sql(e) for e in expression.flatten(unnest=False))
 
     def bitwiseand_sql(self, expression):
         return self.binary(expression, "&")
@@ -794,7 +786,7 @@ def neq_sql(self, expression):
         return self.binary(expression, "<>")
 
     def or_sql(self, expression):
-        return self.binary(expression, "OR", newline=self.pretty)
+        return self.connector_sql(expression, "OR")
 
     def sub_sql(self, expression):
         return self.binary(expression, "-")
@@ -804,9 +796,10 @@ def trycast_sql(self, expression):
             f"TRY_CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')})"
         )
 
-    def binary(self, expression, op, newline=False):
-        sep = "\n" if newline else " "
-        return f"{self.sql(expression, 'this')}{sep}{op} {self.sql(expression, 'expression')}"
+    def binary(self, expression, op):
+        return (
+            f"{self.sql(expression, 'this')} {op} {self.sql(expression, 'expression')}"
+        )
 
     def function_fallback_sql(self, expression):
         args = []
@@ -823,19 +816,14 @@ def format_time(self, expression):
             self.sql(expression, "format"), self.time_mapping, self.time_trie
         )
 
-    def expressions(self, expression, flat=False, pad=0):
+    def expressions(self, expression, flat=False):
         # pylint: disable=cell-var-from-loop
         expressions = expression.args.get("expressions") or []
         if flat:
             return ", ".join(self.sql(e) for e in expressions)
 
-        return self.sep(", ").join(
-            self.indent(
-                f"{'  ' if self.pretty else ''}{self.no_format(lambda: self.sql(e))}",
-                pad=pad,
-            )
-            for e in expressions
-        )
+        expressions = self.sep(", ").join(self.sql(e) for e in expressions)
+        return self.indent(expressions, skip_first=False)
 
     def op_expressions(self, op, expression, flat=False):
         expressions_sql = self.expressions(expression, flat=flat)
@@ -846,5 +834,5 @@ def op_expressions(self, op, expression, flat=False):
     def set_operation(self, expression, op):
         this = self.sql(expression, "this")
         op = self.seg(op)
-        expression = self.indent(self.sql(expression, "expression"), pad=0)
+        expression = self.sql(expression, "expression")
         return f"{this}{op}{self.sep()}{expression}"


Test patch:
diff --git a/tests/fixtures/optimizer/eliminate_subqueries.sql b/tests/fixtures/optimizer/eliminate_subqueries.sql
--- a/tests/fixtures/optimizer/eliminate_subqueries.sql
+++ b/tests/fixtures/optimizer/eliminate_subqueries.sql
@@ -2,9 +2,9 @@ SELECT 1 AS x, 2 AS y
 UNION ALL
 SELECT 1 AS x, 2 AS y;
 WITH _e_0 AS (
-    SELECT
-      1 AS x,
-      2 AS y
+  SELECT
+    1 AS x,
+    2 AS y
 )
 SELECT
   *
@@ -29,11 +29,11 @@ JOIN (
 ) AS y
 ON x.id = y.id;
 WITH _e_0 AS (
-    SELECT
-      *
-    FROM x AS x
-    JOIN y AS y
-      ON x.id = y.id
+  SELECT
+    *
+  FROM x AS x
+  JOIN y AS y
+    ON x.id = y.id
 )
 SELECT
   x.id
diff --git a/tests/fixtures/optimizer/optimizer.sql b/tests/fixtures/optimizer/optimizer.sql
--- a/tests/fixtures/optimizer/optimizer.sql
+++ b/tests/fixtures/optimizer/optimizer.sql
@@ -10,33 +10,33 @@ SELECT
   "d"."a" AS "a",
   SUM("d"."b") AS "_col_1"
 FROM (
+  SELECT
+    "x"."a" AS "a",
+    "y"."b" AS "b"
+  FROM (
     SELECT
-      "x"."a" AS "a",
+      "x"."a" AS "a"
+    FROM "x" AS "x"
+    WHERE
+      "x"."a" > 1
+  ) AS "x"
+  JOIN (
+    SELECT
+      MAX("y"."b") AS "_col_0",
+      "y"."a"
+    FROM "y" AS "y"
+    GROUP BY
+      "y"."a"
+  ) AS "_d_0"
+    ON "_d_0"."_col_0" >= 0
+    AND "_d_0"."a" = "x"."a"
+  JOIN (
+    SELECT
+      "y"."a" AS "a",
       "y"."b" AS "b"
-    FROM (
-        SELECT
-          "x"."a" AS "a"
-        FROM "x" AS "x"
-        WHERE
-          "x"."a" > 1
-    ) AS "x"
-    JOIN (
-        SELECT
-          MAX("y"."b") AS "_col_0",
-          "y"."a"
-        FROM "y" AS "y"
-        GROUP BY
-          "y"."a"
-    ) AS "_d_0"
-      ON "_d_0"."_col_0" >= 0
-      AND "_d_0"."a" = "x"."a"
-    JOIN (
-        SELECT
-          "y"."a" AS "a",
-          "y"."b" AS "b"
-        FROM "y" AS "y"
-    ) AS "y"
-      ON "x"."a" = "y"."a"
+    FROM "y" AS "y"
+  ) AS "y"
+    ON "x"."a" = "y"."a"
 ) AS "d"
 GROUP BY
   "d"."a";
diff --git a/tests/fixtures/optimizer/tpc-h/tpc-h.sql b/tests/fixtures/optimizer/tpc-h/tpc-h.sql
--- a/tests/fixtures/optimizer/tpc-h/tpc-h.sql
+++ b/tests/fixtures/optimizer/tpc-h/tpc-h.sql
@@ -27,8 +27,14 @@ SELECT
   "lineitem"."l_linestatus" AS "l_linestatus",
   SUM("lineitem"."l_quantity") AS "sum_qty",
   SUM("lineitem"."l_extendedprice") AS "sum_base_price",
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "sum_disc_price",
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount") * (1 + "lineitem"."l_tax")) AS "sum_charge",
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "sum_disc_price",
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  ) * (
+    1 + "lineitem"."l_tax"
+  )) AS "sum_charge",
   AVG("lineitem"."l_quantity") AS "avg_qty",
   AVG("lineitem"."l_extendedprice") AS "avg_price",
   AVG("lineitem"."l_discount") AS "avg_disc",
@@ -92,18 +98,18 @@ order by
 limit
    100;
 WITH "_e_0" AS (
-    SELECT
-      "partsupp"."ps_partkey" AS "ps_partkey",
-      "partsupp"."ps_suppkey" AS "ps_suppkey",
-      "partsupp"."ps_supplycost" AS "ps_supplycost"
-    FROM "partsupp" AS "partsupp"
+  SELECT
+    "partsupp"."ps_partkey" AS "ps_partkey",
+    "partsupp"."ps_suppkey" AS "ps_suppkey",
+    "partsupp"."ps_supplycost" AS "ps_supplycost"
+  FROM "partsupp" AS "partsupp"
 ), "_e_1" AS (
-    SELECT
-      "region"."r_regionkey" AS "r_regionkey",
-      "region"."r_name" AS "r_name"
-    FROM "region" AS "region"
-    WHERE
-      "region"."r_name" = 'EUROPE'
+  SELECT
+    "region"."r_regionkey" AS "r_regionkey",
+    "region"."r_name" AS "r_name"
+  FROM "region" AS "region"
+  WHERE
+    "region"."r_name" = 'EUROPE'
 )
 SELECT
   "supplier"."s_acctbal" AS "s_acctbal",
@@ -115,63 +121,63 @@ SELECT
   "supplier"."s_phone" AS "s_phone",
   "supplier"."s_comment" AS "s_comment"
 FROM (
-    SELECT
-      "part"."p_partkey" AS "p_partkey",
-      "part"."p_mfgr" AS "p_mfgr",
-      "part"."p_type" AS "p_type",
-      "part"."p_size" AS "p_size"
-    FROM "part" AS "part"
-    WHERE
-      "part"."p_size" = 15
-      AND "part"."p_type" LIKE '%BRASS'
+  SELECT
+    "part"."p_partkey" AS "p_partkey",
+    "part"."p_mfgr" AS "p_mfgr",
+    "part"."p_type" AS "p_type",
+    "part"."p_size" AS "p_size"
+  FROM "part" AS "part"
+  WHERE
+    "part"."p_size" = 15
+    AND "part"."p_type" LIKE '%BRASS'
 ) AS "part"
 JOIN "_e_0" AS "partsupp"
   ON "part"."p_partkey" = "partsupp"."ps_partkey"
 JOIN (
+  SELECT
+    MIN("partsupp"."ps_supplycost") AS "_col_0",
+    "partsupp"."ps_partkey"
+  FROM "_e_0" AS "partsupp"
+  CROSS JOIN "_e_1" AS "region"
+  JOIN (
     SELECT
-      MIN("partsupp"."ps_supplycost") AS "_col_0",
-      "partsupp"."ps_partkey"
-    FROM "_e_0" AS "partsupp"
-    CROSS JOIN "_e_1" AS "region"
-    JOIN (
-        SELECT
-          "nation"."n_nationkey" AS "n_nationkey",
-          "nation"."n_regionkey" AS "n_regionkey"
-        FROM "nation" AS "nation"
-    ) AS "nation"
-      ON "nation"."n_regionkey" = "region"."r_regionkey"
-    JOIN (
-        SELECT
-          "supplier"."s_suppkey" AS "s_suppkey",
-          "supplier"."s_nationkey" AS "s_nationkey"
-        FROM "supplier" AS "supplier"
-    ) AS "supplier"
-      ON "supplier"."s_nationkey" = "nation"."n_nationkey"
-      AND "supplier"."s_suppkey" = "partsupp"."ps_suppkey"
-    GROUP BY
-      "partsupp"."ps_partkey"
+      "nation"."n_nationkey" AS "n_nationkey",
+      "nation"."n_regionkey" AS "n_regionkey"
+    FROM "nation" AS "nation"
+  ) AS "nation"
+    ON "nation"."n_regionkey" = "region"."r_regionkey"
+  JOIN (
+    SELECT
+      "supplier"."s_suppkey" AS "s_suppkey",
+      "supplier"."s_nationkey" AS "s_nationkey"
+    FROM "supplier" AS "supplier"
+  ) AS "supplier"
+    ON "supplier"."s_nationkey" = "nation"."n_nationkey"
+    AND "supplier"."s_suppkey" = "partsupp"."ps_suppkey"
+  GROUP BY
+    "partsupp"."ps_partkey"
 ) AS "_d_0"
   ON "_d_0"."ps_partkey" = "part"."p_partkey"
   AND "partsupp"."ps_supplycost" = "_d_0"."_col_0"
 CROSS JOIN "_e_1" AS "region"
 JOIN (
-    SELECT
-      "nation"."n_nationkey" AS "n_nationkey",
-      "nation"."n_name" AS "n_name",
-      "nation"."n_regionkey" AS "n_regionkey"
-    FROM "nation" AS "nation"
+  SELECT
+    "nation"."n_nationkey" AS "n_nationkey",
+    "nation"."n_name" AS "n_name",
+    "nation"."n_regionkey" AS "n_regionkey"
+  FROM "nation" AS "nation"
 ) AS "nation"
   ON "nation"."n_regionkey" = "region"."r_regionkey"
 JOIN (
-    SELECT
-      "supplier"."s_suppkey" AS "s_suppkey",
-      "supplier"."s_name" AS "s_name",
-      "supplier"."s_address" AS "s_address",
-      "supplier"."s_nationkey" AS "s_nationkey",
-      "supplier"."s_phone" AS "s_phone",
-      "supplier"."s_acctbal" AS "s_acctbal",
-      "supplier"."s_comment" AS "s_comment"
-    FROM "supplier" AS "supplier"
+  SELECT
+    "supplier"."s_suppkey" AS "s_suppkey",
+    "supplier"."s_name" AS "s_name",
+    "supplier"."s_address" AS "s_address",
+    "supplier"."s_nationkey" AS "s_nationkey",
+    "supplier"."s_phone" AS "s_phone",
+    "supplier"."s_acctbal" AS "s_acctbal",
+    "supplier"."s_comment" AS "s_comment"
+  FROM "supplier" AS "supplier"
 ) AS "supplier"
   ON "supplier"."s_nationkey" = "nation"."n_nationkey"
   AND "supplier"."s_suppkey" = "partsupp"."ps_suppkey"
@@ -211,37 +217,39 @@ limit
         10;
 SELECT
   "lineitem"."l_orderkey" AS "l_orderkey",
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "revenue",
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "revenue",
   CAST("orders"."o_orderdate" AS TEXT) AS "o_orderdate",
   "orders"."o_shippriority" AS "o_shippriority"
 FROM (
-    SELECT
-      "customer"."c_custkey" AS "c_custkey",
-      "customer"."c_mktsegment" AS "c_mktsegment"
-    FROM "customer" AS "customer"
-    WHERE
-      "customer"."c_mktsegment" = 'BUILDING'
+  SELECT
+    "customer"."c_custkey" AS "c_custkey",
+    "customer"."c_mktsegment" AS "c_mktsegment"
+  FROM "customer" AS "customer"
+  WHERE
+    "customer"."c_mktsegment" = 'BUILDING'
 ) AS "customer"
 JOIN (
-    SELECT
-      "orders"."o_orderkey" AS "o_orderkey",
-      "orders"."o_custkey" AS "o_custkey",
-      "orders"."o_orderdate" AS "o_orderdate",
-      "orders"."o_shippriority" AS "o_shippriority"
-    FROM "orders" AS "orders"
-    WHERE
-      "orders"."o_orderdate" < '1995-03-15'
+  SELECT
+    "orders"."o_orderkey" AS "o_orderkey",
+    "orders"."o_custkey" AS "o_custkey",
+    "orders"."o_orderdate" AS "o_orderdate",
+    "orders"."o_shippriority" AS "o_shippriority"
+  FROM "orders" AS "orders"
+  WHERE
+    "orders"."o_orderdate" < '1995-03-15'
 ) AS "orders"
   ON "customer"."c_custkey" = "orders"."o_custkey"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey" AS "l_orderkey",
-      "lineitem"."l_extendedprice" AS "l_extendedprice",
-      "lineitem"."l_discount" AS "l_discount",
-      "lineitem"."l_shipdate" AS "l_shipdate"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_shipdate" > '1995-03-15'
+  SELECT
+    "lineitem"."l_orderkey" AS "l_orderkey",
+    "lineitem"."l_extendedprice" AS "l_extendedprice",
+    "lineitem"."l_discount" AS "l_discount",
+    "lineitem"."l_shipdate" AS "l_shipdate"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_shipdate" > '1995-03-15'
 ) AS "lineitem"
   ON "lineitem"."l_orderkey" = "orders"."o_orderkey"
 GROUP BY
@@ -282,13 +290,13 @@ SELECT
   COUNT(*) AS "order_count"
 FROM "orders" AS "orders"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_commitdate" < "lineitem"."l_receiptdate"
-    GROUP BY
-      "lineitem"."l_orderkey"
+  SELECT
+    "lineitem"."l_orderkey"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_commitdate" < "lineitem"."l_receiptdate"
+  GROUP BY
+    "lineitem"."l_orderkey"
 ) AS "_d_0"
   ON "_d_0"."l_orderkey" = "orders"."o_orderkey"
 WHERE
@@ -328,55 +336,57 @@ order by
         revenue desc;
 SELECT
   "nation"."n_name" AS "n_name",
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "revenue"
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "revenue"
 FROM (
-    SELECT
-      "customer"."c_custkey" AS "c_custkey",
-      "customer"."c_nationkey" AS "c_nationkey"
-    FROM "customer" AS "customer"
+  SELECT
+    "customer"."c_custkey" AS "c_custkey",
+    "customer"."c_nationkey" AS "c_nationkey"
+  FROM "customer" AS "customer"
 ) AS "customer"
 JOIN (
-    SELECT
-      "orders"."o_orderkey" AS "o_orderkey",
-      "orders"."o_custkey" AS "o_custkey",
-      "orders"."o_orderdate" AS "o_orderdate"
-    FROM "orders" AS "orders"
-    WHERE
-      "orders"."o_orderdate" < CAST('1994-01-01' AS DATE) + INTERVAL '1' year
-      AND "orders"."o_orderdate" >= CAST('1994-01-01' AS DATE)
+  SELECT
+    "orders"."o_orderkey" AS "o_orderkey",
+    "orders"."o_custkey" AS "o_custkey",
+    "orders"."o_orderdate" AS "o_orderdate"
+  FROM "orders" AS "orders"
+  WHERE
+    "orders"."o_orderdate" < CAST('1994-01-01' AS DATE) + INTERVAL '1' year
+    AND "orders"."o_orderdate" >= CAST('1994-01-01' AS DATE)
 ) AS "orders"
   ON "customer"."c_custkey" = "orders"."o_custkey"
 CROSS JOIN (
-    SELECT
-      "region"."r_regionkey" AS "r_regionkey",
-      "region"."r_name" AS "r_name"
-    FROM "region" AS "region"
-    WHERE
-      "region"."r_name" = 'ASIA'
+  SELECT
+    "region"."r_regionkey" AS "r_regionkey",
+    "region"."r_name" AS "r_name"
+  FROM "region" AS "region"
+  WHERE
+    "region"."r_name" = 'ASIA'
 ) AS "region"
 JOIN (
-    SELECT
-      "nation"."n_nationkey" AS "n_nationkey",
-      "nation"."n_name" AS "n_name",
-      "nation"."n_regionkey" AS "n_regionkey"
-    FROM "nation" AS "nation"
+  SELECT
+    "nation"."n_nationkey" AS "n_nationkey",
+    "nation"."n_name" AS "n_name",
+    "nation"."n_regionkey" AS "n_regionkey"
+  FROM "nation" AS "nation"
 ) AS "nation"
   ON "nation"."n_regionkey" = "region"."r_regionkey"
 JOIN (
-    SELECT
-      "supplier"."s_suppkey" AS "s_suppkey",
-      "supplier"."s_nationkey" AS "s_nationkey"
-    FROM "supplier" AS "supplier"
+  SELECT
+    "supplier"."s_suppkey" AS "s_suppkey",
+    "supplier"."s_nationkey" AS "s_nationkey"
+  FROM "supplier" AS "supplier"
 ) AS "supplier"
   ON "customer"."c_nationkey" = "supplier"."s_nationkey"
   AND "supplier"."s_nationkey" = "nation"."n_nationkey"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey" AS "l_orderkey",
-      "lineitem"."l_suppkey" AS "l_suppkey",
-      "lineitem"."l_extendedprice" AS "l_extendedprice",
-      "lineitem"."l_discount" AS "l_discount"
-    FROM "lineitem" AS "lineitem"
+  SELECT
+    "lineitem"."l_orderkey" AS "l_orderkey",
+    "lineitem"."l_suppkey" AS "l_suppkey",
+    "lineitem"."l_extendedprice" AS "l_extendedprice",
+    "lineitem"."l_discount" AS "l_discount"
+  FROM "lineitem" AS "lineitem"
 ) AS "lineitem"
   ON "lineitem"."l_orderkey" = "orders"."o_orderkey"
   AND "lineitem"."l_suppkey" = "supplier"."s_suppkey"
@@ -449,13 +459,13 @@ order by
         cust_nation,
         l_year;
 WITH "_e_0" AS (
-    SELECT
-      "nation"."n_nationkey" AS "n_nationkey",
-      "nation"."n_name" AS "n_name"
-    FROM "nation" AS "nation"
-    WHERE
-      "nation"."n_name" = 'FRANCE'
-      OR "nation"."n_name" = 'GERMANY'
+  SELECT
+    "nation"."n_nationkey" AS "n_nationkey",
+    "nation"."n_name" AS "n_name"
+  FROM "nation" AS "nation"
+  WHERE
+    "nation"."n_name" = 'FRANCE'
+    OR "nation"."n_name" = 'GERMANY'
 )
 SELECT
   "shipping"."supp_nation" AS "supp_nation",
@@ -463,49 +473,57 @@ SELECT
   "shipping"."l_year" AS "l_year",
   SUM("shipping"."volume") AS "revenue"
 FROM (
+  SELECT
+    "n1"."n_name" AS "supp_nation",
+    "n2"."n_name" AS "cust_nation",
+    EXTRACT(year FROM "lineitem"."l_shipdate") AS "l_year",
+    "lineitem"."l_extendedprice" * (
+      1 - "lineitem"."l_discount"
+    ) AS "volume"
+  FROM (
     SELECT
-      "n1"."n_name" AS "supp_nation",
-      "n2"."n_name" AS "cust_nation",
-      EXTRACT(year FROM "lineitem"."l_shipdate") AS "l_year",
-      "lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount") AS "volume"
-    FROM (
-        SELECT
-          "supplier"."s_suppkey" AS "s_suppkey",
-          "supplier"."s_nationkey" AS "s_nationkey"
-        FROM "supplier" AS "supplier"
-    ) AS "supplier"
-    JOIN (
-        SELECT
-          "lineitem"."l_orderkey" AS "l_orderkey",
-          "lineitem"."l_suppkey" AS "l_suppkey",
-          "lineitem"."l_extendedprice" AS "l_extendedprice",
-          "lineitem"."l_discount" AS "l_discount",
-          "lineitem"."l_shipdate" AS "l_shipdate"
-        FROM "lineitem" AS "lineitem"
-        WHERE
-          "lineitem"."l_shipdate" BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
-    ) AS "lineitem"
-      ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
-    JOIN (
-        SELECT
-          "orders"."o_orderkey" AS "o_orderkey",
-          "orders"."o_custkey" AS "o_custkey"
-        FROM "orders" AS "orders"
-    ) AS "orders"
-      ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
-    JOIN (
-        SELECT
-          "customer"."c_custkey" AS "c_custkey",
-          "customer"."c_nationkey" AS "c_nationkey"
-        FROM "customer" AS "customer"
-    ) AS "customer"
-      ON "customer"."c_custkey" = "orders"."o_custkey"
-    JOIN "_e_0" AS "n1"
-      ON "supplier"."s_nationkey" = "n1"."n_nationkey"
-    JOIN "_e_0" AS "n2"
-      ON "customer"."c_nationkey" = "n2"."n_nationkey"
-      AND ("n1"."n_name" = 'FRANCE' OR "n2"."n_name" = 'FRANCE')
-      AND ("n1"."n_name" = 'GERMANY' OR "n2"."n_name" = 'GERMANY')
+      "supplier"."s_suppkey" AS "s_suppkey",
+      "supplier"."s_nationkey" AS "s_nationkey"
+    FROM "supplier" AS "supplier"
+  ) AS "supplier"
+  JOIN (
+    SELECT
+      "lineitem"."l_orderkey" AS "l_orderkey",
+      "lineitem"."l_suppkey" AS "l_suppkey",
+      "lineitem"."l_extendedprice" AS "l_extendedprice",
+      "lineitem"."l_discount" AS "l_discount",
+      "lineitem"."l_shipdate" AS "l_shipdate"
+    FROM "lineitem" AS "lineitem"
+    WHERE
+      "lineitem"."l_shipdate" BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
+  ) AS "lineitem"
+    ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
+  JOIN (
+    SELECT
+      "orders"."o_orderkey" AS "o_orderkey",
+      "orders"."o_custkey" AS "o_custkey"
+    FROM "orders" AS "orders"
+  ) AS "orders"
+    ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
+  JOIN (
+    SELECT
+      "customer"."c_custkey" AS "c_custkey",
+      "customer"."c_nationkey" AS "c_nationkey"
+    FROM "customer" AS "customer"
+  ) AS "customer"
+    ON "customer"."c_custkey" = "orders"."o_custkey"
+  JOIN "_e_0" AS "n1"
+    ON "supplier"."s_nationkey" = "n1"."n_nationkey"
+  JOIN "_e_0" AS "n2"
+    ON "customer"."c_nationkey" = "n2"."n_nationkey"
+    AND (
+      "n1"."n_name" = 'FRANCE'
+      OR "n2"."n_name" = 'FRANCE'
+    )
+    AND (
+      "n1"."n_name" = 'GERMANY'
+      OR "n2"."n_name" = 'GERMANY'
+    )
 ) AS "shipping"
 GROUP BY
   "shipping"."supp_nation",
@@ -559,79 +577,82 @@ order by
 SELECT
   "all_nations"."o_year" AS "o_year",
   SUM(CASE
-    WHEN "all_nations"."nation" = 'BRAZIL' THEN "all_nations"."volume"
-    ELSE 0
+  WHEN "all_nations"."nation" = 'BRAZIL'
+  THEN "all_nations"."volume"
+  ELSE 0
   END) / SUM("all_nations"."volume") AS "mkt_share"
 FROM (
+  SELECT
+    EXTRACT(year FROM "orders"."o_orderdate") AS "o_year",
+    "lineitem"."l_extendedprice" * (
+      1 - "lineitem"."l_discount"
+    ) AS "volume",
+    "n2"."n_name" AS "nation"
+  FROM (
     SELECT
-      EXTRACT(year FROM "orders"."o_orderdate") AS "o_year",
-      "lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount") AS "volume",
-      "n2"."n_name" AS "nation"
-    FROM (
-        SELECT
-          "part"."p_partkey" AS "p_partkey",
-          "part"."p_type" AS "p_type"
-        FROM "part" AS "part"
-        WHERE
-          "part"."p_type" = 'ECONOMY ANODIZED STEEL'
-    ) AS "part"
-    CROSS JOIN (
-        SELECT
-          "region"."r_regionkey" AS "r_regionkey",
-          "region"."r_name" AS "r_name"
-        FROM "region" AS "region"
-        WHERE
-          "region"."r_name" = 'AMERICA'
-    ) AS "region"
-    JOIN (
-        SELECT
-          "nation"."n_nationkey" AS "n_nationkey",
-          "nation"."n_regionkey" AS "n_regionkey"
-        FROM "nation" AS "nation"
-    ) AS "n1"
-      ON "n1"."n_regionkey" = "region"."r_regionkey"
-    JOIN (
-        SELECT
-          "customer"."c_custkey" AS "c_custkey",
-          "customer"."c_nationkey" AS "c_nationkey"
-        FROM "customer" AS "customer"
-    ) AS "customer"
-      ON "customer"."c_nationkey" = "n1"."n_nationkey"
-    JOIN (
-        SELECT
-          "orders"."o_orderkey" AS "o_orderkey",
-          "orders"."o_custkey" AS "o_custkey",
-          "orders"."o_orderdate" AS "o_orderdate"
-        FROM "orders" AS "orders"
-        WHERE
-          "orders"."o_orderdate" BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
-    ) AS "orders"
-      ON "orders"."o_custkey" = "customer"."c_custkey"
-    JOIN (
-        SELECT
-          "lineitem"."l_orderkey" AS "l_orderkey",
-          "lineitem"."l_partkey" AS "l_partkey",
-          "lineitem"."l_suppkey" AS "l_suppkey",
-          "lineitem"."l_extendedprice" AS "l_extendedprice",
-          "lineitem"."l_discount" AS "l_discount"
-        FROM "lineitem" AS "lineitem"
-    ) AS "lineitem"
-      ON "lineitem"."l_orderkey" = "orders"."o_orderkey"
-      AND "part"."p_partkey" = "lineitem"."l_partkey"
-    JOIN (
-        SELECT
-          "supplier"."s_suppkey" AS "s_suppkey",
-          "supplier"."s_nationkey" AS "s_nationkey"
-        FROM "supplier" AS "supplier"
-    ) AS "supplier"
-      ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
-    JOIN (
-        SELECT
-          "nation"."n_nationkey" AS "n_nationkey",
-          "nation"."n_name" AS "n_name"
-        FROM "nation" AS "nation"
-    ) AS "n2"
-      ON "supplier"."s_nationkey" = "n2"."n_nationkey"
+      "part"."p_partkey" AS "p_partkey",
+      "part"."p_type" AS "p_type"
+    FROM "part" AS "part"
+    WHERE
+      "part"."p_type" = 'ECONOMY ANODIZED STEEL'
+  ) AS "part"
+  CROSS JOIN (
+    SELECT
+      "region"."r_regionkey" AS "r_regionkey",
+      "region"."r_name" AS "r_name"
+    FROM "region" AS "region"
+    WHERE
+      "region"."r_name" = 'AMERICA'
+  ) AS "region"
+  JOIN (
+    SELECT
+      "nation"."n_nationkey" AS "n_nationkey",
+      "nation"."n_regionkey" AS "n_regionkey"
+    FROM "nation" AS "nation"
+  ) AS "n1"
+    ON "n1"."n_regionkey" = "region"."r_regionkey"
+  JOIN (
+    SELECT
+      "customer"."c_custkey" AS "c_custkey",
+      "customer"."c_nationkey" AS "c_nationkey"
+    FROM "customer" AS "customer"
+  ) AS "customer"
+    ON "customer"."c_nationkey" = "n1"."n_nationkey"
+  JOIN (
+    SELECT
+      "orders"."o_orderkey" AS "o_orderkey",
+      "orders"."o_custkey" AS "o_custkey",
+      "orders"."o_orderdate" AS "o_orderdate"
+    FROM "orders" AS "orders"
+    WHERE
+      "orders"."o_orderdate" BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
+  ) AS "orders"
+    ON "orders"."o_custkey" = "customer"."c_custkey"
+  JOIN (
+    SELECT
+      "lineitem"."l_orderkey" AS "l_orderkey",
+      "lineitem"."l_partkey" AS "l_partkey",
+      "lineitem"."l_suppkey" AS "l_suppkey",
+      "lineitem"."l_extendedprice" AS "l_extendedprice",
+      "lineitem"."l_discount" AS "l_discount"
+    FROM "lineitem" AS "lineitem"
+  ) AS "lineitem"
+    ON "lineitem"."l_orderkey" = "orders"."o_orderkey"
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+  JOIN (
+    SELECT
+      "supplier"."s_suppkey" AS "s_suppkey",
+      "supplier"."s_nationkey" AS "s_nationkey"
+    FROM "supplier" AS "supplier"
+  ) AS "supplier"
+    ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
+  JOIN (
+    SELECT
+      "nation"."n_nationkey" AS "n_nationkey",
+      "nation"."n_name" AS "n_name"
+    FROM "nation" AS "nation"
+  ) AS "n2"
+    ON "supplier"."s_nationkey" = "n2"."n_nationkey"
 ) AS "all_nations"
 GROUP BY
   "all_nations"."o_year"
@@ -678,59 +699,61 @@ SELECT
   "profit"."o_year" AS "o_year",
   SUM("profit"."amount") AS "sum_profit"
 FROM (
+  SELECT
+    "nation"."n_name" AS "nation",
+    EXTRACT(year FROM "orders"."o_orderdate") AS "o_year",
+    "lineitem"."l_extendedprice" * (
+      1 - "lineitem"."l_discount"
+    ) - "partsupp"."ps_supplycost" * "lineitem"."l_quantity" AS "amount"
+  FROM (
     SELECT
-      "nation"."n_name" AS "nation",
-      EXTRACT(year FROM "orders"."o_orderdate") AS "o_year",
-      "lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount") - "partsupp"."ps_supplycost" * "lineitem"."l_quantity" AS "amount"
-    FROM (
-        SELECT
-          "part"."p_partkey" AS "p_partkey",
-          "part"."p_name" AS "p_name"
-        FROM "part" AS "part"
-        WHERE
-          "part"."p_name" LIKE '%green%'
-    ) AS "part"
-    JOIN (
-        SELECT
-          "lineitem"."l_orderkey" AS "l_orderkey",
-          "lineitem"."l_partkey" AS "l_partkey",
-          "lineitem"."l_suppkey" AS "l_suppkey",
-          "lineitem"."l_quantity" AS "l_quantity",
-          "lineitem"."l_extendedprice" AS "l_extendedprice",
-          "lineitem"."l_discount" AS "l_discount"
-        FROM "lineitem" AS "lineitem"
-    ) AS "lineitem"
-      ON "part"."p_partkey" = "lineitem"."l_partkey"
-    JOIN (
-        SELECT
-          "supplier"."s_suppkey" AS "s_suppkey",
-          "supplier"."s_nationkey" AS "s_nationkey"
-        FROM "supplier" AS "supplier"
-    ) AS "supplier"
-      ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
-    JOIN (
-        SELECT
-          "partsupp"."ps_partkey" AS "ps_partkey",
-          "partsupp"."ps_suppkey" AS "ps_suppkey",
-          "partsupp"."ps_supplycost" AS "ps_supplycost"
-        FROM "partsupp" AS "partsupp"
-    ) AS "partsupp"
-      ON "partsupp"."ps_partkey" = "lineitem"."l_partkey"
-      AND "partsupp"."ps_suppkey" = "lineitem"."l_suppkey"
-    JOIN (
-        SELECT
-          "orders"."o_orderkey" AS "o_orderkey",
-          "orders"."o_orderdate" AS "o_orderdate"
-        FROM "orders" AS "orders"
-    ) AS "orders"
-      ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
-    JOIN (
-        SELECT
-          "nation"."n_nationkey" AS "n_nationkey",
-          "nation"."n_name" AS "n_name"
-        FROM "nation" AS "nation"
-    ) AS "nation"
-      ON "supplier"."s_nationkey" = "nation"."n_nationkey"
+      "part"."p_partkey" AS "p_partkey",
+      "part"."p_name" AS "p_name"
+    FROM "part" AS "part"
+    WHERE
+      "part"."p_name" LIKE '%green%'
+  ) AS "part"
+  JOIN (
+    SELECT
+      "lineitem"."l_orderkey" AS "l_orderkey",
+      "lineitem"."l_partkey" AS "l_partkey",
+      "lineitem"."l_suppkey" AS "l_suppkey",
+      "lineitem"."l_quantity" AS "l_quantity",
+      "lineitem"."l_extendedprice" AS "l_extendedprice",
+      "lineitem"."l_discount" AS "l_discount"
+    FROM "lineitem" AS "lineitem"
+  ) AS "lineitem"
+    ON "part"."p_partkey" = "lineitem"."l_partkey"
+  JOIN (
+    SELECT
+      "supplier"."s_suppkey" AS "s_suppkey",
+      "supplier"."s_nationkey" AS "s_nationkey"
+    FROM "supplier" AS "supplier"
+  ) AS "supplier"
+    ON "supplier"."s_suppkey" = "lineitem"."l_suppkey"
+  JOIN (
+    SELECT
+      "partsupp"."ps_partkey" AS "ps_partkey",
+      "partsupp"."ps_suppkey" AS "ps_suppkey",
+      "partsupp"."ps_supplycost" AS "ps_supplycost"
+    FROM "partsupp" AS "partsupp"
+  ) AS "partsupp"
+    ON "partsupp"."ps_partkey" = "lineitem"."l_partkey"
+    AND "partsupp"."ps_suppkey" = "lineitem"."l_suppkey"
+  JOIN (
+    SELECT
+      "orders"."o_orderkey" AS "o_orderkey",
+      "orders"."o_orderdate" AS "o_orderdate"
+    FROM "orders" AS "orders"
+  ) AS "orders"
+    ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
+  JOIN (
+    SELECT
+      "nation"."n_nationkey" AS "n_nationkey",
+      "nation"."n_name" AS "n_name"
+    FROM "nation" AS "nation"
+  ) AS "nation"
+    ON "supplier"."s_nationkey" = "nation"."n_nationkey"
 ) AS "profit"
 GROUP BY
   "profit"."nation",
@@ -778,50 +801,52 @@ limit
 SELECT
   "customer"."c_custkey" AS "c_custkey",
   "customer"."c_name" AS "c_name",
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "revenue",
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "revenue",
   "customer"."c_acctbal" AS "c_acctbal",
   "nation"."n_name" AS "n_name",
   "customer"."c_address" AS "c_address",
   "customer"."c_phone" AS "c_phone",
   "customer"."c_comment" AS "c_comment"
 FROM (
-    SELECT
-      "customer"."c_custkey" AS "c_custkey",
-      "customer"."c_name" AS "c_name",
-      "customer"."c_address" AS "c_address",
-      "customer"."c_nationkey" AS "c_nationkey",
-      "customer"."c_phone" AS "c_phone",
-      "customer"."c_acctbal" AS "c_acctbal",
-      "customer"."c_comment" AS "c_comment"
-    FROM "customer" AS "customer"
+  SELECT
+    "customer"."c_custkey" AS "c_custkey",
+    "customer"."c_name" AS "c_name",
+    "customer"."c_address" AS "c_address",
+    "customer"."c_nationkey" AS "c_nationkey",
+    "customer"."c_phone" AS "c_phone",
+    "customer"."c_acctbal" AS "c_acctbal",
+    "customer"."c_comment" AS "c_comment"
+  FROM "customer" AS "customer"
 ) AS "customer"
 JOIN (
-    SELECT
-      "orders"."o_orderkey" AS "o_orderkey",
-      "orders"."o_custkey" AS "o_custkey",
-      "orders"."o_orderdate" AS "o_orderdate"
-    FROM "orders" AS "orders"
-    WHERE
-      "orders"."o_orderdate" < CAST('1993-10-01' AS DATE) + INTERVAL '3' month
-      AND "orders"."o_orderdate" >= CAST('1993-10-01' AS DATE)
+  SELECT
+    "orders"."o_orderkey" AS "o_orderkey",
+    "orders"."o_custkey" AS "o_custkey",
+    "orders"."o_orderdate" AS "o_orderdate"
+  FROM "orders" AS "orders"
+  WHERE
+    "orders"."o_orderdate" < CAST('1993-10-01' AS DATE) + INTERVAL '3' month
+    AND "orders"."o_orderdate" >= CAST('1993-10-01' AS DATE)
 ) AS "orders"
   ON "customer"."c_custkey" = "orders"."o_custkey"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey" AS "l_orderkey",
-      "lineitem"."l_extendedprice" AS "l_extendedprice",
-      "lineitem"."l_discount" AS "l_discount",
-      "lineitem"."l_returnflag" AS "l_returnflag"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_returnflag" = 'R'
+  SELECT
+    "lineitem"."l_orderkey" AS "l_orderkey",
+    "lineitem"."l_extendedprice" AS "l_extendedprice",
+    "lineitem"."l_discount" AS "l_discount",
+    "lineitem"."l_returnflag" AS "l_returnflag"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_returnflag" = 'R'
 ) AS "lineitem"
   ON "lineitem"."l_orderkey" = "orders"."o_orderkey"
 JOIN (
-    SELECT
-      "nation"."n_nationkey" AS "n_nationkey",
-      "nation"."n_name" AS "n_name"
-    FROM "nation" AS "nation"
+  SELECT
+    "nation"."n_nationkey" AS "n_nationkey",
+    "nation"."n_name" AS "n_name"
+  FROM "nation" AS "nation"
 ) AS "nation"
   ON "customer"."c_nationkey" = "nation"."n_nationkey"
 GROUP BY
@@ -867,28 +892,28 @@ group by
 order by
         value desc;
 WITH "_e_0" AS (
-    SELECT
-      "supplier"."s_suppkey" AS "s_suppkey",
-      "supplier"."s_nationkey" AS "s_nationkey"
-    FROM "supplier" AS "supplier"
+  SELECT
+    "supplier"."s_suppkey" AS "s_suppkey",
+    "supplier"."s_nationkey" AS "s_nationkey"
+  FROM "supplier" AS "supplier"
 ), "_e_1" AS (
-    SELECT
-      "nation"."n_nationkey" AS "n_nationkey",
-      "nation"."n_name" AS "n_name"
-    FROM "nation" AS "nation"
-    WHERE
-      "nation"."n_name" = 'GERMANY'
+  SELECT
+    "nation"."n_nationkey" AS "n_nationkey",
+    "nation"."n_name" AS "n_name"
+  FROM "nation" AS "nation"
+  WHERE
+    "nation"."n_name" = 'GERMANY'
 )
 SELECT
   "partsupp"."ps_partkey" AS "ps_partkey",
   SUM("partsupp"."ps_supplycost" * "partsupp"."ps_availqty") AS "value"
 FROM (
-    SELECT
-      "partsupp"."ps_partkey" AS "ps_partkey",
-      "partsupp"."ps_suppkey" AS "ps_suppkey",
-      "partsupp"."ps_availqty" AS "ps_availqty",
-      "partsupp"."ps_supplycost" AS "ps_supplycost"
-    FROM "partsupp" AS "partsupp"
+  SELECT
+    "partsupp"."ps_partkey" AS "ps_partkey",
+    "partsupp"."ps_suppkey" AS "ps_suppkey",
+    "partsupp"."ps_availqty" AS "ps_availqty",
+    "partsupp"."ps_supplycost" AS "ps_supplycost"
+  FROM "partsupp" AS "partsupp"
 ) AS "partsupp"
 JOIN "_e_0" AS "supplier"
   ON "partsupp"."ps_suppkey" = "supplier"."s_suppkey"
@@ -898,19 +923,19 @@ GROUP BY
   "partsupp"."ps_partkey"
 HAVING
   SUM("partsupp"."ps_supplycost" * "partsupp"."ps_availqty") > (
+    SELECT
+      SUM("partsupp"."ps_supplycost" * "partsupp"."ps_availqty") * 0.0001 AS "_col_0"
+    FROM (
       SELECT
-        SUM("partsupp"."ps_supplycost" * "partsupp"."ps_availqty") * 0.0001 AS "_col_0"
-      FROM (
-          SELECT
-            "partsupp"."ps_suppkey" AS "ps_suppkey",
-            "partsupp"."ps_availqty" AS "ps_availqty",
-            "partsupp"."ps_supplycost" AS "ps_supplycost"
-          FROM "partsupp" AS "partsupp"
-      ) AS "partsupp"
-      JOIN "_e_0" AS "supplier"
-        ON "partsupp"."ps_suppkey" = "supplier"."s_suppkey"
-      JOIN "_e_1" AS "nation"
-        ON "supplier"."s_nationkey" = "nation"."n_nationkey"
+        "partsupp"."ps_suppkey" AS "ps_suppkey",
+        "partsupp"."ps_availqty" AS "ps_availqty",
+        "partsupp"."ps_supplycost" AS "ps_supplycost"
+      FROM "partsupp" AS "partsupp"
+    ) AS "partsupp"
+    JOIN "_e_0" AS "supplier"
+      ON "partsupp"."ps_suppkey" = "supplier"."s_suppkey"
+    JOIN "_e_1" AS "nation"
+      ON "supplier"."s_nationkey" = "nation"."n_nationkey"
   )
 ORDER BY
   "value" DESC;
@@ -949,33 +974,37 @@ order by
 SELECT
   "lineitem"."l_shipmode" AS "l_shipmode",
   SUM(CASE
-    WHEN "orders"."o_orderpriority" = '1-URGENT' OR "orders"."o_orderpriority" = '2-HIGH' THEN 1
-    ELSE 0
+  WHEN "orders"."o_orderpriority" = '1-URGENT'
+    OR "orders"."o_orderpriority" = '2-HIGH'
+  THEN 1
+  ELSE 0
   END) AS "high_line_count",
   SUM(CASE
-    WHEN "orders"."o_orderpriority" <> '1-URGENT' AND "orders"."o_orderpriority" <> '2-HIGH' THEN 1
-    ELSE 0
+  WHEN "orders"."o_orderpriority" <> '1-URGENT'
+    AND "orders"."o_orderpriority" <> '2-HIGH'
+  THEN 1
+  ELSE 0
   END) AS "low_line_count"
 FROM (
-    SELECT
-      "orders"."o_orderkey" AS "o_orderkey",
-      "orders"."o_orderpriority" AS "o_orderpriority"
-    FROM "orders" AS "orders"
+  SELECT
+    "orders"."o_orderkey" AS "o_orderkey",
+    "orders"."o_orderpriority" AS "o_orderpriority"
+  FROM "orders" AS "orders"
 ) AS "orders"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey" AS "l_orderkey",
-      "lineitem"."l_shipdate" AS "l_shipdate",
-      "lineitem"."l_commitdate" AS "l_commitdate",
-      "lineitem"."l_receiptdate" AS "l_receiptdate",
-      "lineitem"."l_shipmode" AS "l_shipmode"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_commitdate" < "lineitem"."l_receiptdate"
-      AND "lineitem"."l_receiptdate" < CAST('1994-01-01' AS DATE) + INTERVAL '1' year
-      AND "lineitem"."l_receiptdate" >= CAST('1994-01-01' AS DATE)
-      AND "lineitem"."l_shipdate" < "lineitem"."l_commitdate"
-      AND "lineitem"."l_shipmode" IN ('MAIL', 'SHIP')
+  SELECT
+    "lineitem"."l_orderkey" AS "l_orderkey",
+    "lineitem"."l_shipdate" AS "l_shipdate",
+    "lineitem"."l_commitdate" AS "l_commitdate",
+    "lineitem"."l_receiptdate" AS "l_receiptdate",
+    "lineitem"."l_shipmode" AS "l_shipmode"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_commitdate" < "lineitem"."l_receiptdate"
+    AND "lineitem"."l_receiptdate" < CAST('1994-01-01' AS DATE) + INTERVAL '1' year
+    AND "lineitem"."l_receiptdate" >= CAST('1994-01-01' AS DATE)
+    AND "lineitem"."l_shipdate" < "lineitem"."l_commitdate"
+    AND "lineitem"."l_shipmode" IN ('MAIL', 'SHIP')
 ) AS "lineitem"
   ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
 GROUP BY
@@ -1010,25 +1039,25 @@ SELECT
   "c_orders"."c_count" AS "c_count",
   COUNT(*) AS "custdist"
 FROM (
+  SELECT
+    COUNT("orders"."o_orderkey") AS "c_count"
+  FROM (
     SELECT
-      COUNT("orders"."o_orderkey") AS "c_count"
-    FROM (
-        SELECT
-          "customer"."c_custkey" AS "c_custkey"
-        FROM "customer" AS "customer"
-    ) AS "customer"
-    LEFT JOIN (
-        SELECT
-          "orders"."o_orderkey" AS "o_orderkey",
-          "orders"."o_custkey" AS "o_custkey",
-          "orders"."o_comment" AS "o_comment"
-        FROM "orders" AS "orders"
-        WHERE
-          NOT "orders"."o_comment" LIKE '%special%requests%'
-    ) AS "orders"
-      ON "customer"."c_custkey" = "orders"."o_custkey"
-    GROUP BY
-      "customer"."c_custkey"
+      "customer"."c_custkey" AS "c_custkey"
+    FROM "customer" AS "customer"
+  ) AS "customer"
+  LEFT JOIN (
+    SELECT
+      "orders"."o_orderkey" AS "o_orderkey",
+      "orders"."o_custkey" AS "o_custkey",
+      "orders"."o_comment" AS "o_comment"
+    FROM "orders" AS "orders"
+    WHERE
+      NOT "orders"."o_comment" LIKE '%special%requests%'
+  ) AS "orders"
+    ON "customer"."c_custkey" = "orders"."o_custkey"
+  GROUP BY
+    "customer"."c_custkey"
 ) AS "c_orders"
 GROUP BY
   "c_orders"."c_count"
@@ -1054,25 +1083,30 @@ where
         and l_shipdate < date '1995-09-01' + interval '1' month;
 SELECT
   100.00 * SUM(CASE
-    WHEN "part"."p_type" LIKE 'PROMO%' THEN "lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")
-    ELSE 0
-  END) / SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "promo_revenue"
+  WHEN "part"."p_type" LIKE 'PROMO%'
+  THEN "lineitem"."l_extendedprice" * (
+      1 - "lineitem"."l_discount"
+    )
+  ELSE 0
+  END) / SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "promo_revenue"
 FROM (
-    SELECT
-      "lineitem"."l_partkey" AS "l_partkey",
-      "lineitem"."l_extendedprice" AS "l_extendedprice",
-      "lineitem"."l_discount" AS "l_discount",
-      "lineitem"."l_shipdate" AS "l_shipdate"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_shipdate" < CAST('1995-09-01' AS DATE) + INTERVAL '1' month
-      AND "lineitem"."l_shipdate" >= CAST('1995-09-01' AS DATE)
+  SELECT
+    "lineitem"."l_partkey" AS "l_partkey",
+    "lineitem"."l_extendedprice" AS "l_extendedprice",
+    "lineitem"."l_discount" AS "l_discount",
+    "lineitem"."l_shipdate" AS "l_shipdate"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_shipdate" < CAST('1995-09-01' AS DATE) + INTERVAL '1' month
+    AND "lineitem"."l_shipdate" >= CAST('1995-09-01' AS DATE)
 ) AS "lineitem"
 JOIN (
-    SELECT
-      "part"."p_partkey" AS "p_partkey",
-      "part"."p_type" AS "p_type"
-    FROM "part" AS "part"
+  SELECT
+    "part"."p_partkey" AS "p_partkey",
+    "part"."p_type" AS "p_type"
+  FROM "part" AS "part"
 ) AS "part"
   ON "lineitem"."l_partkey" = "part"."p_partkey";
 
@@ -1110,15 +1144,17 @@ where
 order by
         s_suppkey;
 WITH "revenue" AS (
-    SELECT
-      "lineitem"."l_suppkey" AS "supplier_no",
-      SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "total_revenue"
-    FROM "lineitem" AS "lineitem"
-    WHERE
-      "lineitem"."l_shipdate" < CAST('1996-01-01' AS DATE) + INTERVAL '3' month
-      AND "lineitem"."l_shipdate" >= CAST('1996-01-01' AS DATE)
-    GROUP BY
-      "lineitem"."l_suppkey"
+  SELECT
+    "lineitem"."l_suppkey" AS "supplier_no",
+    SUM("lineitem"."l_extendedprice" * (
+      1 - "lineitem"."l_discount"
+    )) AS "total_revenue"
+  FROM "lineitem" AS "lineitem"
+  WHERE
+    "lineitem"."l_shipdate" < CAST('1996-01-01' AS DATE) + INTERVAL '3' month
+    AND "lineitem"."l_shipdate" >= CAST('1996-01-01' AS DATE)
+  GROUP BY
+    "lineitem"."l_suppkey"
 )
 SELECT
   "supplier"."s_suppkey" AS "s_suppkey",
@@ -1127,18 +1163,18 @@ SELECT
   "supplier"."s_phone" AS "s_phone",
   "revenue"."total_revenue" AS "total_revenue"
 FROM (
-    SELECT
-      "supplier"."s_suppkey" AS "s_suppkey",
-      "supplier"."s_name" AS "s_name",
-      "supplier"."s_address" AS "s_address",
-      "supplier"."s_phone" AS "s_phone"
-    FROM "supplier" AS "supplier"
+  SELECT
+    "supplier"."s_suppkey" AS "s_suppkey",
+    "supplier"."s_name" AS "s_name",
+    "supplier"."s_address" AS "s_address",
+    "supplier"."s_phone" AS "s_phone"
+  FROM "supplier" AS "supplier"
 ) AS "supplier"
 JOIN "revenue"
   ON "revenue"."total_revenue" = (
-      SELECT
-        MAX("revenue"."total_revenue") AS "_col_0"
-      FROM "revenue"
+    SELECT
+      MAX("revenue"."total_revenue") AS "_col_0"
+    FROM "revenue"
   )
   AND "supplier"."s_suppkey" = "revenue"."supplier_no"
 ORDER BY
@@ -1183,31 +1219,31 @@ SELECT
   "part"."p_size" AS "p_size",
   COUNT(DISTINCT "partsupp"."ps_suppkey") AS "supplier_cnt"
 FROM (
-    SELECT
-      "partsupp"."ps_partkey" AS "ps_partkey",
-      "partsupp"."ps_suppkey" AS "ps_suppkey"
-    FROM "partsupp" AS "partsupp"
+  SELECT
+    "partsupp"."ps_partkey" AS "ps_partkey",
+    "partsupp"."ps_suppkey" AS "ps_suppkey"
+  FROM "partsupp" AS "partsupp"
 ) AS "partsupp"
 JOIN (
-    SELECT
-      "part"."p_partkey" AS "p_partkey",
-      "part"."p_brand" AS "p_brand",
-      "part"."p_type" AS "p_type",
-      "part"."p_size" AS "p_size"
-    FROM "part" AS "part"
-    WHERE
-      "part"."p_brand" <> 'Brand#45'
-      AND "part"."p_size" IN (49, 14, 23, 45, 19, 3, 36, 9)
-      AND NOT "part"."p_type" LIKE 'MEDIUM POLISHED%'
+  SELECT
+    "part"."p_partkey" AS "p_partkey",
+    "part"."p_brand" AS "p_brand",
+    "part"."p_type" AS "p_type",
+    "part"."p_size" AS "p_size"
+  FROM "part" AS "part"
+  WHERE
+    "part"."p_brand" <> 'Brand#45'
+    AND "part"."p_size" IN (49, 14, 23, 45, 19, 3, 36, 9)
+    AND NOT "part"."p_type" LIKE 'MEDIUM POLISHED%'
 ) AS "part"
   ON "part"."p_partkey" = "partsupp"."ps_partkey"
 WHERE
   NOT "partsupp"."ps_suppkey" IN (
-      SELECT
-        "supplier"."s_suppkey" AS "s_suppkey"
-      FROM "supplier" AS "supplier"
-      WHERE
-        "supplier"."s_comment" LIKE '%Customer%Complaints%'
+    SELECT
+      "supplier"."s_suppkey" AS "s_suppkey"
+    FROM "supplier" AS "supplier"
+    WHERE
+      "supplier"."s_comment" LIKE '%Customer%Complaints%'
   )
 GROUP BY
   "part"."p_brand",
@@ -1242,30 +1278,30 @@ where
 SELECT
   SUM("lineitem"."l_extendedprice") / 7.0 AS "avg_yearly"
 FROM (
-    SELECT
-      "lineitem"."l_partkey" AS "l_partkey",
-      "lineitem"."l_quantity" AS "l_quantity",
-      "lineitem"."l_extendedprice" AS "l_extendedprice"
-    FROM "lineitem" AS "lineitem"
+  SELECT
+    "lineitem"."l_partkey" AS "l_partkey",
+    "lineitem"."l_quantity" AS "l_quantity",
+    "lineitem"."l_extendedprice" AS "l_extendedprice"
+  FROM "lineitem" AS "lineitem"
 ) AS "lineitem"
 JOIN (
-    SELECT
-      "part"."p_partkey" AS "p_partkey",
-      "part"."p_brand" AS "p_brand",
-      "part"."p_container" AS "p_container"
-    FROM "part" AS "part"
-    WHERE
-      "part"."p_brand" = 'Brand#23'
-      AND "part"."p_container" = 'MED BOX'
+  SELECT
+    "part"."p_partkey" AS "p_partkey",
+    "part"."p_brand" AS "p_brand",
+    "part"."p_container" AS "p_container"
+  FROM "part" AS "part"
+  WHERE
+    "part"."p_brand" = 'Brand#23'
+    AND "part"."p_container" = 'MED BOX'
 ) AS "part"
   ON "part"."p_partkey" = "lineitem"."l_partkey"
 JOIN (
-    SELECT
-      0.2 * AVG("lineitem"."l_quantity") AS "_col_0",
-      "lineitem"."l_partkey"
-    FROM "lineitem" AS "lineitem"
-    GROUP BY
-      "lineitem"."l_partkey"
+  SELECT
+    0.2 * AVG("lineitem"."l_quantity") AS "_col_0",
+    "lineitem"."l_partkey"
+  FROM "lineitem" AS "lineitem"
+  GROUP BY
+    "lineitem"."l_partkey"
 ) AS "_d_0"
   ON "_d_0"."l_partkey" = "part"."p_partkey"
   AND "lineitem"."l_quantity" < "_d_0"."_col_0";
@@ -1315,35 +1351,35 @@ SELECT
   "orders"."o_totalprice" AS "o_totalprice",
   SUM("lineitem"."l_quantity") AS "_col_5"
 FROM (
-    SELECT
-      "customer"."c_custkey" AS "c_custkey",
-      "customer"."c_name" AS "c_name"
-    FROM "customer" AS "customer"
+  SELECT
+    "customer"."c_custkey" AS "c_custkey",
+    "customer"."c_name" AS "c_name"
+  FROM "customer" AS "customer"
 ) AS "customer"
 JOIN (
-    SELECT
-      "orders"."o_orderkey" AS "o_orderkey",
-      "orders"."o_custkey" AS "o_custkey",
-      "orders"."o_totalprice" AS "o_totalprice",
-      "orders"."o_orderdate" AS "o_orderdate"
-    FROM "orders" AS "orders"
+  SELECT
+    "orders"."o_orderkey" AS "o_orderkey",
+    "orders"."o_custkey" AS "o_custkey",
+    "orders"."o_totalprice" AS "o_totalprice",
+    "orders"."o_orderdate" AS "o_orderdate"
+  FROM "orders" AS "orders"
 ) AS "orders"
   ON "customer"."c_custkey" = "orders"."o_custkey"
 JOIN (
-    SELECT
-      "lineitem"."l_orderkey" AS "l_orderkey",
-      "lineitem"."l_quantity" AS "l_quantity"
-    FROM "lineitem" AS "lineitem"
+  SELECT
+    "lineitem"."l_orderkey" AS "l_orderkey",
+    "lineitem"."l_quantity" AS "l_quantity"
+  FROM "lineitem" AS "lineitem"
 ) AS "lineitem"
   ON "orders"."o_orderkey" = "lineitem"."l_orderkey"
   AND "orders"."o_orderkey" IN (
-      SELECT
-        "lineitem"."l_orderkey" AS "l_orderkey"
-      FROM "lineitem" AS "lineitem"
-      GROUP BY
-        "lineitem"."l_orderkey"
-      HAVING
-        SUM("lineitem"."l_quantity") > 300
+    SELECT
+      "lineitem"."l_orderkey" AS "l_orderkey"
+    FROM "lineitem" AS "lineitem"
+    GROUP BY
+      "lineitem"."l_orderkey"
+    HAVING
+      SUM("lineitem"."l_quantity") > 300
   )
 GROUP BY
   "customer"."c_name",
@@ -1395,29 +1431,73 @@ where
                 and l_shipinstruct = 'DELIVER IN PERSON'
         );
 SELECT
-  SUM("lineitem"."l_extendedprice" * (1 - "lineitem"."l_discount")) AS "revenue"
+  SUM("lineitem"."l_extendedprice" * (
+    1 - "lineitem"."l_discount"
+  )) AS "revenue"
 FROM (
-    SELECT
-      "lineitem"."l_partkey" AS "l_partkey",
-      "lineitem"."l_quantity" AS "l_quantity",
-      "lineitem"."l_extendedprice" AS "l_extendedprice",
-      "lineitem"."l_discount" AS "l_discount",
-      "lineitem"."l_shipinstruct" AS "l_shipinstruct",
-      "lineitem"."l_shipmode" AS "l_shipmode"
-    FROM "lineitem" AS "lineitem"
+  SELECT
+    "lineitem"."l_partkey" AS "l_partkey",
+    "lineitem"."l_quantity" AS "l_quantity",
+    "lineitem"."l_extendedprice" AS "l_extendedprice",
+    "lineitem"."l_discount" AS "l_discount",
+    "lineitem"."l_shipinstruct" AS "l_shipinstruct",
+    "lineitem"."l_shipmode" AS "l_shipmode"
+  FROM "lineitem" AS "lineitem"
 ) AS "lineitem"
 JOIN (
-    SELECT
-      "part"."p_partkey" AS "p_partkey",
-      "part"."p_brand" AS "p_brand",
-      "part"."p_size" AS "p_size",
-      "part"."p_container" AS "p_container"
-    FROM "part" AS "part"
+  SELECT
+    "part"."p_partkey" AS "p_partkey",
+    "part"."p_brand" AS "p_brand",
+    "part"."p_size" AS "p_size",
+    "part"."p_container" AS "p_container"
+  FROM "part" AS "part"
 ) AS "part"
-  ON ("part"."p_brand" = 'Brand#12' AND "part"."p_container" IN ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 5)
-  OR ("part"."p_brand" = 'Brand#23' AND "part"."p_container" IN ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 10)
-  OR ("part"."p_brand" = 'Brand#34' AND "part"."p_container" IN ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 15)
+  ON (
+    "part"."p_brand" = 'Brand#12'
+    AND "part"."p_container" IN ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 5
+  )
+  OR (
+    "part"."p_brand" = 'Brand#23'
+    AND "part"."p_container" IN ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 10
+  )
+  OR (
+    "part"."p_brand" = 'Brand#34'
+    AND "part"."p_container" IN ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 15
+  )
 WHERE
-  ("lineitem"."l_quantity" <= 1 + 10 AND "lineitem"."l_quantity" >= 1 AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON' AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG') AND "part"."p_brand" = 'Brand#12' AND "part"."p_container" IN ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 5)
-  OR ("lineitem"."l_quantity" <= 10 + 10 AND "lineitem"."l_quantity" >= 10 AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON' AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG') AND "part"."p_brand" = 'Brand#23' AND "part"."p_container" IN ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 10)
-  OR ("lineitem"."l_quantity" <= 20 + 10 AND "lineitem"."l_quantity" >= 20 AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON' AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG') AND "part"."p_brand" = 'Brand#34' AND "part"."p_container" IN ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG') AND "part"."p_partkey" = "lineitem"."l_partkey" AND "part"."p_size" BETWEEN 1 AND 15);
+  (
+    "lineitem"."l_quantity" <= 1 + 10
+    AND "lineitem"."l_quantity" >= 1
+    AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON'
+    AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG')
+    AND "part"."p_brand" = 'Brand#12'
+    AND "part"."p_container" IN ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 5
+  )
+  OR (
+    "lineitem"."l_quantity" <= 10 + 10
+    AND "lineitem"."l_quantity" >= 10
+    AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON'
+    AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG')
+    AND "part"."p_brand" = 'Brand#23'
+    AND "part"."p_container" IN ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 10
+  )
+  OR (
+    "lineitem"."l_quantity" <= 20 + 10
+    AND "lineitem"."l_quantity" >= 20
+    AND "lineitem"."l_shipinstruct" = 'DELIVER IN PERSON'
+    AND "lineitem"."l_shipmode" IN ('AIR', 'AIR REG')
+    AND "part"."p_brand" = 'Brand#34'
+    AND "part"."p_container" IN ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
+    AND "part"."p_partkey" = "lineitem"."l_partkey"
+    AND "part"."p_size" BETWEEN 1 AND 15
+  );
diff --git a/tests/fixtures/pretty.sql b/tests/fixtures/pretty.sql
--- a/tests/fixtures/pretty.sql
+++ b/tests/fixtures/pretty.sql
@@ -2,6 +2,7 @@ SELECT * FROM test;
 SELECT
   *
 FROM test;
+
 WITH cte1 AS (
     SELECT a, z and e AS b
     FROM cte
@@ -15,9 +16,9 @@ WITH cte1 AS (
         SELECT 2
         UNION ALL
         SELECT CASE x AND 1 + 1 = 2
-        WHEN TRUE THEN 1
+        WHEN TRUE THEN 1 AND 4 + 3 AND Z
         WHEN x and y THEN 2
-        ELSE 3 END
+        ELSE 3 AND 4 AND g END
         UNION ALL
         SELECT 1
         FROM (SELECT 1) AS x, y, (SELECT 2) z
@@ -34,86 +35,103 @@ LEFT JOIN (
     FROM (SELECT * FROM bar WHERE (c > 1 AND d > 1) OR e > 1 GROUP BY a HAVING a > 1 LIMIT 10) z
 ) y ON x.a = y.b AND x.a > 1 OR (x.c = y.d OR x.c = y.e);
 WITH cte1 AS (
-    SELECT
-      a,
-      z AND e AS b
-    FROM cte
-    WHERE
-      x IN (1, 2, 3)
-      AND z < -1
-      OR z > 1
-      AND w = 'AND'
+  SELECT
+    a,
+    z
+    AND e AS b
+  FROM cte
+  WHERE
+    x IN (1, 2, 3)
+    AND z < -1
+    OR z > 1
+    AND w = 'AND'
 ), cte2 AS (
+  SELECT
+    RANK() OVER(PARTITION BY a, b ORDER BY x DESC) AS a,
+    b
+  FROM cte
+  CROSS JOIN (
     SELECT
-      RANK() OVER(PARTITION BY a, b ORDER BY x DESC) AS a,
-      b
-    FROM cte
-    CROSS JOIN (
-        SELECT
-          1
-        UNION ALL
-        SELECT
-          2
-        UNION ALL
-        SELECT
-          CASE x AND 1 + 1 = 2
-            WHEN TRUE THEN 1
-            WHEN x AND y THEN 2
-            ELSE 3
-          END
-        UNION ALL
-        SELECT
-          1
-        FROM (
-            SELECT
-              1
-        ) AS x, y, (
-            SELECT
-              2
-        ) AS z
-    ) AS x
+      1
+    UNION ALL
+    SELECT
+      2
+    UNION ALL
+    SELECT
+      CASE x
+        AND 1 + 1 = 2
+      WHEN TRUE
+      THEN 1
+        AND 4 + 3
+        AND Z
+      WHEN x
+        AND y
+      THEN 2
+      ELSE 3
+        AND 4
+        AND g
+      END
+    UNION ALL
+    SELECT
+      1
+    FROM (
+      SELECT
+        1
+    ) AS x, y, (
+      SELECT
+        2
+    ) AS z
+  ) AS x
 )
 SELECT
   a,
   b AS c
 FROM (
-    SELECT
-      a AS w,
-      1 + 1 AS c
-    FROM foo
-    WHERE
-      w IN (
-              SELECT
-                z
-              FROM q
-          )
-    GROUP BY
-      a,
-      b
+  SELECT
+    a AS w,
+    1 + 1 AS c
+  FROM foo
+  WHERE
+    w IN (
+      SELECT
+        z
+      FROM q
+    )
+  GROUP BY
+    a,
+    b
 ) AS x
 LEFT JOIN (
+  SELECT
+    a,
+    b
+  FROM (
     SELECT
-      a,
-      b
-    FROM (
-        SELECT
-          *
-        FROM bar
-        WHERE
-          (c > 1 AND d > 1)
-          OR e > 1
-        GROUP BY
-          a
-        HAVING
-          a > 1
-        LIMIT 10
-    ) AS z
+      *
+    FROM bar
+    WHERE
+      (
+        c > 1
+        AND d > 1
+      )
+      OR e > 1
+    GROUP BY
+      a
+    HAVING
+      a > 1
+    LIMIT 10
+  ) AS z
 ) AS y
   ON x.a = y.b
   AND x.a > 1
-  OR (x.c = y.d OR x.c = y.e);
+  OR (
+    x.c = y.d
+    OR x.c = y.e
+  );
+
 SELECT myCol1, myCol2 FROM baseTable LATERAL VIEW OUTER explode(col1) myTable1 AS myCol1 LATERAL VIEW explode(col2) myTable2 AS myCol2
 where a > 1 and b > 2 or c > 3;
+
 SELECT
   myCol1,
   myCol2
@@ -126,22 +144,25 @@ WHERE
   a > 1
   AND b > 2
   OR c > 3;
+
 SELECT * FROM (WITH y AS ( SELECT 1 AS z) SELECT z from y) x;
 SELECT
   *
 FROM (
-    WITH y AS (
-        SELECT
-          1 AS z
-    )
+  WITH y AS (
     SELECT
-      z
-    FROM y
+      1 AS z
+  )
+  SELECT
+    z
+  FROM y
 ) AS x;
+
 INSERT OVERWRITE TABLE x VALUES (1, 2.0, '3.0'), (4, 5.0, '6.0');
 INSERT OVERWRITE TABLE x VALUES
   (1, 2.0, '3.0'),
   (4, 5.0, '6.0');
+
 WITH regional_sales AS (
     SELECT region, SUM(amount) AS total_sales
     FROM orders
@@ -159,22 +180,22 @@ FROM orders
 WHERE region IN (SELECT region FROM top_regions)
 GROUP BY region, product;
 WITH regional_sales AS (
-    SELECT
-      region,
-      SUM(amount) AS total_sales
-    FROM orders
-    GROUP BY
-      region
+  SELECT
+    region,
+    SUM(amount) AS total_sales
+  FROM orders
+  GROUP BY
+    region
 ), top_regions AS (
-    SELECT
-      region
-    FROM regional_sales
-    WHERE
-      total_sales > (
-              SELECT
-                SUM(total_sales) / 10
-              FROM regional_sales
-          )
+  SELECT
+    region
+  FROM regional_sales
+  WHERE
+    total_sales > (
+      SELECT
+        SUM(total_sales) / 10
+      FROM regional_sales
+    )
 )
 SELECT
   region,
@@ -184,13 +205,14 @@ SELECT
 FROM orders
 WHERE
   region IN (
-      SELECT
-        region
-      FROM top_regions
+    SELECT
+      region
+    FROM top_regions
   )
 GROUP BY
   region,
   product;
+
 CREATE TABLE `t_customer_account` ( `id` int, `customer_id` int, `bank` varchar(100), `account_no` varchar(100));
 CREATE TABLE `t_customer_account` (
   `id` INT,
@@ -198,6 +220,7 @@ CREATE TABLE `t_customer_account` (
   `bank` VARCHAR(100),
   `account_no` VARCHAR(100)
 );
+
 CREATE TABLE `t_customer_account` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `customer_id` int(11) DEFAULT NULL COMMENT '客户id',