Skip to content

Commit

Permalink
[FIX] case when wrongly formatted if case when at least in second arg…
Browse files Browse the repository at this point in the history
…ument and preceded by text in quotes (#133)

* [case-when] fix case when issue for first argument being a string

* [case-when] second try to get CI working
  • Loading branch information
PabloRMira authored Jan 11, 2021
1 parent 8705b23 commit 416d533
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 218 deletions.
200 changes: 133 additions & 67 deletions docs/core.html
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,44 @@ <h4 id="remove_wrong_end_comma" class="doc_header"><code>remove_wrong_end_comma<
</div>
</div>

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Helper-function-for-case-when">Helper function for <code>case when</code><a class="anchor-link" href="#Helper-function-for-case-when"> </a></h4>
</div>
</div>
</div>
{% raw %}

<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_case_when" class="doc_header"><code>format_case_when</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L159" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_case_when</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format case when statement in line <code>s</code></p>

</div>

</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">

</div>
{% endraw %}

Expand All @@ -1644,7 +1682,7 @@ <h3 id="SELECT">SELECT<a class="anchor-link" href="#SELECT"> </a></h3>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_select" class="doc_header"><code>format_select</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L159" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_select</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_select" class="doc_header"><code>format_select</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L183" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_select</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format SELECT statement line <code>s</code></p>

Expand Down Expand Up @@ -1779,68 +1817,12 @@ <h4 id="format_select" class="doc_header"><code>format_select</code><a href="htt
<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>Assertion failed

Observed:

select asdf, -- Some comment -- Another comment
asdforqwer


Expected:

select asdf, -- Some comment
<pre>select asdf, -- Some comment
asdforqwer -- Another comment
Exception found at position 28:

10-characters window:

Observed:

mment -- A


Expected:

mment

</pre>
</div>
</div>

<div class="output_area">

<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">-----------------------------------------------------------------------</span>
<span class="ansi-red-fg">AssertionError</span> Traceback (most recent call last)
<span class="ansi-green-fg">~/Dokumente/Python_Stuff/Projekte/sql_formatter/sql_formatter/utils.py</span> in <span class="ansi-cyan-fg">assert_and_print</span><span class="ansi-blue-fg">(s_in, s_expected)</span>
<span class="ansi-green-intense-fg ansi-bold"> 18</span> <span class="ansi-green-fg">try</span><span class="ansi-blue-fg">:</span>
<span class="ansi-green-fg">---&gt; 19</span><span class="ansi-red-fg"> </span><span class="ansi-green-fg">assert</span> s_in <span class="ansi-blue-fg">==</span> s_expected
<span class="ansi-green-intense-fg ansi-bold"> 20</span> <span class="ansi-green-fg">except</span><span class="ansi-blue-fg">:</span>

<span class="ansi-red-fg">AssertionError</span>:

During handling of the above exception, another exception occurred:

<span class="ansi-red-fg">AssertionError</span> Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-70-57dc18c8340b&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span>
<span class="ansi-green-intense-fg ansi-bold"> 4</span> select asdf<span class="ansi-blue-fg">,</span> <span class="ansi-blue-fg">-</span><span class="ansi-blue-fg">-</span> Some comment
<span class="ansi-green-intense-fg ansi-bold"> 5</span> asdforqwer <span class="ansi-blue-fg">-</span><span class="ansi-blue-fg">-</span> Another comment
<span class="ansi-green-fg">----&gt; 6</span><span class="ansi-red-fg"> &#34;&#34;&#34;.strip()
</span><span class="ansi-green-intense-fg ansi-bold"> 7</span> )

