Repository: tobymao/sqlglot

Issue:
Inconsistent formatting when leading_comma=True
Hi! This is something I noticed when trying out the [new formatting settings](https://github.com/TobikoData/sqlmesh/pull/2064) in SQLMesh, although I believe it's unrelated to that patch.  

```pycon
>>> import sqlglot
>>> print(sqlglot.__version__)
23.0.5
>>> sql = "select a, b, c from my_table"
>>> # first column has extra indentation
>>> print(sqlglot.parse_one(sql).sql(pretty=True, pad=4, indent=4, leading_comma=True))
SELECT
        a
    , b
    , c
FROM my_table
>>> # this is not the case when leading_comma=False
>>> print(sqlglot.parse_one(sql).sql(pretty=True, pad=4, indent=4, leading_comma=False))
SELECT
    a,
    b,
    c
FROM my_table
>>> # if pad=2, it formats consistently, but using 2 spaces rather than 4
>>> # (it also formats exactly the same with pad=2 and indent=2)
>>> print(sqlglot.parse_one(sql).sql(pretty=True, pad=2, indent=4, leading_comma=True))
SELECT
    a
  , b
  , c
```

There doesn't seem to be a combination of settings which 4-space-indents consistently with leading commas.
Am I misunderstanding something about how `pad` and `indent` interact?


Discussion:
Hey @giovannipcarvalho, thank you for reporting this. We'll take a look soon 👍 

Resolving patch:
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -46,9 +46,11 @@ class Generator(metaclass=_Generator):
             'safe': Only quote identifiers that are case insensitive.
         normalize: Whether to normalize identifiers to lowercase.
             Default: False.
-        pad: The pad size in a formatted string.
+        pad: The pad size in a formatted string. For example, this affects the indentation of
+            a projection in a query, relative to its nesting level.
             Default: 2.
-        indent: The indentation size in a formatted string.
+        indent: The indentation size in a formatted string. For example, this affects the
+            indentation of subqueries and filters under a `WHERE` clause.
             Default: 2.
         normalize_functions: How to normalize function names. Possible values are:
             "upper" or True (default): Convert names to uppercase.
@@ -3221,7 +3223,7 @@ def expressions(
         num_sqls = len(expressions)
 
         # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
-        pad = " " * self.pad
+        pad = " " * len(sep)
         stripped_sep = sep.strip()
 
         result_sqls = []


Test patch:
diff --git a/tests/test_transpile.py b/tests/test_transpile.py
--- a/tests/test_transpile.py
+++ b/tests/test_transpile.py
@@ -66,6 +66,24 @@ def test_some(self):
         )
 
     def test_leading_comma(self):
+        self.validate(
+            "SELECT a, b, c FROM (SELECT a, b, c FROM t)",
+            "SELECT\n"
+            "      a\n"
+            "    , b\n"
+            "    , c\n"
+            "FROM (\n"
+            "    SELECT\n"
+            "          a\n"
+            "        , b\n"
+            "        , c\n"
+            "    FROM t\n"
+            ")",
+            leading_comma=True,
+            pretty=True,
+            pad=4,
+            indent=4,
+        )
         self.validate(
             "SELECT FOO, BAR, BAZ",
             "SELECT\n    FOO\n  , BAR\n  , BAZ",