Skip to content

Commit

Permalink
[FIX] PARTITION BY with newline not properly formatted (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloRMira authored Jan 22, 2021
1 parent 64ee02a commit 4ec4b93
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 48 deletions.
133 changes: 117 additions & 16 deletions docs/core.html

Large diffs are not rendered by default.

118 changes: 92 additions & 26 deletions docs/utils.html

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
" s = replace_newline_chars(s) # remove newlines but not in the comments\n",
" s = remove_whitespaces_newline(s) # remove whitespaces after and before newline\n",
" s = remove_whitespaces_comments(s) # remove whitespaces after and before [C], [CS] and [CI]\n",
" s = remove_whitespaces_parenthesis(s) # remove whitespaces between parenthesis\n",
" s = remove_redundant_whitespaces(s) # remove too many whitespaces but no newlines\n",
" return s"
]
Expand Down Expand Up @@ -351,6 +352,34 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"create or replace table my_table as select substr(asdf, 1, 2) as qwer, qwer over (PARTITION BY asdf) from table1\n"
]
}
],
"source": [
"assert_and_print(\n",
" clean_query(\n",
"\"\"\"\n",
"create or replace table my_table as\n",
"select substr( asdf, 1, 2 ) as qwer,\n",
"qwer over (\n",
"PARTITION BY asdf\n",
")\n",
"from table1\n",
"\"\"\"\n",
" ), \"create or replace table my_table as select substr(asdf, 1, 2) as qwer, qwer over (PARTITION BY asdf) from table1\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -3302,6 +3331,51 @@
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Partition By with newline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CREATE OR REPLACE TABLE asdf AS\n",
"SELECT asdf,\n",
" qwer OVER (PARTITION BY asdf,\n",
" qwer\n",
" ORDER BY qwerty)\n",
"FROM table1\n"
]
}
],
"source": [
"assert_and_print(\n",
"format_sql(\"\"\"\n",
"create or replace table asdf as\n",
"select asdf, qwer over (\n",
"partition by asdf, qwer order by qwerty\n",
")\n",
"from table1\n",
"\"\"\"),\n",
"\"\"\"\n",
"CREATE OR REPLACE TABLE asdf AS\n",
"SELECT asdf,\n",
" qwer OVER (PARTITION BY asdf,\n",
" qwer\n",
" ORDER BY qwerty)\n",
"FROM table1\n",
"\"\"\".strip()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
49 changes: 48 additions & 1 deletion nbs/02_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,40 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def remove_whitespaces_parenthesis(s):\n",
" \"Remove whitespaces between parenthesis in query `s`\"\n",
" s = re.sub(r\"\\([\\r\\t\\f\\v ]+\", \"(\", s) # remove whitespaces after (\n",
" s = re.sub(r\"[\\r\\t\\f\\v ]+\\)\", \")\", s) # remove whitespaces before )\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"select asdf, substr(qwer, 1, 2) as qwerty\n"
]
}
],
"source": [
"assert_and_print(\n",
" remove_whitespaces_parenthesis(\"select asdf, substr( qwer, 1, 2 ) as qwerty\"),\n",
" \"select asdf, substr(qwer, 1, 2) as qwerty\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -2450,7 +2484,20 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Converted 00_core.ipynb.\n",
"Converted 01_format_file.ipynb.\n",
"Converted 02_utils.ipynb.\n",
"Converted 03_validation.ipynb.\n",
"Converted 04_release.ipynb.\n",
"Converted index.ipynb.\n"
]
}
],
"source": [
"#hide\n",
"from nbdev.export import notebook2script\n",
Expand Down
1 change: 1 addition & 0 deletions sql_formatter/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"remove_whitespaces_newline": "02_utils.ipynb",
"remove_whitespaces_comments": "02_utils.ipynb",
"remove_redundant_whitespaces": "02_utils.ipynb",
"remove_whitespaces_parenthesis": "02_utils.ipynb",
"add_whitespaces_between_symbols": "02_utils.ipynb",
"mark_ci_comments": "02_utils.ipynb",
"mark_comments": "02_utils.ipynb",
Expand Down
1 change: 1 addition & 0 deletions sql_formatter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def clean_query(s):
s = replace_newline_chars(s) # remove newlines but not in the comments
s = remove_whitespaces_newline(s) # remove whitespaces after and before newline
s = remove_whitespaces_comments(s) # remove whitespaces after and before [C], [CS] and [CI]
s = remove_whitespaces_parenthesis(s) # remove whitespaces between parenthesis
s = remove_redundant_whitespaces(s) # remove too many whitespaces but no newlines
return s

Expand Down
18 changes: 13 additions & 5 deletions sql_formatter/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/02_utils.ipynb (unless otherwise specified).

__all__ = ['assert_and_print', 'compress_dicts', 'remove_whitespaces_newline', 'remove_whitespaces_comments',
'remove_redundant_whitespaces', 'add_whitespaces_between_symbols', 'mark_ci_comments', 'mark_comments',
'split_query', 'split_apply_concat', 'split_comment_quote', 'split_comment', 'identify_in_sql',
'split_by_semicolon', 'replace_newline_chars', 'identify_end_of_fields', 'add_newline_indentation',
'extract_outer_subquery', 'format_subquery', 'check_sql_query', 'check_skip_marker',
'identify_create_table_view', 'count_lines', 'find_line_number', 'jaccard_distance', 'assign_comment']
'remove_redundant_whitespaces', 'remove_whitespaces_parenthesis', 'add_whitespaces_between_symbols',
'mark_ci_comments', 'mark_comments', 'split_query', 'split_apply_concat', 'split_comment_quote',
'split_comment', 'identify_in_sql', 'split_by_semicolon', 'replace_newline_chars', 'identify_end_of_fields',
'add_newline_indentation', 'extract_outer_subquery', 'format_subquery', 'check_sql_query',
'check_skip_marker', 'identify_create_table_view', 'count_lines', 'find_line_number', 'jaccard_distance',
'assign_comment']

# Cell
import re
Expand Down Expand Up @@ -82,6 +83,13 @@ def remove_redundant_whitespaces(s):
s = re.sub(r"[\r\t\f\v ]{2,}", " ", s) # remove too many whitespaces but not newlines
return s

# Cell
def remove_whitespaces_parenthesis(s):
"Remove whitespaces between parenthesis in query `s`"
s = re.sub(r"\([\r\t\f\v ]+", "(", s) # remove whitespaces after (
s = re.sub(r"[\r\t\f\v ]+\)", ")", s) # remove whitespaces before )
return s

# Cell
def add_whitespaces_between_symbols(s):
"Add whitespaces between symbols in line `s`"
Expand Down

0 comments on commit 4ec4b93

Please sign in to comment.