<span class="ansi-green-fg">~/Dokumente/Python_Stuff/Projekte/sql_formatter/sql_formatter/utils.py</span> in <span class="ansi-cyan-fg">assert_and_print</span><span class="ansi-blue-fg">(s_in, s_expected)</span>
<span class="ansi-green-intense-fg ansi-bold"> 35</span> print<span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">&#34;Expected:\n&#34;</span><span class="ansi-blue-fg">)</span>
<span class="ansi-green-intense-fg ansi-bold"> 36</span> print<span class="ansi-blue-fg">(</span>s_expected<span class="ansi-blue-fg">[</span>max<span class="ansi-blue-fg">(</span>i<span class="ansi-blue-fg">-</span><span class="ansi-cyan-fg">5</span><span class="ansi-blue-fg">,</span> <span class="ansi-cyan-fg">0</span><span class="ansi-blue-fg">)</span><span class="ansi-blue-fg">:</span>i<span class="ansi-blue-fg">+</span><span class="ansi-cyan-fg">5</span><span class="ansi-blue-fg">]</span><span class="ansi-blue-fg">)</span>
<span class="ansi-green-fg">---&gt; 37</span><span class="ansi-red-fg"> </span><span class="ansi-green-fg">assert</span> s_in <span class="ansi-blue-fg">==</span> s_expected
<span class="ansi-green-intense-fg ansi-bold"> 38</span> print<span class="ansi-blue-fg">(</span>s_in<span class="ansi-blue-fg">)</span>
<span class="ansi-green-intense-fg ansi-bold"> 39</span> <span class="ansi-green-fg">return</span> <span class="ansi-green-fg">None</span>

<span class="ansi-red-fg">AssertionError</span>: </pre>
</div>
</div>

</div>
</div>

Expand Down Expand Up @@ -2588,6 +2570,90 @@ <h4 id="format_select" class="doc_header"><code>format_select</code><a href="htt
</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">assert_and_print</span><span class="p">(</span>
<span class="n">format_select</span><span class="p">(</span><span class="s2">&quot;select qwer1, substr(qwer, case when asdf = &#39;J&#39; then 1 else 0 end, 4) as qwer2, qwer3&quot;</span><span class="p">),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">select qwer1,</span>
<span class="sd"> substr(qwer, case when asdf = &#39;J&#39; then 1</span>
<span class="sd"> else 0 end, 4) as qwer2,</span>
<span class="sd"> qwer3</span>
<span class="sd">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>select qwer1,
substr(qwer, case when asdf = &#39;J&#39; then 1
else 0 end, 4) as qwer2,
qwer3
</pre>
</div>
</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">assert_and_print</span><span class="p">(</span>
<span class="n">format_select</span><span class="p">(</span><span class="s2">&quot;select qwer1, substr(&#39;blabla&#39;, case when asdf = &#39;J&#39; then 1 else 0 end, 4) as qwer2, qwer3&quot;</span><span class="p">),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">select qwer1,</span>
<span class="sd"> substr(&#39;blabla&#39;, case when asdf = &#39;J&#39; then 1</span>
<span class="sd"> else 0 end, 4) as qwer2,</span>
<span class="sd"> qwer3</span>
<span class="sd">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>select qwer1,
substr(&#39;blabla&#39;, case when asdf = &#39;J&#39; then 1
else 0 end, 4) as qwer2,
qwer3
</pre>
</div>
</div>

</div>
</div>

</div>
{% endraw %}

Expand Down Expand Up @@ -2981,7 +3047,7 @@ <h3 id="FROM">FROM<a class="anchor-link" href="#FROM"> </a></h3>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_from" class="doc_header"><code>format_from</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L240" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_from</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_from" class="doc_header"><code>format_from</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L230" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_from</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format FROM statement line <code>s</code></p>

Expand Down Expand Up @@ -3050,7 +3116,7 @@ <h3 id="(LEFT-/-RIGHT-/-INNER-/-OUTER)-JOIN">(LEFT / RIGHT / INNER / OUTER) JOIN


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_join" class="doc_header"><code>format_join</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L251" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_join</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_join" class="doc_header"><code>format_join</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L241" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_join</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format JOIN statement line <code>s</code></p>

Expand Down Expand Up @@ -3119,7 +3185,7 @@ <h3 id="ON">ON<a class="anchor-link" href="#ON"> </a></h3>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_on" class="doc_header"><code>format_on</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L257" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_on</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_on" class="doc_header"><code>format_on</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L247" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_on</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format ON statement line <code>s</code></p>

Expand Down Expand Up @@ -3318,7 +3384,7 @@ <h3 id="WHERE">WHERE<a class="anchor-link" href="#WHERE"> </a></h3>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_where" class="doc_header"><code>format_where</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L283" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_where</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_where" class="doc_header"><code>format_where</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L273" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_where</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format WHERE statement line <code>s</code></p>

Expand Down Expand Up @@ -3467,7 +3533,7 @@ <h2 id="Format-all-statements">Format all statements<a class="anchor-link" href=


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_statement_line" class="doc_header"><code>format_statement_line</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L312" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statement_line</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_statement_line" class="doc_header"><code>format_statement_line</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L302" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statement_line</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format statement line <code>s</code></p>

Expand Down Expand Up @@ -3611,7 +3677,7 @@ <h4 id="format_statement_line" class="doc_header"><code>format_statement_line</c


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_statements" class="doc_header"><code>format_statements</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L330" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statements</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_statements" class="doc_header"><code>format_statements</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L320" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_statements</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format statements lines <code>s</code></p>

Expand Down Expand Up @@ -3689,7 +3755,7 @@ <h3 id="Format-multiline-comments">Format multiline comments<a class="anchor-lin


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_multiline_comments" class="doc_header"><code>format_multiline_comments</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L340" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_multiline_comments</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_multiline_comments" class="doc_header"><code>format_multiline_comments</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L330" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_multiline_comments</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format multiline comments by replacing multiline comment [CI] by newline and adding indentation</p>

Expand Down Expand Up @@ -3728,7 +3794,7 @@ <h2 id="Putting-everything-together">Putting everything together<a class="anchor


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_simple_sql" class="doc_header"><code>format_simple_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L355" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_simple_sql</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_simple_sql" class="doc_header"><code>format_simple_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L345" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_simple_sql</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format a simple SQL query without subqueries <code>s</code></p>

Expand Down Expand Up @@ -3952,7 +4018,7 @@ <h3 id="Main-function-handling-queries-with-subqueries">Main function handling q


<div class="output_markdown rendered_html output_subarea ">
<h4 id="format_sql" class="doc_header"><code>format_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L369" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql</code>(<strong><code>s</code></strong>)</p>
<h4 id="format_sql" class="doc_header"><code>format_sql</code><a href="https://github.com/PabloRMira/sql_formatter/tree/master/sql_formatter/core.py#L359" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>format_sql</code>(<strong><code>s</code></strong>)</p>
</blockquote>
<p>Format SQL query with subqueries <code>s</code></p>

Expand Down
18 changes: 9 additions & 9 deletions docs/format_file.html
Original file line number Diff line number Diff line change
Expand Up @@ -896,11 +896,11 @@ <h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href=
<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>Something went wrong in file: /tmp/tmp8tafo08i
<pre>Something went wrong in file: /tmp/tmp9iqjtdct
[WARNING] Identified CREATE keyword more than twice within the same query at lines [(1, 7)]
You may have forgotten a semicolon (;) to delimit the queries
Aborting formatting for file /tmp/tmp8tafo08i
Aborting formatting for file /tmp/tmp8tafo08i
Aborting formatting for file /tmp/tmp9iqjtdct
Aborting formatting for file /tmp/tmp9iqjtdct
create or replace transient table my_table As
select asdf, Qwer, /* ; */
qwer2, -- ;
Expand Down Expand Up @@ -961,11 +961,11 @@ <h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href=
<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>Something went wrong in file: /tmp/tmp1p77zcwz
<pre>Something went wrong in file: /tmp/tmptxuyuw8q
[WARNING] Identified unbalanced parenthesis at lines [[3], [8]]
You should check your parenthesis
Aborting formatting for file /tmp/tmp1p77zcwz
Aborting formatting for file /tmp/tmp1p77zcwz
Aborting formatting for file /tmp/tmptxuyuw8q
Aborting formatting for file /tmp/tmptxuyuw8q
create or replace transient table my_table As
select asdf, Qwer, /* ; */
(qwer2, -- ;
Expand Down Expand Up @@ -1026,13 +1026,13 @@ <h4 id="format_sql_file" class="doc_header"><code>format_sql_file</code><a href=
<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>Something went wrong in file: /tmp/tmp5x3llo57
<pre>Something went wrong in file: /tmp/tmpwoud5teg
[WARNING] Identified CREATE keyword more than twice within the same query at lines [(1, 7)]
You may have forgotten a semicolon (;) to delimit the queries
[WARNING] Identified unbalanced parenthesis at lines [[3, 8]]
You should check your parenthesis
Aborting formatting for file /tmp/tmp5x3llo57
Aborting formatting for file /tmp/tmp5x3llo57
Aborting formatting for file /tmp/tmpwoud5teg
Aborting formatting for file /tmp/tmpwoud5teg
create or replace transient table my_table As
select asdf, Qwer, /* ; */
(qwer2, -- ;
Expand Down
Loading

0 comments on commit 416d533

Please sign in to comment.