diff options
Diffstat (limited to 'docs/sqlglot/optimizer/simplify.html')
-rw-r--r-- | docs/sqlglot/optimizer/simplify.html | 1356 |
1 files changed, 746 insertions, 610 deletions
diff --git a/docs/sqlglot/optimizer/simplify.html b/docs/sqlglot/optimizer/simplify.html index 3ff8bc9..1fb687d 100644 --- a/docs/sqlglot/optimizer/simplify.html +++ b/docs/sqlglot/optimizer/simplify.html @@ -145,6 +145,15 @@ <a class="function" href="#eval_boolean">eval_boolean</a> </li> <li> + <a class="function" href="#cast_as_date">cast_as_date</a> + </li> + <li> + <a class="function" href="#cast_as_datetime">cast_as_datetime</a> + </li> + <li> + <a class="function" href="#cast_value">cast_value</a> + </li> + <li> <a class="function" href="#extract_date">extract_date</a> </li> <li> @@ -577,359 +586,359 @@ </span><span id="L-387"><a href="#L-387"><span class="linenos">387</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">is_number</span> </span><span id="L-388"><a href="#L-388"><span class="linenos">388</span></a> </span><span id="L-389"><a href="#L-389"><span class="linenos">389</span></a> -</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a><span class="k">def</span> <span class="nf">_is_date</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> -</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> <span class="ow">and</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> +</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a><span class="k">def</span> <span class="nf">_is_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> +</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">)</span> <span class="ow">and</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> </span><span id="L-392"><a href="#L-392"><span class="linenos">392</span></a> </span><span id="L-393"><a href="#L-393"><span class="linenos">393</span></a> -</span><span id="L-394"><a href="#L-394"><span class="linenos">394</span></a><span class="k">def</span> <span class="nf">_is_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> -</span><span id="L-395"><a href="#L-395"><span class="linenos">395</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">)</span> <span class="ow">and</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> -</span><span id="L-396"><a href="#L-396"><span class="linenos">396</span></a> -</span><span id="L-397"><a href="#L-397"><span class="linenos">397</span></a> -</span><span id="L-398"><a href="#L-398"><span class="linenos">398</span></a><span class="nd">@catch</span><span class="p">(</span><span class="ne">ModuleNotFoundError</span><span class="p">,</span> <span class="n">UnsupportedUnit</span><span class="p">)</span> -</span><span id="L-399"><a href="#L-399"><span class="linenos">399</span></a><span class="k">def</span> <span class="nf">simplify_equality</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> -</span><span id="L-400"><a href="#L-400"><span class="linenos">400</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-401"><a href="#L-401"><span class="linenos">401</span></a><span class="sd"> Use the subtraction and addition properties of equality to simplify expressions:</span> -</span><span id="L-402"><a href="#L-402"><span class="linenos">402</span></a> -</span><span id="L-403"><a href="#L-403"><span class="linenos">403</span></a><span class="sd"> x + 1 = 3 becomes x = 2</span> -</span><span id="L-404"><a href="#L-404"><span class="linenos">404</span></a> -</span><span id="L-405"><a href="#L-405"><span class="linenos">405</span></a><span class="sd"> There are two binary operations in the above expression: + and =</span> -</span><span id="L-406"><a href="#L-406"><span class="linenos">406</span></a><span class="sd"> Here's how we reference all the operands in the code below:</span> -</span><span id="L-407"><a href="#L-407"><span class="linenos">407</span></a> -</span><span id="L-408"><a href="#L-408"><span class="linenos">408</span></a><span class="sd"> l r</span> -</span><span id="L-409"><a href="#L-409"><span class="linenos">409</span></a><span class="sd"> x + 1 = 3</span> -</span><span id="L-410"><a href="#L-410"><span class="linenos">410</span></a><span class="sd"> a b</span> -</span><span id="L-411"><a href="#L-411"><span class="linenos">411</span></a><span class="sd"> """</span> -</span><span id="L-412"><a href="#L-412"><span class="linenos">412</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> -</span><span id="L-413"><a href="#L-413"><span class="linenos">413</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> -</span><span id="L-414"><a href="#L-414"><span class="linenos">414</span></a> -</span><span id="L-415"><a href="#L-415"><span class="linenos">415</span></a> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_OPS</span><span class="p">:</span> -</span><span id="L-416"><a href="#L-416"><span class="linenos">416</span></a> <span class="k">pass</span> -</span><span id="L-417"><a href="#L-417"><span class="linenos">417</span></a> <span class="k">elif</span> <span class="n">r</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_OPS</span><span class="p">:</span> -</span><span id="L-418"><a href="#L-418"><span class="linenos">418</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">,</span> <span class="n">l</span> -</span><span id="L-419"><a href="#L-419"><span class="linenos">419</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-420"><a href="#L-420"><span class="linenos">420</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-421"><a href="#L-421"><span class="linenos">421</span></a> -</span><span id="L-422"><a href="#L-422"><span class="linenos">422</span></a> <span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> -</span><span id="L-423"><a href="#L-423"><span class="linenos">423</span></a> <span class="n">a_predicate</span> <span class="o">=</span> <span class="n">_is_number</span> -</span><span id="L-424"><a href="#L-424"><span class="linenos">424</span></a> <span class="n">b_predicate</span> <span class="o">=</span> <span class="n">_is_number</span> -</span><span id="L-425"><a href="#L-425"><span class="linenos">425</span></a> <span class="k">elif</span> <span class="n">_is_date</span><span class="p">(</span><span class="n">r</span><span class="p">):</span> -</span><span id="L-426"><a href="#L-426"><span class="linenos">426</span></a> <span class="n">a_predicate</span> <span class="o">=</span> <span class="n">_is_date</span> -</span><span id="L-427"><a href="#L-427"><span class="linenos">427</span></a> <span class="n">b_predicate</span> <span class="o">=</span> <span class="n">_is_interval</span> -</span><span id="L-428"><a href="#L-428"><span class="linenos">428</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-429"><a href="#L-429"><span class="linenos">429</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-430"><a href="#L-430"><span class="linenos">430</span></a> -</span><span id="L-431"><a href="#L-431"><span class="linenos">431</span></a> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_DATE_OPS</span><span class="p">:</span> -</span><span id="L-432"><a href="#L-432"><span class="linenos">432</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-433"><a href="#L-433"><span class="linenos">433</span></a> <span class="n">b</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">(</span> -</span><span id="L-434"><a href="#L-434"><span class="linenos">434</span></a> <span class="n">this</span><span class="o">=</span><span class="n">l</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> -</span><span id="L-435"><a href="#L-435"><span class="linenos">435</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">l</span><span class="o">.</span><span class="n">unit</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> -</span><span id="L-436"><a href="#L-436"><span class="linenos">436</span></a> <span class="p">)</span> +</span><span id="L-394"><a href="#L-394"><span class="linenos">394</span></a><span class="nd">@catch</span><span class="p">(</span><span class="ne">ModuleNotFoundError</span><span class="p">,</span> <span class="n">UnsupportedUnit</span><span class="p">)</span> +</span><span id="L-395"><a href="#L-395"><span class="linenos">395</span></a><span class="k">def</span> <span class="nf">simplify_equality</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-396"><a href="#L-396"><span class="linenos">396</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-397"><a href="#L-397"><span class="linenos">397</span></a><span class="sd"> Use the subtraction and addition properties of equality to simplify expressions:</span> +</span><span id="L-398"><a href="#L-398"><span class="linenos">398</span></a> +</span><span id="L-399"><a href="#L-399"><span class="linenos">399</span></a><span class="sd"> x + 1 = 3 becomes x = 2</span> +</span><span id="L-400"><a href="#L-400"><span class="linenos">400</span></a> +</span><span id="L-401"><a href="#L-401"><span class="linenos">401</span></a><span class="sd"> There are two binary operations in the above expression: + and =</span> +</span><span id="L-402"><a href="#L-402"><span class="linenos">402</span></a><span class="sd"> Here's how we reference all the operands in the code below:</span> +</span><span id="L-403"><a href="#L-403"><span class="linenos">403</span></a> +</span><span id="L-404"><a href="#L-404"><span class="linenos">404</span></a><span class="sd"> l r</span> +</span><span id="L-405"><a href="#L-405"><span class="linenos">405</span></a><span class="sd"> x + 1 = 3</span> +</span><span id="L-406"><a href="#L-406"><span class="linenos">406</span></a><span class="sd"> a b</span> +</span><span id="L-407"><a href="#L-407"><span class="linenos">407</span></a><span class="sd"> """</span> +</span><span id="L-408"><a href="#L-408"><span class="linenos">408</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> +</span><span id="L-409"><a href="#L-409"><span class="linenos">409</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-410"><a href="#L-410"><span class="linenos">410</span></a> +</span><span id="L-411"><a href="#L-411"><span class="linenos">411</span></a> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_OPS</span><span class="p">:</span> +</span><span id="L-412"><a href="#L-412"><span class="linenos">412</span></a> <span class="k">pass</span> +</span><span id="L-413"><a href="#L-413"><span class="linenos">413</span></a> <span class="k">elif</span> <span class="n">r</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_OPS</span><span class="p">:</span> +</span><span id="L-414"><a href="#L-414"><span class="linenos">414</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">,</span> <span class="n">l</span> +</span><span id="L-415"><a href="#L-415"><span class="linenos">415</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-416"><a href="#L-416"><span class="linenos">416</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-417"><a href="#L-417"><span class="linenos">417</span></a> +</span><span id="L-418"><a href="#L-418"><span class="linenos">418</span></a> <span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-419"><a href="#L-419"><span class="linenos">419</span></a> <span class="n">a_predicate</span> <span class="o">=</span> <span class="n">_is_number</span> +</span><span id="L-420"><a href="#L-420"><span class="linenos">420</span></a> <span class="n">b_predicate</span> <span class="o">=</span> <span class="n">_is_number</span> +</span><span id="L-421"><a href="#L-421"><span class="linenos">421</span></a> <span class="k">elif</span> <span class="n">_is_date_literal</span><span class="p">(</span><span class="n">r</span><span class="p">):</span> +</span><span id="L-422"><a href="#L-422"><span class="linenos">422</span></a> <span class="n">a_predicate</span> <span class="o">=</span> <span class="n">_is_date_literal</span> +</span><span id="L-423"><a href="#L-423"><span class="linenos">423</span></a> <span class="n">b_predicate</span> <span class="o">=</span> <span class="n">_is_interval</span> +</span><span id="L-424"><a href="#L-424"><span class="linenos">424</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-425"><a href="#L-425"><span class="linenos">425</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-426"><a href="#L-426"><span class="linenos">426</span></a> +</span><span id="L-427"><a href="#L-427"><span class="linenos">427</span></a> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_DATE_OPS</span><span class="p">:</span> +</span><span id="L-428"><a href="#L-428"><span class="linenos">428</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-429"><a href="#L-429"><span class="linenos">429</span></a> <span class="n">b</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">interval</span><span class="p">()</span> +</span><span id="L-430"><a href="#L-430"><span class="linenos">430</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-431"><a href="#L-431"><span class="linenos">431</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">l</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-432"><a href="#L-432"><span class="linenos">432</span></a> +</span><span id="L-433"><a href="#L-433"><span class="linenos">433</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">a_predicate</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b_predicate</span><span class="p">(</span><span class="n">b</span><span class="p">):</span> +</span><span id="L-434"><a href="#L-434"><span class="linenos">434</span></a> <span class="k">pass</span> +</span><span id="L-435"><a href="#L-435"><span class="linenos">435</span></a> <span class="k">elif</span> <span class="ow">not</span> <span class="n">a_predicate</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b_predicate</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> +</span><span id="L-436"><a href="#L-436"><span class="linenos">436</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> </span><span id="L-437"><a href="#L-437"><span class="linenos">437</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-438"><a href="#L-438"><span class="linenos">438</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">l</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-438"><a href="#L-438"><span class="linenos">438</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-439"><a href="#L-439"><span class="linenos">439</span></a> -</span><span id="L-440"><a href="#L-440"><span class="linenos">440</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">a_predicate</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b_predicate</span><span class="p">(</span><span class="n">b</span><span class="p">):</span> -</span><span id="L-441"><a href="#L-441"><span class="linenos">441</span></a> <span class="k">pass</span> -</span><span id="L-442"><a href="#L-442"><span class="linenos">442</span></a> <span class="k">elif</span> <span class="ow">not</span> <span class="n">a_predicate</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b_predicate</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> -</span><span id="L-443"><a href="#L-443"><span class="linenos">443</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> -</span><span id="L-444"><a href="#L-444"><span class="linenos">444</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-445"><a href="#L-445"><span class="linenos">445</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-446"><a href="#L-446"><span class="linenos">446</span></a> -</span><span id="L-447"><a href="#L-447"><span class="linenos">447</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span> -</span><span id="L-448"><a href="#L-448"><span class="linenos">448</span></a> <span class="n">this</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">INVERSE_OPS</span><span class="p">[</span><span class="n">l</span><span class="o">.</span><span class="vm">__class__</span><span class="p">](</span><span class="n">this</span><span class="o">=</span><span class="n">r</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">b</span><span class="p">)</span> -</span><span id="L-449"><a href="#L-449"><span class="linenos">449</span></a> <span class="p">)</span> -</span><span id="L-450"><a href="#L-450"><span class="linenos">450</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-451"><a href="#L-451"><span class="linenos">451</span></a> -</span><span id="L-452"><a href="#L-452"><span class="linenos">452</span></a> -</span><span id="L-453"><a href="#L-453"><span class="linenos">453</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> -</span><span id="L-454"><a href="#L-454"><span class="linenos">454</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> -</span><span id="L-455"><a href="#L-455"><span class="linenos">455</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">,</span> <span class="n">root</span><span class="p">)</span> -</span><span id="L-456"><a href="#L-456"><span class="linenos">456</span></a> -</span><span id="L-457"><a href="#L-457"><span class="linenos">457</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> -</span><span id="L-458"><a href="#L-458"><span class="linenos">458</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-459"><a href="#L-459"><span class="linenos">459</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> -</span><span id="L-460"><a href="#L-460"><span class="linenos">460</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> -</span><span id="L-461"><a href="#L-461"><span class="linenos">461</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> -</span><span id="L-462"><a href="#L-462"><span class="linenos">462</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> -</span><span id="L-463"><a href="#L-463"><span class="linenos">463</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> -</span><span id="L-464"><a href="#L-464"><span class="linenos">464</span></a> -</span><span id="L-465"><a href="#L-465"><span class="linenos">465</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-466"><a href="#L-466"><span class="linenos">466</span></a> -</span><span id="L-467"><a href="#L-467"><span class="linenos">467</span></a> -</span><span id="L-468"><a href="#L-468"><span class="linenos">468</span></a><span class="k">def</span> <span class="nf">_simplify_binary</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> -</span><span id="L-469"><a href="#L-469"><span class="linenos">469</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">):</span> -</span><span id="L-470"><a href="#L-470"><span class="linenos">470</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> -</span><span id="L-471"><a href="#L-471"><span class="linenos">471</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-472"><a href="#L-472"><span class="linenos">472</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">True</span> -</span><span id="L-473"><a href="#L-473"><span class="linenos">473</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-474"><a href="#L-474"><span class="linenos">474</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span> -</span><span id="L-475"><a href="#L-475"><span class="linenos">475</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">False</span> -</span><span id="L-476"><a href="#L-476"><span class="linenos">476</span></a> -</span><span id="L-477"><a href="#L-477"><span class="linenos">477</span></a> <span class="k">if</span> <span class="n">is_null</span><span class="p">(</span><span class="n">c</span><span class="p">):</span> -</span><span id="L-478"><a href="#L-478"><span class="linenos">478</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">):</span> -</span><span id="L-479"><a href="#L-479"><span class="linenos">479</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> -</span><span id="L-480"><a href="#L-480"><span class="linenos">480</span></a> <span class="k">if</span> <span class="n">is_null</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> -</span><span id="L-481"><a href="#L-481"><span class="linenos">481</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> -</span><span id="L-482"><a href="#L-482"><span class="linenos">482</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">NullSafeEQ</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NullSafeNEQ</span><span class="p">)):</span> -</span><span id="L-483"><a href="#L-483"><span class="linenos">483</span></a> <span class="k">return</span> <span class="kc">None</span> -</span><span id="L-484"><a href="#L-484"><span class="linenos">484</span></a> <span class="k">elif</span> <span class="n">is_null</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_null</span><span class="p">(</span><span class="n">b</span><span class="p">):</span> -</span><span id="L-485"><a href="#L-485"><span class="linenos">485</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> -</span><span id="L-486"><a href="#L-486"><span class="linenos">486</span></a> -</span><span id="L-487"><a href="#L-487"><span class="linenos">487</span></a> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_number</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> -</span><span id="L-488"><a href="#L-488"><span class="linenos">488</span></a> <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-489"><a href="#L-489"><span class="linenos">489</span></a> <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-490"><a href="#L-490"><span class="linenos">490</span></a> -</span><span id="L-491"><a href="#L-491"><span class="linenos">491</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> -</span><span id="L-492"><a href="#L-492"><span class="linenos">492</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-493"><a href="#L-493"><span class="linenos">493</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> -</span><span id="L-494"><a href="#L-494"><span class="linenos">494</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-495"><a href="#L-495"><span class="linenos">495</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">):</span> -</span><span id="L-496"><a href="#L-496"><span class="linenos">496</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-497"><a href="#L-497"><span class="linenos">497</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Div</span><span class="p">):</span> -</span><span id="L-498"><a href="#L-498"><span class="linenos">498</span></a> <span class="c1"># engines have differing int div behavior so intdiv is not safe</span> -</span><span id="L-499"><a href="#L-499"><span class="linenos">499</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span> -</span><span id="L-500"><a href="#L-500"><span class="linenos">500</span></a> <span class="k">return</span> <span class="kc">None</span> -</span><span id="L-501"><a href="#L-501"><span class="linenos">501</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">/</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-440"><a href="#L-440"><span class="linenos">440</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span> +</span><span id="L-441"><a href="#L-441"><span class="linenos">441</span></a> <span class="n">this</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">INVERSE_OPS</span><span class="p">[</span><span class="n">l</span><span class="o">.</span><span class="vm">__class__</span><span class="p">](</span><span class="n">this</span><span class="o">=</span><span class="n">r</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-442"><a href="#L-442"><span class="linenos">442</span></a> <span class="p">)</span> +</span><span id="L-443"><a href="#L-443"><span class="linenos">443</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-444"><a href="#L-444"><span class="linenos">444</span></a> +</span><span id="L-445"><a href="#L-445"><span class="linenos">445</span></a> +</span><span id="L-446"><a href="#L-446"><span class="linenos">446</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-447"><a href="#L-447"><span class="linenos">447</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-448"><a href="#L-448"><span class="linenos">448</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">,</span> <span class="n">root</span><span class="p">)</span> +</span><span id="L-449"><a href="#L-449"><span class="linenos">449</span></a> +</span><span id="L-450"><a href="#L-450"><span class="linenos">450</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> +</span><span id="L-451"><a href="#L-451"><span class="linenos">451</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-452"><a href="#L-452"><span class="linenos">452</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-453"><a href="#L-453"><span class="linenos">453</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-454"><a href="#L-454"><span class="linenos">454</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> +</span><span id="L-455"><a href="#L-455"><span class="linenos">455</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> +</span><span id="L-456"><a href="#L-456"><span class="linenos">456</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-457"><a href="#L-457"><span class="linenos">457</span></a> +</span><span id="L-458"><a href="#L-458"><span class="linenos">458</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-459"><a href="#L-459"><span class="linenos">459</span></a> +</span><span id="L-460"><a href="#L-460"><span class="linenos">460</span></a> +</span><span id="L-461"><a href="#L-461"><span class="linenos">461</span></a><span class="k">def</span> <span class="nf">_simplify_binary</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-462"><a href="#L-462"><span class="linenos">462</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">):</span> +</span><span id="L-463"><a href="#L-463"><span class="linenos">463</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="L-464"><a href="#L-464"><span class="linenos">464</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-465"><a href="#L-465"><span class="linenos">465</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-466"><a href="#L-466"><span class="linenos">466</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-467"><a href="#L-467"><span class="linenos">467</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span> +</span><span id="L-468"><a href="#L-468"><span class="linenos">468</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-469"><a href="#L-469"><span class="linenos">469</span></a> +</span><span id="L-470"><a href="#L-470"><span class="linenos">470</span></a> <span class="k">if</span> <span class="n">is_null</span><span class="p">(</span><span class="n">c</span><span class="p">):</span> +</span><span id="L-471"><a href="#L-471"><span class="linenos">471</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">):</span> +</span><span id="L-472"><a href="#L-472"><span class="linenos">472</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-473"><a href="#L-473"><span class="linenos">473</span></a> <span class="k">if</span> <span class="n">is_null</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> +</span><span id="L-474"><a href="#L-474"><span class="linenos">474</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-475"><a href="#L-475"><span class="linenos">475</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">NullSafeEQ</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NullSafeNEQ</span><span class="p">)):</span> +</span><span id="L-476"><a href="#L-476"><span class="linenos">476</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-477"><a href="#L-477"><span class="linenos">477</span></a> <span class="k">elif</span> <span class="n">is_null</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_null</span><span class="p">(</span><span class="n">b</span><span class="p">):</span> +</span><span id="L-478"><a href="#L-478"><span class="linenos">478</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-479"><a href="#L-479"><span class="linenos">479</span></a> +</span><span id="L-480"><a href="#L-480"><span class="linenos">480</span></a> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_number</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-481"><a href="#L-481"><span class="linenos">481</span></a> <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-482"><a href="#L-482"><span class="linenos">482</span></a> <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-483"><a href="#L-483"><span class="linenos">483</span></a> +</span><span id="L-484"><a href="#L-484"><span class="linenos">484</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-485"><a href="#L-485"><span class="linenos">485</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-486"><a href="#L-486"><span class="linenos">486</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> +</span><span id="L-487"><a href="#L-487"><span class="linenos">487</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-488"><a href="#L-488"><span class="linenos">488</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">):</span> +</span><span id="L-489"><a href="#L-489"><span class="linenos">489</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-490"><a href="#L-490"><span class="linenos">490</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Div</span><span class="p">):</span> +</span><span id="L-491"><a href="#L-491"><span class="linenos">491</span></a> <span class="c1"># engines have differing int div behavior so intdiv is not safe</span> +</span><span id="L-492"><a href="#L-492"><span class="linenos">492</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span> +</span><span id="L-493"><a href="#L-493"><span class="linenos">493</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-494"><a href="#L-494"><span class="linenos">494</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">/</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-495"><a href="#L-495"><span class="linenos">495</span></a> +</span><span id="L-496"><a href="#L-496"><span class="linenos">496</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-497"><a href="#L-497"><span class="linenos">497</span></a> +</span><span id="L-498"><a href="#L-498"><span class="linenos">498</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> +</span><span id="L-499"><a href="#L-499"><span class="linenos">499</span></a> <span class="k">return</span> <span class="n">boolean</span> +</span><span id="L-500"><a href="#L-500"><span class="linenos">500</span></a> <span class="k">elif</span> <span class="n">a</span><span class="o">.</span><span class="n">is_string</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> +</span><span id="L-501"><a href="#L-501"><span class="linenos">501</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> </span><span id="L-502"><a href="#L-502"><span class="linenos">502</span></a> -</span><span id="L-503"><a href="#L-503"><span class="linenos">503</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-504"><a href="#L-504"><span class="linenos">504</span></a> -</span><span id="L-505"><a href="#L-505"><span class="linenos">505</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> -</span><span id="L-506"><a href="#L-506"><span class="linenos">506</span></a> <span class="k">return</span> <span class="n">boolean</span> -</span><span id="L-507"><a href="#L-507"><span class="linenos">507</span></a> <span class="k">elif</span> <span class="n">a</span><span class="o">.</span><span class="n">is_string</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> -</span><span id="L-508"><a href="#L-508"><span class="linenos">508</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> -</span><span id="L-509"><a href="#L-509"><span class="linenos">509</span></a> -</span><span id="L-510"><a href="#L-510"><span class="linenos">510</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> -</span><span id="L-511"><a href="#L-511"><span class="linenos">511</span></a> <span class="k">return</span> <span class="n">boolean</span> -</span><span id="L-512"><a href="#L-512"><span class="linenos">512</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">):</span> -</span><span id="L-513"><a href="#L-513"><span class="linenos">513</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> -</span><span id="L-514"><a href="#L-514"><span class="linenos">514</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span><span class="p">:</span> -</span><span id="L-515"><a href="#L-515"><span class="linenos">515</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> -</span><span id="L-516"><a href="#L-516"><span class="linenos">516</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-517"><a href="#L-517"><span class="linenos">517</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> -</span><span id="L-518"><a href="#L-518"><span class="linenos">518</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-519"><a href="#L-519"><span class="linenos">519</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">):</span> -</span><span id="L-520"><a href="#L-520"><span class="linenos">520</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> -</span><span id="L-521"><a href="#L-521"><span class="linenos">521</span></a> <span class="c1"># you cannot subtract a date from an interval</span> -</span><span id="L-522"><a href="#L-522"><span class="linenos">522</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> -</span><span id="L-523"><a href="#L-523"><span class="linenos">523</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-503"><a href="#L-503"><span class="linenos">503</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> +</span><span id="L-504"><a href="#L-504"><span class="linenos">504</span></a> <span class="k">return</span> <span class="n">boolean</span> +</span><span id="L-505"><a href="#L-505"><span class="linenos">505</span></a> <span class="k">elif</span> <span class="n">_is_date_literal</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">):</span> +</span><span id="L-506"><a href="#L-506"><span class="linenos">506</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-507"><a href="#L-507"><span class="linenos">507</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span><span class="p">:</span> +</span><span id="L-508"><a href="#L-508"><span class="linenos">508</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-509"><a href="#L-509"><span class="linenos">509</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-510"><a href="#L-510"><span class="linenos">510</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> +</span><span id="L-511"><a href="#L-511"><span class="linenos">511</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-512"><a href="#L-512"><span class="linenos">512</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">)</span> <span class="ow">and</span> <span class="n">_is_date_literal</span><span class="p">(</span><span class="n">b</span><span class="p">):</span> +</span><span id="L-513"><a href="#L-513"><span class="linenos">513</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-514"><a href="#L-514"><span class="linenos">514</span></a> <span class="c1"># you cannot subtract a date from an interval</span> +</span><span id="L-515"><a href="#L-515"><span class="linenos">515</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-516"><a href="#L-516"><span class="linenos">516</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-517"><a href="#L-517"><span class="linenos">517</span></a> +</span><span id="L-518"><a href="#L-518"><span class="linenos">518</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-519"><a href="#L-519"><span class="linenos">519</span></a> +</span><span id="L-520"><a href="#L-520"><span class="linenos">520</span></a> +</span><span id="L-521"><a href="#L-521"><span class="linenos">521</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-522"><a href="#L-522"><span class="linenos">522</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="L-523"><a href="#L-523"><span class="linenos">523</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-524"><a href="#L-524"><span class="linenos">524</span></a> -</span><span id="L-525"><a href="#L-525"><span class="linenos">525</span></a> <span class="k">return</span> <span class="kc">None</span> -</span><span id="L-526"><a href="#L-526"><span class="linenos">526</span></a> +</span><span id="L-525"><a href="#L-525"><span class="linenos">525</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-526"><a href="#L-526"><span class="linenos">526</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> </span><span id="L-527"><a href="#L-527"><span class="linenos">527</span></a> -</span><span id="L-528"><a href="#L-528"><span class="linenos">528</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="L-529"><a href="#L-529"><span class="linenos">529</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> -</span><span id="L-530"><a href="#L-530"><span class="linenos">530</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-531"><a href="#L-531"><span class="linenos">531</span></a> -</span><span id="L-532"><a href="#L-532"><span class="linenos">532</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-533"><a href="#L-533"><span class="linenos">533</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> -</span><span id="L-534"><a href="#L-534"><span class="linenos">534</span></a> -</span><span id="L-535"><a href="#L-535"><span class="linenos">535</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span> -</span><span id="L-536"><a href="#L-536"><span class="linenos">536</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> -</span><span id="L-537"><a href="#L-537"><span class="linenos">537</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> -</span><span id="L-538"><a href="#L-538"><span class="linenos">538</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> -</span><span id="L-539"><a href="#L-539"><span class="linenos">539</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">))</span> -</span><span id="L-540"><a href="#L-540"><span class="linenos">540</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">))</span> -</span><span id="L-541"><a href="#L-541"><span class="linenos">541</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">))</span> -</span><span id="L-542"><a href="#L-542"><span class="linenos">542</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">)))</span> -</span><span id="L-543"><a href="#L-543"><span class="linenos">543</span></a> <span class="p">):</span> -</span><span id="L-544"><a href="#L-544"><span class="linenos">544</span></a> <span class="k">return</span> <span class="n">this</span> -</span><span id="L-545"><a href="#L-545"><span class="linenos">545</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-528"><a href="#L-528"><span class="linenos">528</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span> +</span><span id="L-529"><a href="#L-529"><span class="linenos">529</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> +</span><span id="L-530"><a href="#L-530"><span class="linenos">530</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> +</span><span id="L-531"><a href="#L-531"><span class="linenos">531</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="L-532"><a href="#L-532"><span class="linenos">532</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">))</span> +</span><span id="L-533"><a href="#L-533"><span class="linenos">533</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">))</span> +</span><span id="L-534"><a href="#L-534"><span class="linenos">534</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">))</span> +</span><span id="L-535"><a href="#L-535"><span class="linenos">535</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">)))</span> +</span><span id="L-536"><a href="#L-536"><span class="linenos">536</span></a> <span class="p">):</span> +</span><span id="L-537"><a href="#L-537"><span class="linenos">537</span></a> <span class="k">return</span> <span class="n">this</span> +</span><span id="L-538"><a href="#L-538"><span class="linenos">538</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-539"><a href="#L-539"><span class="linenos">539</span></a> +</span><span id="L-540"><a href="#L-540"><span class="linenos">540</span></a> +</span><span id="L-541"><a href="#L-541"><span class="linenos">541</span></a><span class="n">CONSTANTS</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-542"><a href="#L-542"><span class="linenos">542</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">,</span> +</span><span id="L-543"><a href="#L-543"><span class="linenos">543</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">,</span> +</span><span id="L-544"><a href="#L-544"><span class="linenos">544</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">,</span> +</span><span id="L-545"><a href="#L-545"><span class="linenos">545</span></a><span class="p">)</span> </span><span id="L-546"><a href="#L-546"><span class="linenos">546</span></a> </span><span id="L-547"><a href="#L-547"><span class="linenos">547</span></a> -</span><span id="L-548"><a href="#L-548"><span class="linenos">548</span></a><span class="n">CONSTANTS</span> <span class="o">=</span> <span class="p">(</span> -</span><span id="L-549"><a href="#L-549"><span class="linenos">549</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">,</span> -</span><span id="L-550"><a href="#L-550"><span class="linenos">550</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">,</span> -</span><span id="L-551"><a href="#L-551"><span class="linenos">551</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">,</span> -</span><span id="L-552"><a href="#L-552"><span class="linenos">552</span></a><span class="p">)</span> -</span><span id="L-553"><a href="#L-553"><span class="linenos">553</span></a> -</span><span id="L-554"><a href="#L-554"><span class="linenos">554</span></a> -</span><span id="L-555"><a href="#L-555"><span class="linenos">555</span></a><span class="k">def</span> <span class="nf">simplify_coalesce</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="L-556"><a href="#L-556"><span class="linenos">556</span></a> <span class="c1"># COALESCE(x) -> x</span> -</span><span id="L-557"><a href="#L-557"><span class="linenos">557</span></a> <span class="k">if</span> <span class="p">(</span> -</span><span id="L-558"><a href="#L-558"><span class="linenos">558</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">)</span> -</span><span id="L-559"><a href="#L-559"><span class="linenos">559</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="L-560"><a href="#L-560"><span class="linenos">560</span></a> <span class="c1"># COALESCE is also used as a Spark partitioning hint</span> -</span><span id="L-561"><a href="#L-561"><span class="linenos">561</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> -</span><span id="L-562"><a href="#L-562"><span class="linenos">562</span></a> <span class="p">):</span> -</span><span id="L-563"><a href="#L-563"><span class="linenos">563</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-564"><a href="#L-564"><span class="linenos">564</span></a> -</span><span id="L-565"><a href="#L-565"><span class="linenos">565</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> -</span><span id="L-566"><a href="#L-566"><span class="linenos">566</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-567"><a href="#L-567"><span class="linenos">567</span></a> -</span><span id="L-568"><a href="#L-568"><span class="linenos">568</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> -</span><span id="L-569"><a href="#L-569"><span class="linenos">569</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> -</span><span id="L-570"><a href="#L-570"><span class="linenos">570</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> -</span><span id="L-571"><a href="#L-571"><span class="linenos">571</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> -</span><span id="L-572"><a href="#L-572"><span class="linenos">572</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> -</span><span id="L-573"><a href="#L-573"><span class="linenos">573</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> -</span><span id="L-574"><a href="#L-574"><span class="linenos">574</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-575"><a href="#L-575"><span class="linenos">575</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-576"><a href="#L-576"><span class="linenos">576</span></a> -</span><span id="L-577"><a href="#L-577"><span class="linenos">577</span></a> <span class="c1"># This transformation is valid for non-constants,</span> -</span><span id="L-578"><a href="#L-578"><span class="linenos">578</span></a> <span class="c1"># but it really only does anything if they are both constants.</span> -</span><span id="L-579"><a href="#L-579"><span class="linenos">579</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> +</span><span id="L-548"><a href="#L-548"><span class="linenos">548</span></a><span class="k">def</span> <span class="nf">simplify_coalesce</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-549"><a href="#L-549"><span class="linenos">549</span></a> <span class="c1"># COALESCE(x) -> x</span> +</span><span id="L-550"><a href="#L-550"><span class="linenos">550</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-551"><a href="#L-551"><span class="linenos">551</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">)</span> +</span><span id="L-552"><a href="#L-552"><span class="linenos">552</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="L-553"><a href="#L-553"><span class="linenos">553</span></a> <span class="c1"># COALESCE is also used as a Spark partitioning hint</span> +</span><span id="L-554"><a href="#L-554"><span class="linenos">554</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> +</span><span id="L-555"><a href="#L-555"><span class="linenos">555</span></a> <span class="p">):</span> +</span><span id="L-556"><a href="#L-556"><span class="linenos">556</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-557"><a href="#L-557"><span class="linenos">557</span></a> +</span><span id="L-558"><a href="#L-558"><span class="linenos">558</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> +</span><span id="L-559"><a href="#L-559"><span class="linenos">559</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-560"><a href="#L-560"><span class="linenos">560</span></a> +</span><span id="L-561"><a href="#L-561"><span class="linenos">561</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> +</span><span id="L-562"><a href="#L-562"><span class="linenos">562</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> +</span><span id="L-563"><a href="#L-563"><span class="linenos">563</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-564"><a href="#L-564"><span class="linenos">564</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> +</span><span id="L-565"><a href="#L-565"><span class="linenos">565</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-566"><a href="#L-566"><span class="linenos">566</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> +</span><span id="L-567"><a href="#L-567"><span class="linenos">567</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-568"><a href="#L-568"><span class="linenos">568</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-569"><a href="#L-569"><span class="linenos">569</span></a> +</span><span id="L-570"><a href="#L-570"><span class="linenos">570</span></a> <span class="c1"># This transformation is valid for non-constants,</span> +</span><span id="L-571"><a href="#L-571"><span class="linenos">571</span></a> <span class="c1"># but it really only does anything if they are both constants.</span> +</span><span id="L-572"><a href="#L-572"><span class="linenos">572</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> +</span><span id="L-573"><a href="#L-573"><span class="linenos">573</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-574"><a href="#L-574"><span class="linenos">574</span></a> +</span><span id="L-575"><a href="#L-575"><span class="linenos">575</span></a> <span class="c1"># Find the first constant arg</span> +</span><span id="L-576"><a href="#L-576"><span class="linenos">576</span></a> <span class="k">for</span> <span class="n">arg_index</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">):</span> +</span><span id="L-577"><a href="#L-577"><span class="linenos">577</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> +</span><span id="L-578"><a href="#L-578"><span class="linenos">578</span></a> <span class="k">break</span> +</span><span id="L-579"><a href="#L-579"><span class="linenos">579</span></a> <span class="k">else</span><span class="p">:</span> </span><span id="L-580"><a href="#L-580"><span class="linenos">580</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-581"><a href="#L-581"><span class="linenos">581</span></a> -</span><span id="L-582"><a href="#L-582"><span class="linenos">582</span></a> <span class="c1"># Find the first constant arg</span> -</span><span id="L-583"><a href="#L-583"><span class="linenos">583</span></a> <span class="k">for</span> <span class="n">arg_index</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">):</span> -</span><span id="L-584"><a href="#L-584"><span class="linenos">584</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> -</span><span id="L-585"><a href="#L-585"><span class="linenos">585</span></a> <span class="k">break</span> -</span><span id="L-586"><a href="#L-586"><span class="linenos">586</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-587"><a href="#L-587"><span class="linenos">587</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-588"><a href="#L-588"><span class="linenos">588</span></a> -</span><span id="L-589"><a href="#L-589"><span class="linenos">589</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">[:</span><span class="n">arg_index</span><span class="p">])</span> -</span><span id="L-590"><a href="#L-590"><span class="linenos">590</span></a> -</span><span id="L-591"><a href="#L-591"><span class="linenos">591</span></a> <span class="c1"># Remove the COALESCE function. This is an optimization, skipping a simplify iteration,</span> -</span><span id="L-592"><a href="#L-592"><span class="linenos">592</span></a> <span class="c1"># since we already remove COALESCE at the top of this function.</span> -</span><span id="L-593"><a href="#L-593"><span class="linenos">593</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">coalesce</span> <span class="k">if</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span> <span class="k">else</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-594"><a href="#L-594"><span class="linenos">594</span></a> -</span><span id="L-595"><a href="#L-595"><span class="linenos">595</span></a> <span class="c1"># This expression is more complex than when we started, but it will get simplified further</span> -</span><span id="L-596"><a href="#L-596"><span class="linenos">596</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">paren</span><span class="p">(</span> -</span><span id="L-597"><a href="#L-597"><span class="linenos">597</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span> -</span><span id="L-598"><a href="#L-598"><span class="linenos">598</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="L-599"><a href="#L-599"><span class="linenos">599</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">())</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> -</span><span id="L-600"><a href="#L-600"><span class="linenos">600</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> -</span><span id="L-601"><a href="#L-601"><span class="linenos">601</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-602"><a href="#L-602"><span class="linenos">602</span></a> <span class="p">),</span> -</span><span id="L-603"><a href="#L-603"><span class="linenos">603</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="L-604"><a href="#L-604"><span class="linenos">604</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()),</span> -</span><span id="L-605"><a href="#L-605"><span class="linenos">605</span></a> <span class="nb">type</span><span class="p">(</span><span class="n">expression</span><span class="p">)(</span><span class="n">this</span><span class="o">=</span><span class="n">arg</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> -</span><span id="L-606"><a href="#L-606"><span class="linenos">606</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-607"><a href="#L-607"><span class="linenos">607</span></a> <span class="p">),</span> -</span><span id="L-608"><a href="#L-608"><span class="linenos">608</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-609"><a href="#L-609"><span class="linenos">609</span></a> <span class="p">)</span> -</span><span id="L-610"><a href="#L-610"><span class="linenos">610</span></a> <span class="p">)</span> -</span><span id="L-611"><a href="#L-611"><span class="linenos">611</span></a> -</span><span id="L-612"><a href="#L-612"><span class="linenos">612</span></a> -</span><span id="L-613"><a href="#L-613"><span class="linenos">613</span></a><span class="n">CONCATS</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Concat</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DPipe</span><span class="p">)</span> -</span><span id="L-614"><a href="#L-614"><span class="linenos">614</span></a><span class="n">SAFE_CONCATS</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeDPipe</span><span class="p">)</span> -</span><span id="L-615"><a href="#L-615"><span class="linenos">615</span></a> -</span><span id="L-616"><a href="#L-616"><span class="linenos">616</span></a> -</span><span id="L-617"><a href="#L-617"><span class="linenos">617</span></a><span class="k">def</span> <span class="nf">simplify_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="L-618"><a href="#L-618"><span class="linenos">618</span></a><span class="w"> </span><span class="sd">"""Reduces all groups that contain string literals by concatenating them."""</span> -</span><span id="L-619"><a href="#L-619"><span class="linenos">619</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">CONCATS</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">):</span> -</span><span id="L-620"><a href="#L-620"><span class="linenos">620</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-621"><a href="#L-621"><span class="linenos">621</span></a> -</span><span id="L-622"><a href="#L-622"><span class="linenos">622</span></a> <span class="n">new_args</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="L-623"><a href="#L-623"><span class="linenos">623</span></a> <span class="k">for</span> <span class="n">is_string_group</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span> -</span><span id="L-624"><a href="#L-624"><span class="linenos">624</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> <span class="ow">or</span> <span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">is_string</span> -</span><span id="L-625"><a href="#L-625"><span class="linenos">625</span></a> <span class="p">):</span> -</span><span id="L-626"><a href="#L-626"><span class="linenos">626</span></a> <span class="k">if</span> <span class="n">is_string_group</span><span class="p">:</span> -</span><span id="L-627"><a href="#L-627"><span class="linenos">627</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">string</span> <span class="ow">in</span> <span class="n">group</span><span class="p">)))</span> -</span><span id="L-628"><a href="#L-628"><span class="linenos">628</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-629"><a href="#L-629"><span class="linenos">629</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> +</span><span id="L-582"><a href="#L-582"><span class="linenos">582</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">[:</span><span class="n">arg_index</span><span class="p">])</span> +</span><span id="L-583"><a href="#L-583"><span class="linenos">583</span></a> +</span><span id="L-584"><a href="#L-584"><span class="linenos">584</span></a> <span class="c1"># Remove the COALESCE function. This is an optimization, skipping a simplify iteration,</span> +</span><span id="L-585"><a href="#L-585"><span class="linenos">585</span></a> <span class="c1"># since we already remove COALESCE at the top of this function.</span> +</span><span id="L-586"><a href="#L-586"><span class="linenos">586</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">coalesce</span> <span class="k">if</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span> <span class="k">else</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-587"><a href="#L-587"><span class="linenos">587</span></a> +</span><span id="L-588"><a href="#L-588"><span class="linenos">588</span></a> <span class="c1"># This expression is more complex than when we started, but it will get simplified further</span> +</span><span id="L-589"><a href="#L-589"><span class="linenos">589</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">paren</span><span class="p">(</span> +</span><span id="L-590"><a href="#L-590"><span class="linenos">590</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span> +</span><span id="L-591"><a href="#L-591"><span class="linenos">591</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="L-592"><a href="#L-592"><span class="linenos">592</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">())</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="L-593"><a href="#L-593"><span class="linenos">593</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> +</span><span id="L-594"><a href="#L-594"><span class="linenos">594</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-595"><a href="#L-595"><span class="linenos">595</span></a> <span class="p">),</span> +</span><span id="L-596"><a href="#L-596"><span class="linenos">596</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="L-597"><a href="#L-597"><span class="linenos">597</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()),</span> +</span><span id="L-598"><a href="#L-598"><span class="linenos">598</span></a> <span class="nb">type</span><span class="p">(</span><span class="n">expression</span><span class="p">)(</span><span class="n">this</span><span class="o">=</span><span class="n">arg</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> +</span><span id="L-599"><a href="#L-599"><span class="linenos">599</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-600"><a href="#L-600"><span class="linenos">600</span></a> <span class="p">),</span> +</span><span id="L-601"><a href="#L-601"><span class="linenos">601</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-602"><a href="#L-602"><span class="linenos">602</span></a> <span class="p">)</span> +</span><span id="L-603"><a href="#L-603"><span class="linenos">603</span></a> <span class="p">)</span> +</span><span id="L-604"><a href="#L-604"><span class="linenos">604</span></a> +</span><span id="L-605"><a href="#L-605"><span class="linenos">605</span></a> +</span><span id="L-606"><a href="#L-606"><span class="linenos">606</span></a><span class="n">CONCATS</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Concat</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DPipe</span><span class="p">)</span> +</span><span id="L-607"><a href="#L-607"><span class="linenos">607</span></a><span class="n">SAFE_CONCATS</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeDPipe</span><span class="p">)</span> +</span><span id="L-608"><a href="#L-608"><span class="linenos">608</span></a> +</span><span id="L-609"><a href="#L-609"><span class="linenos">609</span></a> +</span><span id="L-610"><a href="#L-610"><span class="linenos">610</span></a><span class="k">def</span> <span class="nf">simplify_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-611"><a href="#L-611"><span class="linenos">611</span></a><span class="w"> </span><span class="sd">"""Reduces all groups that contain string literals by concatenating them."""</span> +</span><span id="L-612"><a href="#L-612"><span class="linenos">612</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">CONCATS</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">):</span> +</span><span id="L-613"><a href="#L-613"><span class="linenos">613</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-614"><a href="#L-614"><span class="linenos">614</span></a> +</span><span id="L-615"><a href="#L-615"><span class="linenos">615</span></a> <span class="n">new_args</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-616"><a href="#L-616"><span class="linenos">616</span></a> <span class="k">for</span> <span class="n">is_string_group</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span> +</span><span id="L-617"><a href="#L-617"><span class="linenos">617</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> <span class="ow">or</span> <span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">is_string</span> +</span><span id="L-618"><a href="#L-618"><span class="linenos">618</span></a> <span class="p">):</span> +</span><span id="L-619"><a href="#L-619"><span class="linenos">619</span></a> <span class="k">if</span> <span class="n">is_string_group</span><span class="p">:</span> +</span><span id="L-620"><a href="#L-620"><span class="linenos">620</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">string</span> <span class="ow">in</span> <span class="n">group</span><span class="p">)))</span> +</span><span id="L-621"><a href="#L-621"><span class="linenos">621</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-622"><a href="#L-622"><span class="linenos">622</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> +</span><span id="L-623"><a href="#L-623"><span class="linenos">623</span></a> +</span><span id="L-624"><a href="#L-624"><span class="linenos">624</span></a> <span class="c1"># Ensures we preserve the right concat type, i.e. whether it's "safe" or not</span> +</span><span id="L-625"><a href="#L-625"><span class="linenos">625</span></a> <span class="n">concat_type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">SAFE_CONCATS</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span> +</span><span id="L-626"><a href="#L-626"><span class="linenos">626</span></a> <span class="k">return</span> <span class="n">new_args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">concat_type</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_args</span><span class="p">)</span> +</span><span id="L-627"><a href="#L-627"><span class="linenos">627</span></a> +</span><span id="L-628"><a href="#L-628"><span class="linenos">628</span></a> +</span><span id="L-629"><a href="#L-629"><span class="linenos">629</span></a><span class="n">DateRange</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">Tuple</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]</span> </span><span id="L-630"><a href="#L-630"><span class="linenos">630</span></a> -</span><span id="L-631"><a href="#L-631"><span class="linenos">631</span></a> <span class="c1"># Ensures we preserve the right concat type, i.e. whether it's "safe" or not</span> -</span><span id="L-632"><a href="#L-632"><span class="linenos">632</span></a> <span class="n">concat_type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">SAFE_CONCATS</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span> -</span><span id="L-633"><a href="#L-633"><span class="linenos">633</span></a> <span class="k">return</span> <span class="n">new_args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">concat_type</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_args</span><span class="p">)</span> -</span><span id="L-634"><a href="#L-634"><span class="linenos">634</span></a> +</span><span id="L-631"><a href="#L-631"><span class="linenos">631</span></a> +</span><span id="L-632"><a href="#L-632"><span class="linenos">632</span></a><span class="k">def</span> <span class="nf">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">DateRange</span><span class="p">]:</span> +</span><span id="L-633"><a href="#L-633"><span class="linenos">633</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-634"><a href="#L-634"><span class="linenos">634</span></a><span class="sd"> Get the date range for a DATE_TRUNC equality comparison:</span> </span><span id="L-635"><a href="#L-635"><span class="linenos">635</span></a> -</span><span id="L-636"><a href="#L-636"><span class="linenos">636</span></a><span class="n">DateRange</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">Tuple</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]</span> -</span><span id="L-637"><a href="#L-637"><span class="linenos">637</span></a> -</span><span id="L-638"><a href="#L-638"><span class="linenos">638</span></a> -</span><span id="L-639"><a href="#L-639"><span class="linenos">639</span></a><span class="k">def</span> <span class="nf">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">DateRange</span><span class="p">]:</span> -</span><span id="L-640"><a href="#L-640"><span class="linenos">640</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-641"><a href="#L-641"><span class="linenos">641</span></a><span class="sd"> Get the date range for a DATE_TRUNC equality comparison:</span> +</span><span id="L-636"><a href="#L-636"><span class="linenos">636</span></a><span class="sd"> Example:</span> +</span><span id="L-637"><a href="#L-637"><span class="linenos">637</span></a><span class="sd"> _datetrunc_range(date(2021-01-01), 'year') == (date(2021-01-01), date(2022-01-01))</span> +</span><span id="L-638"><a href="#L-638"><span class="linenos">638</span></a><span class="sd"> Returns:</span> +</span><span id="L-639"><a href="#L-639"><span class="linenos">639</span></a><span class="sd"> tuple of [min, max) or None if a value can never be equal to `date` for `unit`</span> +</span><span id="L-640"><a href="#L-640"><span class="linenos">640</span></a><span class="sd"> """</span> +</span><span id="L-641"><a href="#L-641"><span class="linenos">641</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> </span><span id="L-642"><a href="#L-642"><span class="linenos">642</span></a> -</span><span id="L-643"><a href="#L-643"><span class="linenos">643</span></a><span class="sd"> Example:</span> -</span><span id="L-644"><a href="#L-644"><span class="linenos">644</span></a><span class="sd"> _datetrunc_range(date(2021-01-01), 'year') == (date(2021-01-01), date(2022-01-01))</span> -</span><span id="L-645"><a href="#L-645"><span class="linenos">645</span></a><span class="sd"> Returns:</span> -</span><span id="L-646"><a href="#L-646"><span class="linenos">646</span></a><span class="sd"> tuple of [min, max) or None if a value can never be equal to `date` for `unit`</span> -</span><span id="L-647"><a href="#L-647"><span class="linenos">647</span></a><span class="sd"> """</span> -</span><span id="L-648"><a href="#L-648"><span class="linenos">648</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="L-643"><a href="#L-643"><span class="linenos">643</span></a> <span class="k">if</span> <span class="n">date</span> <span class="o">!=</span> <span class="n">floor</span><span class="p">:</span> +</span><span id="L-644"><a href="#L-644"><span class="linenos">644</span></a> <span class="c1"># This will always be False, except for NULL values.</span> +</span><span id="L-645"><a href="#L-645"><span class="linenos">645</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-646"><a href="#L-646"><span class="linenos">646</span></a> +</span><span id="L-647"><a href="#L-647"><span class="linenos">647</span></a> <span class="k">return</span> <span class="n">floor</span><span class="p">,</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> +</span><span id="L-648"><a href="#L-648"><span class="linenos">648</span></a> </span><span id="L-649"><a href="#L-649"><span class="linenos">649</span></a> -</span><span id="L-650"><a href="#L-650"><span class="linenos">650</span></a> <span class="k">if</span> <span class="n">date</span> <span class="o">!=</span> <span class="n">floor</span><span class="p">:</span> -</span><span id="L-651"><a href="#L-651"><span class="linenos">651</span></a> <span class="c1"># This will always be False, except for NULL values.</span> -</span><span id="L-652"><a href="#L-652"><span class="linenos">652</span></a> <span class="k">return</span> <span class="kc">None</span> -</span><span id="L-653"><a href="#L-653"><span class="linenos">653</span></a> -</span><span id="L-654"><a href="#L-654"><span class="linenos">654</span></a> <span class="k">return</span> <span class="n">floor</span><span class="p">,</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> -</span><span id="L-655"><a href="#L-655"><span class="linenos">655</span></a> -</span><span id="L-656"><a href="#L-656"><span class="linenos">656</span></a> -</span><span id="L-657"><a href="#L-657"><span class="linenos">657</span></a><span class="k">def</span> <span class="nf">_datetrunc_eq_expression</span><span class="p">(</span><span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">drange</span><span class="p">:</span> <span class="n">DateRange</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> -</span><span id="L-658"><a href="#L-658"><span class="linenos">658</span></a><span class="w"> </span><span class="sd">"""Get the logical expression for a date range"""</span> -</span><span id="L-659"><a href="#L-659"><span class="linenos">659</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="L-660"><a href="#L-660"><span class="linenos">660</span></a> <span class="n">left</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> -</span><span id="L-661"><a href="#L-661"><span class="linenos">661</span></a> <span class="n">left</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> -</span><span id="L-662"><a href="#L-662"><span class="linenos">662</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-663"><a href="#L-663"><span class="linenos">663</span></a> <span class="p">)</span> -</span><span id="L-664"><a href="#L-664"><span class="linenos">664</span></a> +</span><span id="L-650"><a href="#L-650"><span class="linenos">650</span></a><span class="k">def</span> <span class="nf">_datetrunc_eq_expression</span><span class="p">(</span><span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">drange</span><span class="p">:</span> <span class="n">DateRange</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-651"><a href="#L-651"><span class="linenos">651</span></a><span class="w"> </span><span class="sd">"""Get the logical expression for a date range"""</span> +</span><span id="L-652"><a href="#L-652"><span class="linenos">652</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="L-653"><a href="#L-653"><span class="linenos">653</span></a> <span class="n">left</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> +</span><span id="L-654"><a href="#L-654"><span class="linenos">654</span></a> <span class="n">left</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> +</span><span id="L-655"><a href="#L-655"><span class="linenos">655</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-656"><a href="#L-656"><span class="linenos">656</span></a> <span class="p">)</span> +</span><span id="L-657"><a href="#L-657"><span class="linenos">657</span></a> +</span><span id="L-658"><a href="#L-658"><span class="linenos">658</span></a> +</span><span id="L-659"><a href="#L-659"><span class="linenos">659</span></a><span class="k">def</span> <span class="nf">_datetrunc_eq</span><span class="p">(</span> +</span><span id="L-660"><a href="#L-660"><span class="linenos">660</span></a> <span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span> +</span><span id="L-661"><a href="#L-661"><span class="linenos">661</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> +</span><span id="L-662"><a href="#L-662"><span class="linenos">662</span></a> <span class="n">drange</span> <span class="o">=</span> <span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="L-663"><a href="#L-663"><span class="linenos">663</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">drange</span><span class="p">:</span> +</span><span id="L-664"><a href="#L-664"><span class="linenos">664</span></a> <span class="k">return</span> <span class="kc">None</span> </span><span id="L-665"><a href="#L-665"><span class="linenos">665</span></a> -</span><span id="L-666"><a href="#L-666"><span class="linenos">666</span></a><span class="k">def</span> <span class="nf">_datetrunc_eq</span><span class="p">(</span> -</span><span id="L-667"><a href="#L-667"><span class="linenos">667</span></a> <span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span> -</span><span id="L-668"><a href="#L-668"><span class="linenos">668</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> -</span><span id="L-669"><a href="#L-669"><span class="linenos">669</span></a> <span class="n">drange</span> <span class="o">=</span> <span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> -</span><span id="L-670"><a href="#L-670"><span class="linenos">670</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">drange</span><span class="p">:</span> -</span><span id="L-671"><a href="#L-671"><span class="linenos">671</span></a> <span class="k">return</span> <span class="kc">None</span> -</span><span id="L-672"><a href="#L-672"><span class="linenos">672</span></a> -</span><span id="L-673"><a href="#L-673"><span class="linenos">673</span></a> <span class="k">return</span> <span class="n">_datetrunc_eq_expression</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">drange</span><span class="p">)</span> -</span><span id="L-674"><a href="#L-674"><span class="linenos">674</span></a> +</span><span id="L-666"><a href="#L-666"><span class="linenos">666</span></a> <span class="k">return</span> <span class="n">_datetrunc_eq_expression</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">drange</span><span class="p">)</span> +</span><span id="L-667"><a href="#L-667"><span class="linenos">667</span></a> +</span><span id="L-668"><a href="#L-668"><span class="linenos">668</span></a> +</span><span id="L-669"><a href="#L-669"><span class="linenos">669</span></a><span class="k">def</span> <span class="nf">_datetrunc_neq</span><span class="p">(</span> +</span><span id="L-670"><a href="#L-670"><span class="linenos">670</span></a> <span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span> +</span><span id="L-671"><a href="#L-671"><span class="linenos">671</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> +</span><span id="L-672"><a href="#L-672"><span class="linenos">672</span></a> <span class="n">drange</span> <span class="o">=</span> <span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="L-673"><a href="#L-673"><span class="linenos">673</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">drange</span><span class="p">:</span> +</span><span id="L-674"><a href="#L-674"><span class="linenos">674</span></a> <span class="k">return</span> <span class="kc">None</span> </span><span id="L-675"><a href="#L-675"><span class="linenos">675</span></a> -</span><span id="L-676"><a href="#L-676"><span class="linenos">676</span></a><span class="k">def</span> <span class="nf">_datetrunc_neq</span><span class="p">(</span> -</span><span id="L-677"><a href="#L-677"><span class="linenos">677</span></a> <span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">date</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span> -</span><span id="L-678"><a href="#L-678"><span class="linenos">678</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> -</span><span id="L-679"><a href="#L-679"><span class="linenos">679</span></a> <span class="n">drange</span> <span class="o">=</span> <span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> -</span><span id="L-680"><a href="#L-680"><span class="linenos">680</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">drange</span><span class="p">:</span> -</span><span id="L-681"><a href="#L-681"><span class="linenos">681</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-676"><a href="#L-676"><span class="linenos">676</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="L-677"><a href="#L-677"><span class="linenos">677</span></a> <span class="n">left</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> +</span><span id="L-678"><a href="#L-678"><span class="linenos">678</span></a> <span class="n">left</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> +</span><span id="L-679"><a href="#L-679"><span class="linenos">679</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-680"><a href="#L-680"><span class="linenos">680</span></a> <span class="p">)</span> +</span><span id="L-681"><a href="#L-681"><span class="linenos">681</span></a> </span><span id="L-682"><a href="#L-682"><span class="linenos">682</span></a> -</span><span id="L-683"><a href="#L-683"><span class="linenos">683</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="L-684"><a href="#L-684"><span class="linenos">684</span></a> <span class="n">left</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> -</span><span id="L-685"><a href="#L-685"><span class="linenos">685</span></a> <span class="n">left</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">drange</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> -</span><span id="L-686"><a href="#L-686"><span class="linenos">686</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-687"><a href="#L-687"><span class="linenos">687</span></a> <span class="p">)</span> -</span><span id="L-688"><a href="#L-688"><span class="linenos">688</span></a> -</span><span id="L-689"><a href="#L-689"><span class="linenos">689</span></a> -</span><span id="L-690"><a href="#L-690"><span class="linenos">690</span></a><span class="n">DateTruncBinaryTransform</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[</span> -</span><span id="L-691"><a href="#L-691"><span class="linenos">691</span></a> <span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="nb">str</span><span class="p">],</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]</span> -</span><span id="L-692"><a href="#L-692"><span class="linenos">692</span></a><span class="p">]</span> -</span><span id="L-693"><a href="#L-693"><span class="linenos">693</span></a><span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Type</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">],</span> <span class="n">DateTruncBinaryTransform</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-694"><a href="#L-694"><span class="linenos">694</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)),</span> -</span><span id="L-695"><a href="#L-695"><span class="linenos">695</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">u</span><span class="p">)),</span> -</span><span id="L-696"><a href="#L-696"><span class="linenos">696</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">u</span><span class="p">)),</span> -</span><span id="L-697"><a href="#L-697"><span class="linenos">697</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)),</span> -</span><span id="L-698"><a href="#L-698"><span class="linenos">698</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">:</span> <span class="n">_datetrunc_eq</span><span class="p">,</span> -</span><span id="L-699"><a href="#L-699"><span class="linenos">699</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">:</span> <span class="n">_datetrunc_neq</span><span class="p">,</span> -</span><span id="L-700"><a href="#L-700"><span class="linenos">700</span></a><span class="p">}</span> -</span><span id="L-701"><a href="#L-701"><span class="linenos">701</span></a><span class="n">DATETRUNC_COMPARISONS</span> <span class="o">=</span> <span class="p">{</span><span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">,</span> <span class="o">*</span><span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">}</span> -</span><span id="L-702"><a href="#L-702"><span class="linenos">702</span></a> -</span><span id="L-703"><a href="#L-703"><span class="linenos">703</span></a> -</span><span id="L-704"><a href="#L-704"><span class="linenos">704</span></a><span class="k">def</span> <span class="nf">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">right</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> -</span><span id="L-705"><a href="#L-705"><span class="linenos">705</span></a> <span class="k">return</span> <span class="p">(</span> -</span><span id="L-706"><a href="#L-706"><span class="linenos">706</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DateTrunc</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TimestampTrunc</span><span class="p">))</span> -</span><span id="L-707"><a href="#L-707"><span class="linenos">707</span></a> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">right</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> -</span><span id="L-708"><a href="#L-708"><span class="linenos">708</span></a> <span class="ow">and</span> <span class="n">right</span><span class="o">.</span><span class="n">is_type</span><span class="p">(</span><span class="o">*</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEMPORAL_TYPES</span><span class="p">)</span> -</span><span id="L-709"><a href="#L-709"><span class="linenos">709</span></a> <span class="p">)</span> -</span><span id="L-710"><a href="#L-710"><span class="linenos">710</span></a> +</span><span id="L-683"><a href="#L-683"><span class="linenos">683</span></a><span class="n">DateTruncBinaryTransform</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[</span> +</span><span id="L-684"><a href="#L-684"><span class="linenos">684</span></a> <span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="nb">str</span><span class="p">],</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]</span> +</span><span id="L-685"><a href="#L-685"><span class="linenos">685</span></a><span class="p">]</span> +</span><span id="L-686"><a href="#L-686"><span class="linenos">686</span></a><span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Type</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">],</span> <span class="n">DateTruncBinaryTransform</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-687"><a href="#L-687"><span class="linenos">687</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)),</span> +</span><span id="L-688"><a href="#L-688"><span class="linenos">688</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">u</span><span class="p">)),</span> +</span><span id="L-689"><a href="#L-689"><span class="linenos">689</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o"><</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">u</span><span class="p">)),</span> +</span><span id="L-690"><a href="#L-690"><span class="linenos">690</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">l</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="n">l</span> <span class="o">>=</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">u</span><span class="p">)),</span> +</span><span id="L-691"><a href="#L-691"><span class="linenos">691</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">:</span> <span class="n">_datetrunc_eq</span><span class="p">,</span> +</span><span id="L-692"><a href="#L-692"><span class="linenos">692</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">:</span> <span class="n">_datetrunc_neq</span><span class="p">,</span> +</span><span id="L-693"><a href="#L-693"><span class="linenos">693</span></a><span class="p">}</span> +</span><span id="L-694"><a href="#L-694"><span class="linenos">694</span></a><span class="n">DATETRUNC_COMPARISONS</span> <span class="o">=</span> <span class="p">{</span><span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">,</span> <span class="o">*</span><span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">}</span> +</span><span id="L-695"><a href="#L-695"><span class="linenos">695</span></a> +</span><span id="L-696"><a href="#L-696"><span class="linenos">696</span></a> +</span><span id="L-697"><a href="#L-697"><span class="linenos">697</span></a><span class="k">def</span> <span class="nf">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">left</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">right</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> +</span><span id="L-698"><a href="#L-698"><span class="linenos">698</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DateTrunc</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TimestampTrunc</span><span class="p">))</span> <span class="ow">and</span> <span class="n">_is_date_literal</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-699"><a href="#L-699"><span class="linenos">699</span></a> +</span><span id="L-700"><a href="#L-700"><span class="linenos">700</span></a> +</span><span id="L-701"><a href="#L-701"><span class="linenos">701</span></a><span class="nd">@catch</span><span class="p">(</span><span class="ne">ModuleNotFoundError</span><span class="p">,</span> <span class="n">UnsupportedUnit</span><span class="p">)</span> +</span><span id="L-702"><a href="#L-702"><span class="linenos">702</span></a><span class="k">def</span> <span class="nf">simplify_datetrunc_predicate</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-703"><a href="#L-703"><span class="linenos">703</span></a><span class="w"> </span><span class="sd">"""Simplify expressions like `DATE_TRUNC('year', x) >= CAST('2021-01-01' AS DATE)`"""</span> +</span><span id="L-704"><a href="#L-704"><span class="linenos">704</span></a> <span class="n">comparison</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span> +</span><span id="L-705"><a href="#L-705"><span class="linenos">705</span></a> +</span><span id="L-706"><a href="#L-706"><span class="linenos">706</span></a> <span class="k">if</span> <span class="n">comparison</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">DATETRUNC_COMPARISONS</span><span class="p">:</span> +</span><span id="L-707"><a href="#L-707"><span class="linenos">707</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-708"><a href="#L-708"><span class="linenos">708</span></a> +</span><span id="L-709"><a href="#L-709"><span class="linenos">709</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="L-710"><a href="#L-710"><span class="linenos">710</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> </span><span id="L-711"><a href="#L-711"><span class="linenos">711</span></a> -</span><span id="L-712"><a href="#L-712"><span class="linenos">712</span></a><span class="nd">@catch</span><span class="p">(</span><span class="ne">ModuleNotFoundError</span><span class="p">,</span> <span class="n">UnsupportedUnit</span><span class="p">)</span> -</span><span id="L-713"><a href="#L-713"><span class="linenos">713</span></a><span class="k">def</span> <span class="nf">simplify_datetrunc_predicate</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> -</span><span id="L-714"><a href="#L-714"><span class="linenos">714</span></a><span class="w"> </span><span class="sd">"""Simplify expressions like `DATE_TRUNC('year', x) >= CAST('2021-01-01' AS DATE)`"""</span> -</span><span id="L-715"><a href="#L-715"><span class="linenos">715</span></a> <span class="n">comparison</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span> -</span><span id="L-716"><a href="#L-716"><span class="linenos">716</span></a> -</span><span id="L-717"><a href="#L-717"><span class="linenos">717</span></a> <span class="k">if</span> <span class="n">comparison</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">DATETRUNC_COMPARISONS</span><span class="p">:</span> -</span><span id="L-718"><a href="#L-718"><span class="linenos">718</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-712"><a href="#L-712"><span class="linenos">712</span></a> <span class="k">if</span> <span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> +</span><span id="L-713"><a href="#L-713"><span class="linenos">713</span></a> <span class="k">pass</span> +</span><span id="L-714"><a href="#L-714"><span class="linenos">714</span></a> <span class="k">elif</span> <span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span> +</span><span id="L-715"><a href="#L-715"><span class="linenos">715</span></a> <span class="n">comparison</span> <span class="o">=</span> <span class="n">INVERSE_COMPARISONS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">comparison</span><span class="p">,</span> <span class="n">comparison</span><span class="p">)</span> +</span><span id="L-716"><a href="#L-716"><span class="linenos">716</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">,</span> <span class="n">l</span> +</span><span id="L-717"><a href="#L-717"><span class="linenos">717</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-718"><a href="#L-718"><span class="linenos">718</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-719"><a href="#L-719"><span class="linenos">719</span></a> -</span><span id="L-720"><a href="#L-720"><span class="linenos">720</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> -</span><span id="L-721"><a href="#L-721"><span class="linenos">721</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-720"><a href="#L-720"><span class="linenos">720</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">unit</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> +</span><span id="L-721"><a href="#L-721"><span class="linenos">721</span></a> <span class="n">date</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> </span><span id="L-722"><a href="#L-722"><span class="linenos">722</span></a> -</span><span id="L-723"><a href="#L-723"><span class="linenos">723</span></a> <span class="k">if</span> <span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> -</span><span id="L-724"><a href="#L-724"><span class="linenos">724</span></a> <span class="k">pass</span> -</span><span id="L-725"><a href="#L-725"><span class="linenos">725</span></a> <span class="k">elif</span> <span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span> -</span><span id="L-726"><a href="#L-726"><span class="linenos">726</span></a> <span class="n">comparison</span> <span class="o">=</span> <span class="n">INVERSE_COMPARISONS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">comparison</span><span class="p">,</span> <span class="n">comparison</span><span class="p">)</span> -</span><span id="L-727"><a href="#L-727"><span class="linenos">727</span></a> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="p">,</span> <span class="n">l</span> -</span><span id="L-728"><a href="#L-728"><span class="linenos">728</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-729"><a href="#L-729"><span class="linenos">729</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-723"><a href="#L-723"><span class="linenos">723</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">date</span><span class="p">:</span> +</span><span id="L-724"><a href="#L-724"><span class="linenos">724</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-725"><a href="#L-725"><span class="linenos">725</span></a> +</span><span id="L-726"><a href="#L-726"><span class="linenos">726</span></a> <span class="k">return</span> <span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">[</span><span class="n">comparison</span><span class="p">](</span><span class="n">l</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> <span class="ow">or</span> <span class="n">expression</span> +</span><span id="L-727"><a href="#L-727"><span class="linenos">727</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">):</span> +</span><span id="L-728"><a href="#L-728"><span class="linenos">728</span></a> <span class="n">l</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-729"><a href="#L-729"><span class="linenos">729</span></a> <span class="n">rs</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> </span><span id="L-730"><a href="#L-730"><span class="linenos">730</span></a> -</span><span id="L-731"><a href="#L-731"><span class="linenos">731</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">unit</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> -</span><span id="L-732"><a href="#L-732"><span class="linenos">732</span></a> <span class="n">date</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> +</span><span id="L-731"><a href="#L-731"><span class="linenos">731</span></a> <span class="k">if</span> <span class="n">rs</span> <span class="ow">and</span> <span class="nb">all</span><span class="p">(</span><span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rs</span><span class="p">):</span> +</span><span id="L-732"><a href="#L-732"><span class="linenos">732</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">unit</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> </span><span id="L-733"><a href="#L-733"><span class="linenos">733</span></a> -</span><span id="L-734"><a href="#L-734"><span class="linenos">734</span></a> <span class="k">return</span> <span class="n">DATETRUNC_BINARY_COMPARISONS</span><span class="p">[</span><span class="n">comparison</span><span class="p">](</span><span class="n">l</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> <span class="ow">or</span> <span class="n">expression</span> -</span><span id="L-735"><a href="#L-735"><span class="linenos">735</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">):</span> -</span><span id="L-736"><a href="#L-736"><span class="linenos">736</span></a> <span class="n">l</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-737"><a href="#L-737"><span class="linenos">737</span></a> <span class="n">rs</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="L-738"><a href="#L-738"><span class="linenos">738</span></a> -</span><span id="L-739"><a href="#L-739"><span class="linenos">739</span></a> <span class="k">if</span> <span class="nb">all</span><span class="p">(</span><span class="n">_is_datetrunc_predicate</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rs</span><span class="p">):</span> -</span><span id="L-740"><a href="#L-740"><span class="linenos">740</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">unit</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> -</span><span id="L-741"><a href="#L-741"><span class="linenos">741</span></a> -</span><span id="L-742"><a href="#L-742"><span class="linenos">742</span></a> <span class="n">ranges</span> <span class="o">=</span> <span class="p">[</span><span class="n">r</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="p">[</span><span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">extract_date</span><span class="p">(</span><span class="n">r</span><span class="p">),</span> <span class="n">unit</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rs</span><span class="p">]</span> <span class="k">if</span> <span class="n">r</span><span class="p">]</span> +</span><span id="L-734"><a href="#L-734"><span class="linenos">734</span></a> <span class="n">ranges</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-735"><a href="#L-735"><span class="linenos">735</span></a> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rs</span><span class="p">:</span> +</span><span id="L-736"><a href="#L-736"><span class="linenos">736</span></a> <span class="n">date</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> +</span><span id="L-737"><a href="#L-737"><span class="linenos">737</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">date</span><span class="p">:</span> +</span><span id="L-738"><a href="#L-738"><span class="linenos">738</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-739"><a href="#L-739"><span class="linenos">739</span></a> <span class="n">drange</span> <span class="o">=</span> <span class="n">_datetrunc_range</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="L-740"><a href="#L-740"><span class="linenos">740</span></a> <span class="k">if</span> <span class="n">drange</span><span class="p">:</span> +</span><span id="L-741"><a href="#L-741"><span class="linenos">741</span></a> <span class="n">ranges</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">drange</span><span class="p">)</span> +</span><span id="L-742"><a href="#L-742"><span class="linenos">742</span></a> </span><span id="L-743"><a href="#L-743"><span class="linenos">743</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">ranges</span><span class="p">:</span> </span><span id="L-744"><a href="#L-744"><span class="linenos">744</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-745"><a href="#L-745"><span class="linenos">745</span></a> @@ -1001,118 +1010,161 @@ </span><span id="L-811"><a href="#L-811"><span class="linenos">811</span></a> <span class="k">return</span> <span class="kc">None</span> </span><span id="L-812"><a href="#L-812"><span class="linenos">812</span></a> </span><span id="L-813"><a href="#L-813"><span class="linenos">813</span></a> -</span><span id="L-814"><a href="#L-814"><span class="linenos">814</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">):</span> -</span><span id="L-815"><a href="#L-815"><span class="linenos">815</span></a> <span class="c1"># The "fromisoformat" conversion could fail if the cast is used on an identifier,</span> -</span><span id="L-816"><a href="#L-816"><span class="linenos">816</span></a> <span class="c1"># so in that case we can't extract the date.</span> -</span><span id="L-817"><a href="#L-817"><span class="linenos">817</span></a> <span class="k">try</span><span class="p">:</span> -</span><span id="L-818"><a href="#L-818"><span class="linenos">818</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">:</span> -</span><span id="L-819"><a href="#L-819"><span class="linenos">819</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-820"><a href="#L-820"><span class="linenos">820</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> -</span><span id="L-821"><a href="#L-821"><span class="linenos">821</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-822"><a href="#L-822"><span class="linenos">822</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> -</span><span id="L-823"><a href="#L-823"><span class="linenos">823</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-814"><a href="#L-814"><span class="linenos">814</span></a><span class="k">def</span> <span class="nf">cast_as_date</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]:</span> +</span><span id="L-815"><a href="#L-815"><span class="linenos">815</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span> +</span><span id="L-816"><a href="#L-816"><span class="linenos">816</span></a> <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> +</span><span id="L-817"><a href="#L-817"><span class="linenos">817</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">):</span> +</span><span id="L-818"><a href="#L-818"><span class="linenos">818</span></a> <span class="k">return</span> <span class="n">value</span> +</span><span id="L-819"><a href="#L-819"><span class="linenos">819</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-820"><a href="#L-820"><span class="linenos">820</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> +</span><span id="L-821"><a href="#L-821"><span class="linenos">821</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> +</span><span id="L-822"><a href="#L-822"><span class="linenos">822</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-823"><a href="#L-823"><span class="linenos">823</span></a> </span><span id="L-824"><a href="#L-824"><span class="linenos">824</span></a> -</span><span id="L-825"><a href="#L-825"><span class="linenos">825</span></a> -</span><span id="L-826"><a href="#L-826"><span class="linenos">826</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="L-827"><a href="#L-827"><span class="linenos">827</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-828"><a href="#L-828"><span class="linenos">828</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> -</span><span id="L-829"><a href="#L-829"><span class="linenos">829</span></a> +</span><span id="L-825"><a href="#L-825"><span class="linenos">825</span></a><span class="k">def</span> <span class="nf">cast_as_datetime</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">]:</span> +</span><span id="L-826"><a href="#L-826"><span class="linenos">826</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span> +</span><span id="L-827"><a href="#L-827"><span class="linenos">827</span></a> <span class="k">return</span> <span class="n">value</span> +</span><span id="L-828"><a href="#L-828"><span class="linenos">828</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">):</span> +</span><span id="L-829"><a href="#L-829"><span class="linenos">829</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">day</span><span class="p">)</span> </span><span id="L-830"><a href="#L-830"><span class="linenos">830</span></a> <span class="k">try</span><span class="p">:</span> -</span><span id="L-831"><a href="#L-831"><span class="linenos">831</span></a> <span class="k">return</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-832"><a href="#L-832"><span class="linenos">832</span></a> <span class="k">except</span> <span class="p">(</span><span class="n">UnsupportedUnit</span><span class="p">,</span> <span class="ne">ModuleNotFoundError</span><span class="p">):</span> +</span><span id="L-831"><a href="#L-831"><span class="linenos">831</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="L-832"><a href="#L-832"><span class="linenos">832</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> </span><span id="L-833"><a href="#L-833"><span class="linenos">833</span></a> <span class="k">return</span> <span class="kc">None</span> </span><span id="L-834"><a href="#L-834"><span class="linenos">834</span></a> </span><span id="L-835"><a href="#L-835"><span class="linenos">835</span></a> -</span><span id="L-836"><a href="#L-836"><span class="linenos">836</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> -</span><span id="L-837"><a href="#L-837"><span class="linenos">837</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> -</span><span id="L-838"><a href="#L-838"><span class="linenos">838</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> -</span><span id="L-839"><a href="#L-839"><span class="linenos">839</span></a> <span class="s2">"DATETIME"</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> <span class="k">else</span> <span class="s2">"DATE"</span><span class="p">,</span> -</span><span id="L-840"><a href="#L-840"><span class="linenos">840</span></a> <span class="p">)</span> -</span><span id="L-841"><a href="#L-841"><span class="linenos">841</span></a> -</span><span id="L-842"><a href="#L-842"><span class="linenos">842</span></a> -</span><span id="L-843"><a href="#L-843"><span class="linenos">843</span></a><span class="k">def</span> <span class="nf">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> -</span><span id="L-844"><a href="#L-844"><span class="linenos">844</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> +</span><span id="L-836"><a href="#L-836"><span class="linenos">836</span></a><span class="k">def</span> <span class="nf">cast_value</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]]:</span> +</span><span id="L-837"><a href="#L-837"><span class="linenos">837</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span> +</span><span id="L-838"><a href="#L-838"><span class="linenos">838</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-839"><a href="#L-839"><span class="linenos">839</span></a> <span class="k">if</span> <span class="n">to</span><span class="o">.</span><span class="n">is_type</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">):</span> +</span><span id="L-840"><a href="#L-840"><span class="linenos">840</span></a> <span class="k">return</span> <span class="n">cast_as_date</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="L-841"><a href="#L-841"><span class="linenos">841</span></a> <span class="k">if</span> <span class="n">to</span><span class="o">.</span><span class="n">is_type</span><span class="p">(</span><span class="o">*</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEMPORAL_TYPES</span><span class="p">):</span> +</span><span id="L-842"><a href="#L-842"><span class="linenos">842</span></a> <span class="k">return</span> <span class="n">cast_as_datetime</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="L-843"><a href="#L-843"><span class="linenos">843</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-844"><a href="#L-844"><span class="linenos">844</span></a> </span><span id="L-845"><a href="#L-845"><span class="linenos">845</span></a> -</span><span id="L-846"><a href="#L-846"><span class="linenos">846</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> -</span><span id="L-847"><a href="#L-847"><span class="linenos">847</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-848"><a href="#L-848"><span class="linenos">848</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> -</span><span id="L-849"><a href="#L-849"><span class="linenos">849</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">3</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-850"><a href="#L-850"><span class="linenos">850</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> -</span><span id="L-851"><a href="#L-851"><span class="linenos">851</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-852"><a href="#L-852"><span class="linenos">852</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> -</span><span id="L-853"><a href="#L-853"><span class="linenos">853</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-854"><a href="#L-854"><span class="linenos">854</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> -</span><span id="L-855"><a href="#L-855"><span class="linenos">855</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-856"><a href="#L-856"><span class="linenos">856</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"hour"</span><span class="p">:</span> -</span><span id="L-857"><a href="#L-857"><span class="linenos">857</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-858"><a href="#L-858"><span class="linenos">858</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"minute"</span><span class="p">:</span> -</span><span id="L-859"><a href="#L-859"><span class="linenos">859</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="L-860"><a href="#L-860"><span class="linenos">860</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"second"</span><span class="p">:</span> -</span><span id="L-861"><a href="#L-861"><span class="linenos">861</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-846"><a href="#L-846"><span class="linenos">846</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]]:</span> +</span><span id="L-847"><a href="#L-847"><span class="linenos">847</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">):</span> +</span><span id="L-848"><a href="#L-848"><span class="linenos">848</span></a> <span class="n">to</span> <span class="o">=</span> <span class="n">cast</span><span class="o">.</span><span class="n">to</span> +</span><span id="L-849"><a href="#L-849"><span class="linenos">849</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">):</span> +</span><span id="L-850"><a href="#L-850"><span class="linenos">850</span></a> <span class="n">to</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">)</span> +</span><span id="L-851"><a href="#L-851"><span class="linenos">851</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-852"><a href="#L-852"><span class="linenos">852</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-853"><a href="#L-853"><span class="linenos">853</span></a> +</span><span id="L-854"><a href="#L-854"><span class="linenos">854</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">):</span> +</span><span id="L-855"><a href="#L-855"><span class="linenos">855</span></a> <span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span> <span class="o">=</span> <span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-856"><a href="#L-856"><span class="linenos">856</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">)):</span> +</span><span id="L-857"><a href="#L-857"><span class="linenos">857</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="L-858"><a href="#L-858"><span class="linenos">858</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-859"><a href="#L-859"><span class="linenos">859</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-860"><a href="#L-860"><span class="linenos">860</span></a> <span class="k">return</span> <span class="n">cast_value</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">to</span><span class="p">)</span> +</span><span id="L-861"><a href="#L-861"><span class="linenos">861</span></a> </span><span id="L-862"><a href="#L-862"><span class="linenos">862</span></a> -</span><span id="L-863"><a href="#L-863"><span class="linenos">863</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> -</span><span id="L-864"><a href="#L-864"><span class="linenos">864</span></a> +</span><span id="L-863"><a href="#L-863"><span class="linenos">863</span></a><span class="k">def</span> <span class="nf">_is_date_literal</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span> +</span><span id="L-864"><a href="#L-864"><span class="linenos">864</span></a> <span class="k">return</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> </span><span id="L-865"><a href="#L-865"><span class="linenos">865</span></a> -</span><span id="L-866"><a href="#L-866"><span class="linenos">866</span></a><span class="k">def</span> <span class="nf">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> -</span><span id="L-867"><a href="#L-867"><span class="linenos">867</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> -</span><span id="L-868"><a href="#L-868"><span class="linenos">868</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-869"><a href="#L-869"><span class="linenos">869</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> -</span><span id="L-870"><a href="#L-870"><span class="linenos">870</span></a> <span class="k">if</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">3</span><span class="p">:</span> -</span><span id="L-871"><a href="#L-871"><span class="linenos">871</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-872"><a href="#L-872"><span class="linenos">872</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">6</span><span class="p">:</span> -</span><span id="L-873"><a href="#L-873"><span class="linenos">873</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-874"><a href="#L-874"><span class="linenos">874</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">9</span><span class="p">:</span> -</span><span id="L-875"><a href="#L-875"><span class="linenos">875</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-876"><a href="#L-876"><span class="linenos">876</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-877"><a href="#L-877"><span class="linenos">877</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-878"><a href="#L-878"><span class="linenos">878</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> -</span><span id="L-879"><a href="#L-879"><span class="linenos">879</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="L-880"><a href="#L-880"><span class="linenos">880</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> -</span><span id="L-881"><a href="#L-881"><span class="linenos">881</span></a> <span class="c1"># Assuming week starts on Monday (0) and ends on Sunday (6)</span> -</span><span id="L-882"><a href="#L-882"><span class="linenos">882</span></a> <span class="k">return</span> <span class="n">d</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">())</span> -</span><span id="L-883"><a href="#L-883"><span class="linenos">883</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> -</span><span id="L-884"><a href="#L-884"><span class="linenos">884</span></a> <span class="k">return</span> <span class="n">d</span> +</span><span id="L-866"><a href="#L-866"><span class="linenos">866</span></a> +</span><span id="L-867"><a href="#L-867"><span class="linenos">867</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-868"><a href="#L-868"><span class="linenos">868</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-869"><a href="#L-869"><span class="linenos">869</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> +</span><span id="L-870"><a href="#L-870"><span class="linenos">870</span></a> +</span><span id="L-871"><a href="#L-871"><span class="linenos">871</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-872"><a href="#L-872"><span class="linenos">872</span></a> <span class="k">return</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-873"><a href="#L-873"><span class="linenos">873</span></a> <span class="k">except</span> <span class="p">(</span><span class="n">UnsupportedUnit</span><span class="p">,</span> <span class="ne">ModuleNotFoundError</span><span class="p">):</span> +</span><span id="L-874"><a href="#L-874"><span class="linenos">874</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-875"><a href="#L-875"><span class="linenos">875</span></a> +</span><span id="L-876"><a href="#L-876"><span class="linenos">876</span></a> +</span><span id="L-877"><a href="#L-877"><span class="linenos">877</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> +</span><span id="L-878"><a href="#L-878"><span class="linenos">878</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> +</span><span id="L-879"><a href="#L-879"><span class="linenos">879</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> +</span><span id="L-880"><a href="#L-880"><span class="linenos">880</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span> +</span><span id="L-881"><a href="#L-881"><span class="linenos">881</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> +</span><span id="L-882"><a href="#L-882"><span class="linenos">882</span></a> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">,</span> +</span><span id="L-883"><a href="#L-883"><span class="linenos">883</span></a> <span class="p">)</span> +</span><span id="L-884"><a href="#L-884"><span class="linenos">884</span></a> </span><span id="L-885"><a href="#L-885"><span class="linenos">885</span></a> -</span><span id="L-886"><a href="#L-886"><span class="linenos">886</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> -</span><span id="L-887"><a href="#L-887"><span class="linenos">887</span></a> +</span><span id="L-886"><a href="#L-886"><span class="linenos">886</span></a><span class="k">def</span> <span class="nf">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> +</span><span id="L-887"><a href="#L-887"><span class="linenos">887</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> </span><span id="L-888"><a href="#L-888"><span class="linenos">888</span></a> -</span><span id="L-889"><a href="#L-889"><span class="linenos">889</span></a><span class="k">def</span> <span class="nf">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> -</span><span id="L-890"><a href="#L-890"><span class="linenos">890</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> -</span><span id="L-891"><a href="#L-891"><span class="linenos">891</span></a> -</span><span id="L-892"><a href="#L-892"><span class="linenos">892</span></a> <span class="k">if</span> <span class="n">floor</span> <span class="o">==</span> <span class="n">d</span><span class="p">:</span> -</span><span id="L-893"><a href="#L-893"><span class="linenos">893</span></a> <span class="k">return</span> <span class="n">d</span> -</span><span id="L-894"><a href="#L-894"><span class="linenos">894</span></a> -</span><span id="L-895"><a href="#L-895"><span class="linenos">895</span></a> <span class="k">return</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> -</span><span id="L-896"><a href="#L-896"><span class="linenos">896</span></a> -</span><span id="L-897"><a href="#L-897"><span class="linenos">897</span></a> -</span><span id="L-898"><a href="#L-898"><span class="linenos">898</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> -</span><span id="L-899"><a href="#L-899"><span class="linenos">899</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> -</span><span id="L-900"><a href="#L-900"><span class="linenos">900</span></a> -</span><span id="L-901"><a href="#L-901"><span class="linenos">901</span></a> -</span><span id="L-902"><a href="#L-902"><span class="linenos">902</span></a><span class="k">def</span> <span class="nf">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">simplifier</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> -</span><span id="L-903"><a href="#L-903"><span class="linenos">903</span></a> <span class="k">if</span> <span class="n">root</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">same_parent</span><span class="p">:</span> -</span><span id="L-904"><a href="#L-904"><span class="linenos">904</span></a> <span class="n">operands</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="L-905"><a href="#L-905"><span class="linenos">905</span></a> <span class="n">queue</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">unnest</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span> -</span><span id="L-906"><a href="#L-906"><span class="linenos">906</span></a> <span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">queue</span><span class="p">)</span> +</span><span id="L-889"><a href="#L-889"><span class="linenos">889</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="L-890"><a href="#L-890"><span class="linenos">890</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-891"><a href="#L-891"><span class="linenos">891</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> +</span><span id="L-892"><a href="#L-892"><span class="linenos">892</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">3</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-893"><a href="#L-893"><span class="linenos">893</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="L-894"><a href="#L-894"><span class="linenos">894</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-895"><a href="#L-895"><span class="linenos">895</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="L-896"><a href="#L-896"><span class="linenos">896</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-897"><a href="#L-897"><span class="linenos">897</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="L-898"><a href="#L-898"><span class="linenos">898</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-899"><a href="#L-899"><span class="linenos">899</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"hour"</span><span class="p">:</span> +</span><span id="L-900"><a href="#L-900"><span class="linenos">900</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-901"><a href="#L-901"><span class="linenos">901</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"minute"</span><span class="p">:</span> +</span><span id="L-902"><a href="#L-902"><span class="linenos">902</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-903"><a href="#L-903"><span class="linenos">903</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"second"</span><span class="p">:</span> +</span><span id="L-904"><a href="#L-904"><span class="linenos">904</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="L-905"><a href="#L-905"><span class="linenos">905</span></a> +</span><span id="L-906"><a href="#L-906"><span class="linenos">906</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> </span><span id="L-907"><a href="#L-907"><span class="linenos">907</span></a> -</span><span id="L-908"><a href="#L-908"><span class="linenos">908</span></a> <span class="k">while</span> <span class="n">queue</span><span class="p">:</span> -</span><span id="L-909"><a href="#L-909"><span class="linenos">909</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> -</span><span id="L-910"><a href="#L-910"><span class="linenos">910</span></a> -</span><span id="L-911"><a href="#L-911"><span class="linenos">911</span></a> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">queue</span><span class="p">:</span> -</span><span id="L-912"><a href="#L-912"><span class="linenos">912</span></a> <span class="n">result</span> <span class="o">=</span> <span class="n">simplifier</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> -</span><span id="L-913"><a href="#L-913"><span class="linenos">913</span></a> -</span><span id="L-914"><a href="#L-914"><span class="linenos">914</span></a> <span class="k">if</span> <span class="n">result</span> <span class="ow">and</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">expression</span><span class="p">:</span> -</span><span id="L-915"><a href="#L-915"><span class="linenos">915</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> -</span><span id="L-916"><a href="#L-916"><span class="linenos">916</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> -</span><span id="L-917"><a href="#L-917"><span class="linenos">917</span></a> <span class="k">break</span> -</span><span id="L-918"><a href="#L-918"><span class="linenos">918</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-919"><a href="#L-919"><span class="linenos">919</span></a> <span class="n">operands</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> -</span><span id="L-920"><a href="#L-920"><span class="linenos">920</span></a> -</span><span id="L-921"><a href="#L-921"><span class="linenos">921</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">operands</span><span class="p">)</span> <span class="o"><</span> <span class="n">size</span><span class="p">:</span> -</span><span id="L-922"><a href="#L-922"><span class="linenos">922</span></a> <span class="k">return</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span> -</span><span id="L-923"><a href="#L-923"><span class="linenos">923</span></a> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">b</span><span class="p">),</span> <span class="n">operands</span> -</span><span id="L-924"><a href="#L-924"><span class="linenos">924</span></a> <span class="p">)</span> -</span><span id="L-925"><a href="#L-925"><span class="linenos">925</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-908"><a href="#L-908"><span class="linenos">908</span></a> +</span><span id="L-909"><a href="#L-909"><span class="linenos">909</span></a><span class="k">def</span> <span class="nf">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> +</span><span id="L-910"><a href="#L-910"><span class="linenos">910</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="L-911"><a href="#L-911"><span class="linenos">911</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-912"><a href="#L-912"><span class="linenos">912</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> +</span><span id="L-913"><a href="#L-913"><span class="linenos">913</span></a> <span class="k">if</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">3</span><span class="p">:</span> +</span><span id="L-914"><a href="#L-914"><span class="linenos">914</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-915"><a href="#L-915"><span class="linenos">915</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">6</span><span class="p">:</span> +</span><span id="L-916"><a href="#L-916"><span class="linenos">916</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-917"><a href="#L-917"><span class="linenos">917</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">9</span><span class="p">:</span> +</span><span id="L-918"><a href="#L-918"><span class="linenos">918</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-919"><a href="#L-919"><span class="linenos">919</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-920"><a href="#L-920"><span class="linenos">920</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-921"><a href="#L-921"><span class="linenos">921</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="L-922"><a href="#L-922"><span class="linenos">922</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="L-923"><a href="#L-923"><span class="linenos">923</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="L-924"><a href="#L-924"><span class="linenos">924</span></a> <span class="c1"># Assuming week starts on Monday (0) and ends on Sunday (6)</span> +</span><span id="L-925"><a href="#L-925"><span class="linenos">925</span></a> <span class="k">return</span> <span class="n">d</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">())</span> +</span><span id="L-926"><a href="#L-926"><span class="linenos">926</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="L-927"><a href="#L-927"><span class="linenos">927</span></a> <span class="k">return</span> <span class="n">d</span> +</span><span id="L-928"><a href="#L-928"><span class="linenos">928</span></a> +</span><span id="L-929"><a href="#L-929"><span class="linenos">929</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-930"><a href="#L-930"><span class="linenos">930</span></a> +</span><span id="L-931"><a href="#L-931"><span class="linenos">931</span></a> +</span><span id="L-932"><a href="#L-932"><span class="linenos">932</span></a><span class="k">def</span> <span class="nf">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> +</span><span id="L-933"><a href="#L-933"><span class="linenos">933</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="L-934"><a href="#L-934"><span class="linenos">934</span></a> +</span><span id="L-935"><a href="#L-935"><span class="linenos">935</span></a> <span class="k">if</span> <span class="n">floor</span> <span class="o">==</span> <span class="n">d</span><span class="p">:</span> +</span><span id="L-936"><a href="#L-936"><span class="linenos">936</span></a> <span class="k">return</span> <span class="n">d</span> +</span><span id="L-937"><a href="#L-937"><span class="linenos">937</span></a> +</span><span id="L-938"><a href="#L-938"><span class="linenos">938</span></a> <span class="k">return</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> +</span><span id="L-939"><a href="#L-939"><span class="linenos">939</span></a> +</span><span id="L-940"><a href="#L-940"><span class="linenos">940</span></a> +</span><span id="L-941"><a href="#L-941"><span class="linenos">941</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="L-942"><a href="#L-942"><span class="linenos">942</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-943"><a href="#L-943"><span class="linenos">943</span></a> +</span><span id="L-944"><a href="#L-944"><span class="linenos">944</span></a> +</span><span id="L-945"><a href="#L-945"><span class="linenos">945</span></a><span class="k">def</span> <span class="nf">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">simplifier</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-946"><a href="#L-946"><span class="linenos">946</span></a> <span class="k">if</span> <span class="n">root</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">same_parent</span><span class="p">:</span> +</span><span id="L-947"><a href="#L-947"><span class="linenos">947</span></a> <span class="n">operands</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-948"><a href="#L-948"><span class="linenos">948</span></a> <span class="n">queue</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">unnest</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span> +</span><span id="L-949"><a href="#L-949"><span class="linenos">949</span></a> <span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">queue</span><span class="p">)</span> +</span><span id="L-950"><a href="#L-950"><span class="linenos">950</span></a> +</span><span id="L-951"><a href="#L-951"><span class="linenos">951</span></a> <span class="k">while</span> <span class="n">queue</span><span class="p">:</span> +</span><span id="L-952"><a href="#L-952"><span class="linenos">952</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> +</span><span id="L-953"><a href="#L-953"><span class="linenos">953</span></a> +</span><span id="L-954"><a href="#L-954"><span class="linenos">954</span></a> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">queue</span><span class="p">:</span> +</span><span id="L-955"><a href="#L-955"><span class="linenos">955</span></a> <span class="n">result</span> <span class="o">=</span> <span class="n">simplifier</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-956"><a href="#L-956"><span class="linenos">956</span></a> +</span><span id="L-957"><a href="#L-957"><span class="linenos">957</span></a> <span class="k">if</span> <span class="n">result</span> <span class="ow">and</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">expression</span><span class="p">:</span> +</span><span id="L-958"><a href="#L-958"><span class="linenos">958</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-959"><a href="#L-959"><span class="linenos">959</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> +</span><span id="L-960"><a href="#L-960"><span class="linenos">960</span></a> <span class="k">break</span> +</span><span id="L-961"><a href="#L-961"><span class="linenos">961</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-962"><a href="#L-962"><span class="linenos">962</span></a> <span class="n">operands</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> +</span><span id="L-963"><a href="#L-963"><span class="linenos">963</span></a> +</span><span id="L-964"><a href="#L-964"><span class="linenos">964</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">operands</span><span class="p">)</span> <span class="o"><</span> <span class="n">size</span><span class="p">:</span> +</span><span id="L-965"><a href="#L-965"><span class="linenos">965</span></a> <span class="k">return</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span> +</span><span id="L-966"><a href="#L-966"><span class="linenos">966</span></a> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">b</span><span class="p">),</span> <span class="n">operands</span> +</span><span id="L-967"><a href="#L-967"><span class="linenos">967</span></a> <span class="p">)</span> +</span><span id="L-968"><a href="#L-968"><span class="linenos">968</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -1751,19 +1803,19 @@ a b </div> <a class="headerlink" href="#simplify_literals"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_literals-454"><a href="#simplify_literals-454"><span class="linenos">454</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> -</span><span id="simplify_literals-455"><a href="#simplify_literals-455"><span class="linenos">455</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> -</span><span id="simplify_literals-456"><a href="#simplify_literals-456"><span class="linenos">456</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">,</span> <span class="n">root</span><span class="p">)</span> -</span><span id="simplify_literals-457"><a href="#simplify_literals-457"><span class="linenos">457</span></a> -</span><span id="simplify_literals-458"><a href="#simplify_literals-458"><span class="linenos">458</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> -</span><span id="simplify_literals-459"><a href="#simplify_literals-459"><span class="linenos">459</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="simplify_literals-460"><a href="#simplify_literals-460"><span class="linenos">460</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> -</span><span id="simplify_literals-461"><a href="#simplify_literals-461"><span class="linenos">461</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> -</span><span id="simplify_literals-462"><a href="#simplify_literals-462"><span class="linenos">462</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> -</span><span id="simplify_literals-463"><a href="#simplify_literals-463"><span class="linenos">463</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> -</span><span id="simplify_literals-464"><a href="#simplify_literals-464"><span class="linenos">464</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> -</span><span id="simplify_literals-465"><a href="#simplify_literals-465"><span class="linenos">465</span></a> -</span><span id="simplify_literals-466"><a href="#simplify_literals-466"><span class="linenos">466</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_literals-447"><a href="#simplify_literals-447"><span class="linenos">447</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="simplify_literals-448"><a href="#simplify_literals-448"><span class="linenos">448</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="simplify_literals-449"><a href="#simplify_literals-449"><span class="linenos">449</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">,</span> <span class="n">root</span><span class="p">)</span> +</span><span id="simplify_literals-450"><a href="#simplify_literals-450"><span class="linenos">450</span></a> +</span><span id="simplify_literals-451"><a href="#simplify_literals-451"><span class="linenos">451</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> +</span><span id="simplify_literals-452"><a href="#simplify_literals-452"><span class="linenos">452</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_literals-453"><a href="#simplify_literals-453"><span class="linenos">453</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="simplify_literals-454"><a href="#simplify_literals-454"><span class="linenos">454</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="simplify_literals-455"><a href="#simplify_literals-455"><span class="linenos">455</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> +</span><span id="simplify_literals-456"><a href="#simplify_literals-456"><span class="linenos">456</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> +</span><span id="simplify_literals-457"><a href="#simplify_literals-457"><span class="linenos">457</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="simplify_literals-458"><a href="#simplify_literals-458"><span class="linenos">458</span></a> +</span><span id="simplify_literals-459"><a href="#simplify_literals-459"><span class="linenos">459</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -1781,24 +1833,24 @@ a b </div> <a class="headerlink" href="#simplify_parens"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_parens-529"><a href="#simplify_parens-529"><span class="linenos">529</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="simplify_parens-530"><a href="#simplify_parens-530"><span class="linenos">530</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> -</span><span id="simplify_parens-531"><a href="#simplify_parens-531"><span class="linenos">531</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="simplify_parens-532"><a href="#simplify_parens-532"><span class="linenos">532</span></a> -</span><span id="simplify_parens-533"><a href="#simplify_parens-533"><span class="linenos">533</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="simplify_parens-534"><a href="#simplify_parens-534"><span class="linenos">534</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> -</span><span id="simplify_parens-535"><a href="#simplify_parens-535"><span class="linenos">535</span></a> -</span><span id="simplify_parens-536"><a href="#simplify_parens-536"><span class="linenos">536</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span> -</span><span id="simplify_parens-537"><a href="#simplify_parens-537"><span class="linenos">537</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> -</span><span id="simplify_parens-538"><a href="#simplify_parens-538"><span class="linenos">538</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> -</span><span id="simplify_parens-539"><a href="#simplify_parens-539"><span class="linenos">539</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> -</span><span id="simplify_parens-540"><a href="#simplify_parens-540"><span class="linenos">540</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">))</span> -</span><span id="simplify_parens-541"><a href="#simplify_parens-541"><span class="linenos">541</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">))</span> -</span><span id="simplify_parens-542"><a href="#simplify_parens-542"><span class="linenos">542</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">))</span> -</span><span id="simplify_parens-543"><a href="#simplify_parens-543"><span class="linenos">543</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">)))</span> -</span><span id="simplify_parens-544"><a href="#simplify_parens-544"><span class="linenos">544</span></a> <span class="p">):</span> -</span><span id="simplify_parens-545"><a href="#simplify_parens-545"><span class="linenos">545</span></a> <span class="k">return</span> <span class="n">this</span> -</span><span id="simplify_parens-546"><a href="#simplify_parens-546"><span class="linenos">546</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_parens-522"><a href="#simplify_parens-522"><span class="linenos">522</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_parens-523"><a href="#simplify_parens-523"><span class="linenos">523</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="simplify_parens-524"><a href="#simplify_parens-524"><span class="linenos">524</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="simplify_parens-525"><a href="#simplify_parens-525"><span class="linenos">525</span></a> +</span><span id="simplify_parens-526"><a href="#simplify_parens-526"><span class="linenos">526</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_parens-527"><a href="#simplify_parens-527"><span class="linenos">527</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="simplify_parens-528"><a href="#simplify_parens-528"><span class="linenos">528</span></a> +</span><span id="simplify_parens-529"><a href="#simplify_parens-529"><span class="linenos">529</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span> +</span><span id="simplify_parens-530"><a href="#simplify_parens-530"><span class="linenos">530</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> +</span><span id="simplify_parens-531"><a href="#simplify_parens-531"><span class="linenos">531</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> +</span><span id="simplify_parens-532"><a href="#simplify_parens-532"><span class="linenos">532</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="simplify_parens-533"><a href="#simplify_parens-533"><span class="linenos">533</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">))</span> +</span><span id="simplify_parens-534"><a href="#simplify_parens-534"><span class="linenos">534</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">))</span> +</span><span id="simplify_parens-535"><a href="#simplify_parens-535"><span class="linenos">535</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">))</span> +</span><span id="simplify_parens-536"><a href="#simplify_parens-536"><span class="linenos">536</span></a> <span class="ow">or</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">)))</span> +</span><span id="simplify_parens-537"><a href="#simplify_parens-537"><span class="linenos">537</span></a> <span class="p">):</span> +</span><span id="simplify_parens-538"><a href="#simplify_parens-538"><span class="linenos">538</span></a> <span class="k">return</span> <span class="n">this</span> +</span><span id="simplify_parens-539"><a href="#simplify_parens-539"><span class="linenos">539</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -1829,62 +1881,62 @@ a b </div> <a class="headerlink" href="#simplify_coalesce"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_coalesce-556"><a href="#simplify_coalesce-556"><span class="linenos">556</span></a><span class="k">def</span> <span class="nf">simplify_coalesce</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="simplify_coalesce-557"><a href="#simplify_coalesce-557"><span class="linenos">557</span></a> <span class="c1"># COALESCE(x) -> x</span> -</span><span id="simplify_coalesce-558"><a href="#simplify_coalesce-558"><span class="linenos">558</span></a> <span class="k">if</span> <span class="p">(</span> -</span><span id="simplify_coalesce-559"><a href="#simplify_coalesce-559"><span class="linenos">559</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">)</span> -</span><span id="simplify_coalesce-560"><a href="#simplify_coalesce-560"><span class="linenos">560</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="simplify_coalesce-561"><a href="#simplify_coalesce-561"><span class="linenos">561</span></a> <span class="c1"># COALESCE is also used as a Spark partitioning hint</span> -</span><span id="simplify_coalesce-562"><a href="#simplify_coalesce-562"><span class="linenos">562</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> -</span><span id="simplify_coalesce-563"><a href="#simplify_coalesce-563"><span class="linenos">563</span></a> <span class="p">):</span> -</span><span id="simplify_coalesce-564"><a href="#simplify_coalesce-564"><span class="linenos">564</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="simplify_coalesce-565"><a href="#simplify_coalesce-565"><span class="linenos">565</span></a> -</span><span id="simplify_coalesce-566"><a href="#simplify_coalesce-566"><span class="linenos">566</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> -</span><span id="simplify_coalesce-567"><a href="#simplify_coalesce-567"><span class="linenos">567</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="simplify_coalesce-568"><a href="#simplify_coalesce-568"><span class="linenos">568</span></a> -</span><span id="simplify_coalesce-569"><a href="#simplify_coalesce-569"><span class="linenos">569</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> -</span><span id="simplify_coalesce-570"><a href="#simplify_coalesce-570"><span class="linenos">570</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> -</span><span id="simplify_coalesce-571"><a href="#simplify_coalesce-571"><span class="linenos">571</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> -</span><span id="simplify_coalesce-572"><a href="#simplify_coalesce-572"><span class="linenos">572</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> -</span><span id="simplify_coalesce-573"><a href="#simplify_coalesce-573"><span class="linenos">573</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> -</span><span id="simplify_coalesce-574"><a href="#simplify_coalesce-574"><span class="linenos">574</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> -</span><span id="simplify_coalesce-575"><a href="#simplify_coalesce-575"><span class="linenos">575</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="simplify_coalesce-576"><a href="#simplify_coalesce-576"><span class="linenos">576</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="simplify_coalesce-577"><a href="#simplify_coalesce-577"><span class="linenos">577</span></a> -</span><span id="simplify_coalesce-578"><a href="#simplify_coalesce-578"><span class="linenos">578</span></a> <span class="c1"># This transformation is valid for non-constants,</span> -</span><span id="simplify_coalesce-579"><a href="#simplify_coalesce-579"><span class="linenos">579</span></a> <span class="c1"># but it really only does anything if they are both constants.</span> -</span><span id="simplify_coalesce-580"><a href="#simplify_coalesce-580"><span class="linenos">580</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_coalesce-549"><a href="#simplify_coalesce-549"><span class="linenos">549</span></a><span class="k">def</span> <span class="nf">simplify_coalesce</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_coalesce-550"><a href="#simplify_coalesce-550"><span class="linenos">550</span></a> <span class="c1"># COALESCE(x) -> x</span> +</span><span id="simplify_coalesce-551"><a href="#simplify_coalesce-551"><span class="linenos">551</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="simplify_coalesce-552"><a href="#simplify_coalesce-552"><span class="linenos">552</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">)</span> +</span><span id="simplify_coalesce-553"><a href="#simplify_coalesce-553"><span class="linenos">553</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="simplify_coalesce-554"><a href="#simplify_coalesce-554"><span class="linenos">554</span></a> <span class="c1"># COALESCE is also used as a Spark partitioning hint</span> +</span><span id="simplify_coalesce-555"><a href="#simplify_coalesce-555"><span class="linenos">555</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> +</span><span id="simplify_coalesce-556"><a href="#simplify_coalesce-556"><span class="linenos">556</span></a> <span class="p">):</span> +</span><span id="simplify_coalesce-557"><a href="#simplify_coalesce-557"><span class="linenos">557</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_coalesce-558"><a href="#simplify_coalesce-558"><span class="linenos">558</span></a> +</span><span id="simplify_coalesce-559"><a href="#simplify_coalesce-559"><span class="linenos">559</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> +</span><span id="simplify_coalesce-560"><a href="#simplify_coalesce-560"><span class="linenos">560</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="simplify_coalesce-561"><a href="#simplify_coalesce-561"><span class="linenos">561</span></a> +</span><span id="simplify_coalesce-562"><a href="#simplify_coalesce-562"><span class="linenos">562</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> +</span><span id="simplify_coalesce-563"><a href="#simplify_coalesce-563"><span class="linenos">563</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> +</span><span id="simplify_coalesce-564"><a href="#simplify_coalesce-564"><span class="linenos">564</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="simplify_coalesce-565"><a href="#simplify_coalesce-565"><span class="linenos">565</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">):</span> +</span><span id="simplify_coalesce-566"><a href="#simplify_coalesce-566"><span class="linenos">566</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="simplify_coalesce-567"><a href="#simplify_coalesce-567"><span class="linenos">567</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> +</span><span id="simplify_coalesce-568"><a href="#simplify_coalesce-568"><span class="linenos">568</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="simplify_coalesce-569"><a href="#simplify_coalesce-569"><span class="linenos">569</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="simplify_coalesce-570"><a href="#simplify_coalesce-570"><span class="linenos">570</span></a> +</span><span id="simplify_coalesce-571"><a href="#simplify_coalesce-571"><span class="linenos">571</span></a> <span class="c1"># This transformation is valid for non-constants,</span> +</span><span id="simplify_coalesce-572"><a href="#simplify_coalesce-572"><span class="linenos">572</span></a> <span class="c1"># but it really only does anything if they are both constants.</span> +</span><span id="simplify_coalesce-573"><a href="#simplify_coalesce-573"><span class="linenos">573</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> +</span><span id="simplify_coalesce-574"><a href="#simplify_coalesce-574"><span class="linenos">574</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="simplify_coalesce-575"><a href="#simplify_coalesce-575"><span class="linenos">575</span></a> +</span><span id="simplify_coalesce-576"><a href="#simplify_coalesce-576"><span class="linenos">576</span></a> <span class="c1"># Find the first constant arg</span> +</span><span id="simplify_coalesce-577"><a href="#simplify_coalesce-577"><span class="linenos">577</span></a> <span class="k">for</span> <span class="n">arg_index</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">):</span> +</span><span id="simplify_coalesce-578"><a href="#simplify_coalesce-578"><span class="linenos">578</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> +</span><span id="simplify_coalesce-579"><a href="#simplify_coalesce-579"><span class="linenos">579</span></a> <span class="k">break</span> +</span><span id="simplify_coalesce-580"><a href="#simplify_coalesce-580"><span class="linenos">580</span></a> <span class="k">else</span><span class="p">:</span> </span><span id="simplify_coalesce-581"><a href="#simplify_coalesce-581"><span class="linenos">581</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="simplify_coalesce-582"><a href="#simplify_coalesce-582"><span class="linenos">582</span></a> -</span><span id="simplify_coalesce-583"><a href="#simplify_coalesce-583"><span class="linenos">583</span></a> <span class="c1"># Find the first constant arg</span> -</span><span id="simplify_coalesce-584"><a href="#simplify_coalesce-584"><span class="linenos">584</span></a> <span class="k">for</span> <span class="n">arg_index</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">):</span> -</span><span id="simplify_coalesce-585"><a href="#simplify_coalesce-585"><span class="linenos">585</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">CONSTANTS</span><span class="p">):</span> -</span><span id="simplify_coalesce-586"><a href="#simplify_coalesce-586"><span class="linenos">586</span></a> <span class="k">break</span> -</span><span id="simplify_coalesce-587"><a href="#simplify_coalesce-587"><span class="linenos">587</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="simplify_coalesce-588"><a href="#simplify_coalesce-588"><span class="linenos">588</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="simplify_coalesce-589"><a href="#simplify_coalesce-589"><span class="linenos">589</span></a> -</span><span id="simplify_coalesce-590"><a href="#simplify_coalesce-590"><span class="linenos">590</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">[:</span><span class="n">arg_index</span><span class="p">])</span> -</span><span id="simplify_coalesce-591"><a href="#simplify_coalesce-591"><span class="linenos">591</span></a> -</span><span id="simplify_coalesce-592"><a href="#simplify_coalesce-592"><span class="linenos">592</span></a> <span class="c1"># Remove the COALESCE function. This is an optimization, skipping a simplify iteration,</span> -</span><span id="simplify_coalesce-593"><a href="#simplify_coalesce-593"><span class="linenos">593</span></a> <span class="c1"># since we already remove COALESCE at the top of this function.</span> -</span><span id="simplify_coalesce-594"><a href="#simplify_coalesce-594"><span class="linenos">594</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">coalesce</span> <span class="k">if</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span> <span class="k">else</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">this</span> -</span><span id="simplify_coalesce-595"><a href="#simplify_coalesce-595"><span class="linenos">595</span></a> -</span><span id="simplify_coalesce-596"><a href="#simplify_coalesce-596"><span class="linenos">596</span></a> <span class="c1"># This expression is more complex than when we started, but it will get simplified further</span> -</span><span id="simplify_coalesce-597"><a href="#simplify_coalesce-597"><span class="linenos">597</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">paren</span><span class="p">(</span> -</span><span id="simplify_coalesce-598"><a href="#simplify_coalesce-598"><span class="linenos">598</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span> -</span><span id="simplify_coalesce-599"><a href="#simplify_coalesce-599"><span class="linenos">599</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="simplify_coalesce-600"><a href="#simplify_coalesce-600"><span class="linenos">600</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">())</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> -</span><span id="simplify_coalesce-601"><a href="#simplify_coalesce-601"><span class="linenos">601</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> -</span><span id="simplify_coalesce-602"><a href="#simplify_coalesce-602"><span class="linenos">602</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="simplify_coalesce-603"><a href="#simplify_coalesce-603"><span class="linenos">603</span></a> <span class="p">),</span> -</span><span id="simplify_coalesce-604"><a href="#simplify_coalesce-604"><span class="linenos">604</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> -</span><span id="simplify_coalesce-605"><a href="#simplify_coalesce-605"><span class="linenos">605</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()),</span> -</span><span id="simplify_coalesce-606"><a href="#simplify_coalesce-606"><span class="linenos">606</span></a> <span class="nb">type</span><span class="p">(</span><span class="n">expression</span><span class="p">)(</span><span class="n">this</span><span class="o">=</span><span class="n">arg</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> -</span><span id="simplify_coalesce-607"><a href="#simplify_coalesce-607"><span class="linenos">607</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="simplify_coalesce-608"><a href="#simplify_coalesce-608"><span class="linenos">608</span></a> <span class="p">),</span> -</span><span id="simplify_coalesce-609"><a href="#simplify_coalesce-609"><span class="linenos">609</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="simplify_coalesce-610"><a href="#simplify_coalesce-610"><span class="linenos">610</span></a> <span class="p">)</span> -</span><span id="simplify_coalesce-611"><a href="#simplify_coalesce-611"><span class="linenos">611</span></a> <span class="p">)</span> +</span><span id="simplify_coalesce-583"><a href="#simplify_coalesce-583"><span class="linenos">583</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span><span class="p">[:</span><span class="n">arg_index</span><span class="p">])</span> +</span><span id="simplify_coalesce-584"><a href="#simplify_coalesce-584"><span class="linenos">584</span></a> +</span><span id="simplify_coalesce-585"><a href="#simplify_coalesce-585"><span class="linenos">585</span></a> <span class="c1"># Remove the COALESCE function. This is an optimization, skipping a simplify iteration,</span> +</span><span id="simplify_coalesce-586"><a href="#simplify_coalesce-586"><span class="linenos">586</span></a> <span class="c1"># since we already remove COALESCE at the top of this function.</span> +</span><span id="simplify_coalesce-587"><a href="#simplify_coalesce-587"><span class="linenos">587</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="n">coalesce</span> <span class="k">if</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">expressions</span> <span class="k">else</span> <span class="n">coalesce</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_coalesce-588"><a href="#simplify_coalesce-588"><span class="linenos">588</span></a> +</span><span id="simplify_coalesce-589"><a href="#simplify_coalesce-589"><span class="linenos">589</span></a> <span class="c1"># This expression is more complex than when we started, but it will get simplified further</span> +</span><span id="simplify_coalesce-590"><a href="#simplify_coalesce-590"><span class="linenos">590</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">paren</span><span class="p">(</span> +</span><span id="simplify_coalesce-591"><a href="#simplify_coalesce-591"><span class="linenos">591</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span> +</span><span id="simplify_coalesce-592"><a href="#simplify_coalesce-592"><span class="linenos">592</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="simplify_coalesce-593"><a href="#simplify_coalesce-593"><span class="linenos">593</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">())</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="simplify_coalesce-594"><a href="#simplify_coalesce-594"><span class="linenos">594</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> +</span><span id="simplify_coalesce-595"><a href="#simplify_coalesce-595"><span class="linenos">595</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="simplify_coalesce-596"><a href="#simplify_coalesce-596"><span class="linenos">596</span></a> <span class="p">),</span> +</span><span id="simplify_coalesce-597"><a href="#simplify_coalesce-597"><span class="linenos">597</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="simplify_coalesce-598"><a href="#simplify_coalesce-598"><span class="linenos">598</span></a> <span class="n">coalesce</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()),</span> +</span><span id="simplify_coalesce-599"><a href="#simplify_coalesce-599"><span class="linenos">599</span></a> <span class="nb">type</span><span class="p">(</span><span class="n">expression</span><span class="p">)(</span><span class="n">this</span><span class="o">=</span><span class="n">arg</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> +</span><span id="simplify_coalesce-600"><a href="#simplify_coalesce-600"><span class="linenos">600</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="simplify_coalesce-601"><a href="#simplify_coalesce-601"><span class="linenos">601</span></a> <span class="p">),</span> +</span><span id="simplify_coalesce-602"><a href="#simplify_coalesce-602"><span class="linenos">602</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="simplify_coalesce-603"><a href="#simplify_coalesce-603"><span class="linenos">603</span></a> <span class="p">)</span> +</span><span id="simplify_coalesce-604"><a href="#simplify_coalesce-604"><span class="linenos">604</span></a> <span class="p">)</span> </span></pre></div> @@ -1926,23 +1978,23 @@ a b </div> <a class="headerlink" href="#simplify_concat"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_concat-618"><a href="#simplify_concat-618"><span class="linenos">618</span></a><span class="k">def</span> <span class="nf">simplify_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="simplify_concat-619"><a href="#simplify_concat-619"><span class="linenos">619</span></a><span class="w"> </span><span class="sd">"""Reduces all groups that contain string literals by concatenating them."""</span> -</span><span id="simplify_concat-620"><a href="#simplify_concat-620"><span class="linenos">620</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">CONCATS</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">):</span> -</span><span id="simplify_concat-621"><a href="#simplify_concat-621"><span class="linenos">621</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="simplify_concat-622"><a href="#simplify_concat-622"><span class="linenos">622</span></a> -</span><span id="simplify_concat-623"><a href="#simplify_concat-623"><span class="linenos">623</span></a> <span class="n">new_args</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="simplify_concat-624"><a href="#simplify_concat-624"><span class="linenos">624</span></a> <span class="k">for</span> <span class="n">is_string_group</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span> -</span><span id="simplify_concat-625"><a href="#simplify_concat-625"><span class="linenos">625</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> <span class="ow">or</span> <span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">is_string</span> -</span><span id="simplify_concat-626"><a href="#simplify_concat-626"><span class="linenos">626</span></a> <span class="p">):</span> -</span><span id="simplify_concat-627"><a href="#simplify_concat-627"><span class="linenos">627</span></a> <span class="k">if</span> <span class="n">is_string_group</span><span class="p">:</span> -</span><span id="simplify_concat-628"><a href="#simplify_concat-628"><span class="linenos">628</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">string</span> <span class="ow">in</span> <span class="n">group</span><span class="p">)))</span> -</span><span id="simplify_concat-629"><a href="#simplify_concat-629"><span class="linenos">629</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="simplify_concat-630"><a href="#simplify_concat-630"><span class="linenos">630</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> -</span><span id="simplify_concat-631"><a href="#simplify_concat-631"><span class="linenos">631</span></a> -</span><span id="simplify_concat-632"><a href="#simplify_concat-632"><span class="linenos">632</span></a> <span class="c1"># Ensures we preserve the right concat type, i.e. whether it's "safe" or not</span> -</span><span id="simplify_concat-633"><a href="#simplify_concat-633"><span class="linenos">633</span></a> <span class="n">concat_type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">SAFE_CONCATS</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span> -</span><span id="simplify_concat-634"><a href="#simplify_concat-634"><span class="linenos">634</span></a> <span class="k">return</span> <span class="n">new_args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">concat_type</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_args</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_concat-611"><a href="#simplify_concat-611"><span class="linenos">611</span></a><span class="k">def</span> <span class="nf">simplify_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_concat-612"><a href="#simplify_concat-612"><span class="linenos">612</span></a><span class="w"> </span><span class="sd">"""Reduces all groups that contain string literals by concatenating them."""</span> +</span><span id="simplify_concat-613"><a href="#simplify_concat-613"><span class="linenos">613</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">CONCATS</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">):</span> +</span><span id="simplify_concat-614"><a href="#simplify_concat-614"><span class="linenos">614</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="simplify_concat-615"><a href="#simplify_concat-615"><span class="linenos">615</span></a> +</span><span id="simplify_concat-616"><a href="#simplify_concat-616"><span class="linenos">616</span></a> <span class="n">new_args</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="simplify_concat-617"><a href="#simplify_concat-617"><span class="linenos">617</span></a> <span class="k">for</span> <span class="n">is_string_group</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span> +</span><span id="simplify_concat-618"><a href="#simplify_concat-618"><span class="linenos">618</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> <span class="ow">or</span> <span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">is_string</span> +</span><span id="simplify_concat-619"><a href="#simplify_concat-619"><span class="linenos">619</span></a> <span class="p">):</span> +</span><span id="simplify_concat-620"><a href="#simplify_concat-620"><span class="linenos">620</span></a> <span class="k">if</span> <span class="n">is_string_group</span><span class="p">:</span> +</span><span id="simplify_concat-621"><a href="#simplify_concat-621"><span class="linenos">621</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">string</span> <span class="ow">in</span> <span class="n">group</span><span class="p">)))</span> +</span><span id="simplify_concat-622"><a href="#simplify_concat-622"><span class="linenos">622</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="simplify_concat-623"><a href="#simplify_concat-623"><span class="linenos">623</span></a> <span class="n">new_args</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> +</span><span id="simplify_concat-624"><a href="#simplify_concat-624"><span class="linenos">624</span></a> +</span><span id="simplify_concat-625"><a href="#simplify_concat-625"><span class="linenos">625</span></a> <span class="c1"># Ensures we preserve the right concat type, i.e. whether it's "safe" or not</span> +</span><span id="simplify_concat-626"><a href="#simplify_concat-626"><span class="linenos">626</span></a> <span class="n">concat_type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">SafeConcat</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">SAFE_CONCATS</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span> +</span><span id="simplify_concat-627"><a href="#simplify_concat-627"><span class="linenos">627</span></a> <span class="k">return</span> <span class="n">new_args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">concat_type</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_args</span><span class="p">)</span> </span></pre></div> @@ -1993,7 +2045,7 @@ a b <div class="attr variable"> <span class="name">DATETRUNC_COMPARISONS</span> = <input id="DATETRUNC_COMPARISONS-view-value" class="view-value-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> - <label class="view-value-button pdoc-button" for="DATETRUNC_COMPARISONS-view-value"></label><span class="default_value">{<class '<a href="../expressions.html#NEQ">sqlglot.expressions.NEQ</a>'>, <class '<a href="../expressions.html#EQ">sqlglot.expressions.EQ</a>'>, <class '<a href="../expressions.html#LTE">sqlglot.expressions.LTE</a>'>, <class '<a href="../expressions.html#GTE">sqlglot.expressions.GTE</a>'>, <class '<a href="../expressions.html#In">sqlglot.expressions.In</a>'>, <class '<a href="../expressions.html#GT">sqlglot.expressions.GT</a>'>, <class '<a href="../expressions.html#LT">sqlglot.expressions.LT</a>'>}</span> + <label class="view-value-button pdoc-button" for="DATETRUNC_COMPARISONS-view-value"></label><span class="default_value">{<class '<a href="../expressions.html#EQ">sqlglot.expressions.EQ</a>'>, <class '<a href="../expressions.html#GTE">sqlglot.expressions.GTE</a>'>, <class '<a href="../expressions.html#NEQ">sqlglot.expressions.NEQ</a>'>, <class '<a href="../expressions.html#In">sqlglot.expressions.In</a>'>, <class '<a href="../expressions.html#LTE">sqlglot.expressions.LTE</a>'>, <class '<a href="../expressions.html#GT">sqlglot.expressions.GT</a>'>, <class '<a href="../expressions.html#LT">sqlglot.expressions.LT</a>'>}</span> </div> @@ -2029,7 +2081,7 @@ a b <section id="JOINS"> <div class="attr variable"> <span class="name">JOINS</span> = -<span class="default_value">{('RIGHT', ''), ('', 'INNER'), ('RIGHT', 'OUTER'), ('', '')}</span> +<span class="default_value">{('', ''), ('RIGHT', ''), ('', 'INNER'), ('RIGHT', 'OUTER')}</span> </div> @@ -2178,27 +2230,109 @@ a b </section> + <section id="cast_as_date"> + <input id="cast_as_date-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">cast_as_date</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">value</span><span class="p">:</span> <span class="n">Any</span></span><span class="return-annotation">) -> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]</span>:</span></span> + + <label class="view-source-button" for="cast_as_date-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#cast_as_date"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="cast_as_date-815"><a href="#cast_as_date-815"><span class="linenos">815</span></a><span class="k">def</span> <span class="nf">cast_as_date</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]:</span> +</span><span id="cast_as_date-816"><a href="#cast_as_date-816"><span class="linenos">816</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span> +</span><span id="cast_as_date-817"><a href="#cast_as_date-817"><span class="linenos">817</span></a> <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> +</span><span id="cast_as_date-818"><a href="#cast_as_date-818"><span class="linenos">818</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">):</span> +</span><span id="cast_as_date-819"><a href="#cast_as_date-819"><span class="linenos">819</span></a> <span class="k">return</span> <span class="n">value</span> +</span><span id="cast_as_date-820"><a href="#cast_as_date-820"><span class="linenos">820</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="cast_as_date-821"><a href="#cast_as_date-821"><span class="linenos">821</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> +</span><span id="cast_as_date-822"><a href="#cast_as_date-822"><span class="linenos">822</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> +</span><span id="cast_as_date-823"><a href="#cast_as_date-823"><span class="linenos">823</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> + <section id="cast_as_datetime"> + <input id="cast_as_datetime-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">cast_as_datetime</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">value</span><span class="p">:</span> <span class="n">Any</span></span><span class="return-annotation">) -> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">]</span>:</span></span> + + <label class="view-source-button" for="cast_as_datetime-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#cast_as_datetime"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="cast_as_datetime-826"><a href="#cast_as_datetime-826"><span class="linenos">826</span></a><span class="k">def</span> <span class="nf">cast_as_datetime</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">]:</span> +</span><span id="cast_as_datetime-827"><a href="#cast_as_datetime-827"><span class="linenos">827</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">):</span> +</span><span id="cast_as_datetime-828"><a href="#cast_as_datetime-828"><span class="linenos">828</span></a> <span class="k">return</span> <span class="n">value</span> +</span><span id="cast_as_datetime-829"><a href="#cast_as_datetime-829"><span class="linenos">829</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">):</span> +</span><span id="cast_as_datetime-830"><a href="#cast_as_datetime-830"><span class="linenos">830</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">day</span><span class="p">)</span> +</span><span id="cast_as_datetime-831"><a href="#cast_as_datetime-831"><span class="linenos">831</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="cast_as_datetime-832"><a href="#cast_as_datetime-832"><span class="linenos">832</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="cast_as_datetime-833"><a href="#cast_as_datetime-833"><span class="linenos">833</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> +</span><span id="cast_as_datetime-834"><a href="#cast_as_datetime-834"><span class="linenos">834</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> + <section id="cast_value"> + <input id="cast_value-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">cast_value</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">value</span><span class="p">:</span> <span class="n">Any</span>, </span><span class="param"><span class="n">to</span><span class="p">:</span> <span class="n"><a href="../expressions.html#DataType">sqlglot.expressions.DataType</a></span></span><span class="return-annotation">) -> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]</span>:</span></span> + + <label class="view-source-button" for="cast_value-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#cast_value"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="cast_value-837"><a href="#cast_value-837"><span class="linenos">837</span></a><span class="k">def</span> <span class="nf">cast_value</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]]:</span> +</span><span id="cast_value-838"><a href="#cast_value-838"><span class="linenos">838</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span> +</span><span id="cast_value-839"><a href="#cast_value-839"><span class="linenos">839</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="cast_value-840"><a href="#cast_value-840"><span class="linenos">840</span></a> <span class="k">if</span> <span class="n">to</span><span class="o">.</span><span class="n">is_type</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">):</span> +</span><span id="cast_value-841"><a href="#cast_value-841"><span class="linenos">841</span></a> <span class="k">return</span> <span class="n">cast_as_date</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="cast_value-842"><a href="#cast_value-842"><span class="linenos">842</span></a> <span class="k">if</span> <span class="n">to</span><span class="o">.</span><span class="n">is_type</span><span class="p">(</span><span class="o">*</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEMPORAL_TYPES</span><span class="p">):</span> +</span><span id="cast_value-843"><a href="#cast_value-843"><span class="linenos">843</span></a> <span class="k">return</span> <span class="n">cast_as_datetime</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> +</span><span id="cast_value-844"><a href="#cast_value-844"><span class="linenos">844</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> <section id="extract_date"> <input id="extract_date-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> <div class="attr function"> <span class="def">def</span> - <span class="name">extract_date</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">cast</span></span><span class="return-annotation">):</span></span> + <span class="name">extract_date</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">cast</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]</span>:</span></span> <label class="view-source-button" for="extract_date-view-source"><span>View Source</span></label> </div> <a class="headerlink" href="#extract_date"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="extract_date-815"><a href="#extract_date-815"><span class="linenos">815</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">):</span> -</span><span id="extract_date-816"><a href="#extract_date-816"><span class="linenos">816</span></a> <span class="c1"># The "fromisoformat" conversion could fail if the cast is used on an identifier,</span> -</span><span id="extract_date-817"><a href="#extract_date-817"><span class="linenos">817</span></a> <span class="c1"># so in that case we can't extract the date.</span> -</span><span id="extract_date-818"><a href="#extract_date-818"><span class="linenos">818</span></a> <span class="k">try</span><span class="p">:</span> -</span><span id="extract_date-819"><a href="#extract_date-819"><span class="linenos">819</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">:</span> -</span><span id="extract_date-820"><a href="#extract_date-820"><span class="linenos">820</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="extract_date-821"><a href="#extract_date-821"><span class="linenos">821</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> -</span><span id="extract_date-822"><a href="#extract_date-822"><span class="linenos">822</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="extract_date-823"><a href="#extract_date-823"><span class="linenos">823</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> -</span><span id="extract_date-824"><a href="#extract_date-824"><span class="linenos">824</span></a> <span class="k">return</span> <span class="kc">None</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="extract_date-847"><a href="#extract_date-847"><span class="linenos">847</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">]]:</span> +</span><span id="extract_date-848"><a href="#extract_date-848"><span class="linenos">848</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">):</span> +</span><span id="extract_date-849"><a href="#extract_date-849"><span class="linenos">849</span></a> <span class="n">to</span> <span class="o">=</span> <span class="n">cast</span><span class="o">.</span><span class="n">to</span> +</span><span id="extract_date-850"><a href="#extract_date-850"><span class="linenos">850</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">):</span> +</span><span id="extract_date-851"><a href="#extract_date-851"><span class="linenos">851</span></a> <span class="n">to</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">)</span> +</span><span id="extract_date-852"><a href="#extract_date-852"><span class="linenos">852</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="extract_date-853"><a href="#extract_date-853"><span class="linenos">853</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="extract_date-854"><a href="#extract_date-854"><span class="linenos">854</span></a> +</span><span id="extract_date-855"><a href="#extract_date-855"><span class="linenos">855</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">):</span> +</span><span id="extract_date-856"><a href="#extract_date-856"><span class="linenos">856</span></a> <span class="n">value</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Any</span> <span class="o">=</span> <span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="extract_date-857"><a href="#extract_date-857"><span class="linenos">857</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">)):</span> +</span><span id="extract_date-858"><a href="#extract_date-858"><span class="linenos">858</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="extract_date-859"><a href="#extract_date-859"><span class="linenos">859</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="extract_date-860"><a href="#extract_date-860"><span class="linenos">860</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="extract_date-861"><a href="#extract_date-861"><span class="linenos">861</span></a> <span class="k">return</span> <span class="n">cast_value</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">to</span><span class="p">)</span> </span></pre></div> @@ -2216,14 +2350,14 @@ a b </div> <a class="headerlink" href="#extract_interval"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="extract_interval-827"><a href="#extract_interval-827"><span class="linenos">827</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> -</span><span id="extract_interval-828"><a href="#extract_interval-828"><span class="linenos">828</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="extract_interval-829"><a href="#extract_interval-829"><span class="linenos">829</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> -</span><span id="extract_interval-830"><a href="#extract_interval-830"><span class="linenos">830</span></a> -</span><span id="extract_interval-831"><a href="#extract_interval-831"><span class="linenos">831</span></a> <span class="k">try</span><span class="p">:</span> -</span><span id="extract_interval-832"><a href="#extract_interval-832"><span class="linenos">832</span></a> <span class="k">return</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> -</span><span id="extract_interval-833"><a href="#extract_interval-833"><span class="linenos">833</span></a> <span class="k">except</span> <span class="p">(</span><span class="n">UnsupportedUnit</span><span class="p">,</span> <span class="ne">ModuleNotFoundError</span><span class="p">):</span> -</span><span id="extract_interval-834"><a href="#extract_interval-834"><span class="linenos">834</span></a> <span class="k">return</span> <span class="kc">None</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="extract_interval-868"><a href="#extract_interval-868"><span class="linenos">868</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="extract_interval-869"><a href="#extract_interval-869"><span class="linenos">869</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="extract_interval-870"><a href="#extract_interval-870"><span class="linenos">870</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> +</span><span id="extract_interval-871"><a href="#extract_interval-871"><span class="linenos">871</span></a> +</span><span id="extract_interval-872"><a href="#extract_interval-872"><span class="linenos">872</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="extract_interval-873"><a href="#extract_interval-873"><span class="linenos">873</span></a> <span class="k">return</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> +</span><span id="extract_interval-874"><a href="#extract_interval-874"><span class="linenos">874</span></a> <span class="k">except</span> <span class="p">(</span><span class="n">UnsupportedUnit</span><span class="p">,</span> <span class="ne">ModuleNotFoundError</span><span class="p">):</span> +</span><span id="extract_interval-875"><a href="#extract_interval-875"><span class="linenos">875</span></a> <span class="k">return</span> <span class="kc">None</span> </span></pre></div> @@ -2241,11 +2375,13 @@ a b </div> <a class="headerlink" href="#date_literal"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="date_literal-837"><a href="#date_literal-837"><span class="linenos">837</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> -</span><span id="date_literal-838"><a href="#date_literal-838"><span class="linenos">838</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> -</span><span id="date_literal-839"><a href="#date_literal-839"><span class="linenos">839</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> -</span><span id="date_literal-840"><a href="#date_literal-840"><span class="linenos">840</span></a> <span class="s2">"DATETIME"</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> <span class="k">else</span> <span class="s2">"DATE"</span><span class="p">,</span> -</span><span id="date_literal-841"><a href="#date_literal-841"><span class="linenos">841</span></a> <span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="date_literal-878"><a href="#date_literal-878"><span class="linenos">878</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> +</span><span id="date_literal-879"><a href="#date_literal-879"><span class="linenos">879</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> +</span><span id="date_literal-880"><a href="#date_literal-880"><span class="linenos">880</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> +</span><span id="date_literal-881"><a href="#date_literal-881"><span class="linenos">881</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span> +</span><span id="date_literal-882"><a href="#date_literal-882"><span class="linenos">882</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> +</span><span id="date_literal-883"><a href="#date_literal-883"><span class="linenos">883</span></a> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">,</span> +</span><span id="date_literal-884"><a href="#date_literal-884"><span class="linenos">884</span></a> <span class="p">)</span> </span></pre></div> @@ -2263,27 +2399,27 @@ a b </div> <a class="headerlink" href="#interval"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="interval-844"><a href="#interval-844"><span class="linenos">844</span></a><span class="k">def</span> <span class="nf">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> -</span><span id="interval-845"><a href="#interval-845"><span class="linenos">845</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> -</span><span id="interval-846"><a href="#interval-846"><span class="linenos">846</span></a> -</span><span id="interval-847"><a href="#interval-847"><span class="linenos">847</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> -</span><span id="interval-848"><a href="#interval-848"><span class="linenos">848</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-849"><a href="#interval-849"><span class="linenos">849</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> -</span><span id="interval-850"><a href="#interval-850"><span class="linenos">850</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">3</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-851"><a href="#interval-851"><span class="linenos">851</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> -</span><span id="interval-852"><a href="#interval-852"><span class="linenos">852</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-853"><a href="#interval-853"><span class="linenos">853</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> -</span><span id="interval-854"><a href="#interval-854"><span class="linenos">854</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-855"><a href="#interval-855"><span class="linenos">855</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> -</span><span id="interval-856"><a href="#interval-856"><span class="linenos">856</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-857"><a href="#interval-857"><span class="linenos">857</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"hour"</span><span class="p">:</span> -</span><span id="interval-858"><a href="#interval-858"><span class="linenos">858</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-859"><a href="#interval-859"><span class="linenos">859</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"minute"</span><span class="p">:</span> -</span><span id="interval-860"><a href="#interval-860"><span class="linenos">860</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-861"><a href="#interval-861"><span class="linenos">861</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"second"</span><span class="p">:</span> -</span><span id="interval-862"><a href="#interval-862"><span class="linenos">862</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> -</span><span id="interval-863"><a href="#interval-863"><span class="linenos">863</span></a> -</span><span id="interval-864"><a href="#interval-864"><span class="linenos">864</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="interval-887"><a href="#interval-887"><span class="linenos">887</span></a><span class="k">def</span> <span class="nf">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> +</span><span id="interval-888"><a href="#interval-888"><span class="linenos">888</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> +</span><span id="interval-889"><a href="#interval-889"><span class="linenos">889</span></a> +</span><span id="interval-890"><a href="#interval-890"><span class="linenos">890</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="interval-891"><a href="#interval-891"><span class="linenos">891</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-892"><a href="#interval-892"><span class="linenos">892</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> +</span><span id="interval-893"><a href="#interval-893"><span class="linenos">893</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">3</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-894"><a href="#interval-894"><span class="linenos">894</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="interval-895"><a href="#interval-895"><span class="linenos">895</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-896"><a href="#interval-896"><span class="linenos">896</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="interval-897"><a href="#interval-897"><span class="linenos">897</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-898"><a href="#interval-898"><span class="linenos">898</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="interval-899"><a href="#interval-899"><span class="linenos">899</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-900"><a href="#interval-900"><span class="linenos">900</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"hour"</span><span class="p">:</span> +</span><span id="interval-901"><a href="#interval-901"><span class="linenos">901</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-902"><a href="#interval-902"><span class="linenos">902</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"minute"</span><span class="p">:</span> +</span><span id="interval-903"><a href="#interval-903"><span class="linenos">903</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-904"><a href="#interval-904"><span class="linenos">904</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"second"</span><span class="p">:</span> +</span><span id="interval-905"><a href="#interval-905"><span class="linenos">905</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> +</span><span id="interval-906"><a href="#interval-906"><span class="linenos">906</span></a> +</span><span id="interval-907"><a href="#interval-907"><span class="linenos">907</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> </span></pre></div> @@ -2301,27 +2437,27 @@ a b </div> <a class="headerlink" href="#date_floor"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="date_floor-867"><a href="#date_floor-867"><span class="linenos">867</span></a><span class="k">def</span> <span class="nf">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> -</span><span id="date_floor-868"><a href="#date_floor-868"><span class="linenos">868</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> -</span><span id="date_floor-869"><a href="#date_floor-869"><span class="linenos">869</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-870"><a href="#date_floor-870"><span class="linenos">870</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> -</span><span id="date_floor-871"><a href="#date_floor-871"><span class="linenos">871</span></a> <span class="k">if</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">3</span><span class="p">:</span> -</span><span id="date_floor-872"><a href="#date_floor-872"><span class="linenos">872</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-873"><a href="#date_floor-873"><span class="linenos">873</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">6</span><span class="p">:</span> -</span><span id="date_floor-874"><a href="#date_floor-874"><span class="linenos">874</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-875"><a href="#date_floor-875"><span class="linenos">875</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">9</span><span class="p">:</span> -</span><span id="date_floor-876"><a href="#date_floor-876"><span class="linenos">876</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-877"><a href="#date_floor-877"><span class="linenos">877</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="date_floor-878"><a href="#date_floor-878"><span class="linenos">878</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-879"><a href="#date_floor-879"><span class="linenos">879</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> -</span><span id="date_floor-880"><a href="#date_floor-880"><span class="linenos">880</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> -</span><span id="date_floor-881"><a href="#date_floor-881"><span class="linenos">881</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> -</span><span id="date_floor-882"><a href="#date_floor-882"><span class="linenos">882</span></a> <span class="c1"># Assuming week starts on Monday (0) and ends on Sunday (6)</span> -</span><span id="date_floor-883"><a href="#date_floor-883"><span class="linenos">883</span></a> <span class="k">return</span> <span class="n">d</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">())</span> -</span><span id="date_floor-884"><a href="#date_floor-884"><span class="linenos">884</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> -</span><span id="date_floor-885"><a href="#date_floor-885"><span class="linenos">885</span></a> <span class="k">return</span> <span class="n">d</span> -</span><span id="date_floor-886"><a href="#date_floor-886"><span class="linenos">886</span></a> -</span><span id="date_floor-887"><a href="#date_floor-887"><span class="linenos">887</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="date_floor-910"><a href="#date_floor-910"><span class="linenos">910</span></a><span class="k">def</span> <span class="nf">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> +</span><span id="date_floor-911"><a href="#date_floor-911"><span class="linenos">911</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="date_floor-912"><a href="#date_floor-912"><span class="linenos">912</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-913"><a href="#date_floor-913"><span class="linenos">913</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"quarter"</span><span class="p">:</span> +</span><span id="date_floor-914"><a href="#date_floor-914"><span class="linenos">914</span></a> <span class="k">if</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">3</span><span class="p">:</span> +</span><span id="date_floor-915"><a href="#date_floor-915"><span class="linenos">915</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-916"><a href="#date_floor-916"><span class="linenos">916</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">6</span><span class="p">:</span> +</span><span id="date_floor-917"><a href="#date_floor-917"><span class="linenos">917</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-918"><a href="#date_floor-918"><span class="linenos">918</span></a> <span class="k">elif</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span> <span class="o"><=</span> <span class="mi">9</span><span class="p">:</span> +</span><span id="date_floor-919"><a href="#date_floor-919"><span class="linenos">919</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-920"><a href="#date_floor-920"><span class="linenos">920</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="date_floor-921"><a href="#date_floor-921"><span class="linenos">921</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-922"><a href="#date_floor-922"><span class="linenos">922</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="date_floor-923"><a href="#date_floor-923"><span class="linenos">923</span></a> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> +</span><span id="date_floor-924"><a href="#date_floor-924"><span class="linenos">924</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="date_floor-925"><a href="#date_floor-925"><span class="linenos">925</span></a> <span class="c1"># Assuming week starts on Monday (0) and ends on Sunday (6)</span> +</span><span id="date_floor-926"><a href="#date_floor-926"><span class="linenos">926</span></a> <span class="k">return</span> <span class="n">d</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">())</span> +</span><span id="date_floor-927"><a href="#date_floor-927"><span class="linenos">927</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="date_floor-928"><a href="#date_floor-928"><span class="linenos">928</span></a> <span class="k">return</span> <span class="n">d</span> +</span><span id="date_floor-929"><a href="#date_floor-929"><span class="linenos">929</span></a> +</span><span id="date_floor-930"><a href="#date_floor-930"><span class="linenos">930</span></a> <span class="k">raise</span> <span class="n">UnsupportedUnit</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unsupported unit: </span><span class="si">{</span><span class="n">unit</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> </span></pre></div> @@ -2339,13 +2475,13 @@ a b </div> <a class="headerlink" href="#date_ceil"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="date_ceil-890"><a href="#date_ceil-890"><span class="linenos">890</span></a><span class="k">def</span> <span class="nf">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> -</span><span id="date_ceil-891"><a href="#date_ceil-891"><span class="linenos">891</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> -</span><span id="date_ceil-892"><a href="#date_ceil-892"><span class="linenos">892</span></a> -</span><span id="date_ceil-893"><a href="#date_ceil-893"><span class="linenos">893</span></a> <span class="k">if</span> <span class="n">floor</span> <span class="o">==</span> <span class="n">d</span><span class="p">:</span> -</span><span id="date_ceil-894"><a href="#date_ceil-894"><span class="linenos">894</span></a> <span class="k">return</span> <span class="n">d</span> -</span><span id="date_ceil-895"><a href="#date_ceil-895"><span class="linenos">895</span></a> -</span><span id="date_ceil-896"><a href="#date_ceil-896"><span class="linenos">896</span></a> <span class="k">return</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="date_ceil-933"><a href="#date_ceil-933"><span class="linenos">933</span></a><span class="k">def</span> <span class="nf">date_ceil</span><span class="p">(</span><span class="n">d</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">:</span> +</span><span id="date_ceil-934"><a href="#date_ceil-934"><span class="linenos">934</span></a> <span class="n">floor</span> <span class="o">=</span> <span class="n">date_floor</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span> +</span><span id="date_ceil-935"><a href="#date_ceil-935"><span class="linenos">935</span></a> +</span><span id="date_ceil-936"><a href="#date_ceil-936"><span class="linenos">936</span></a> <span class="k">if</span> <span class="n">floor</span> <span class="o">==</span> <span class="n">d</span><span class="p">:</span> +</span><span id="date_ceil-937"><a href="#date_ceil-937"><span class="linenos">937</span></a> <span class="k">return</span> <span class="n">d</span> +</span><span id="date_ceil-938"><a href="#date_ceil-938"><span class="linenos">938</span></a> +</span><span id="date_ceil-939"><a href="#date_ceil-939"><span class="linenos">939</span></a> <span class="k">return</span> <span class="n">floor</span> <span class="o">+</span> <span class="n">interval</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span> </span></pre></div> @@ -2363,8 +2499,8 @@ a b </div> <a class="headerlink" href="#boolean_literal"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="boolean_literal-899"><a href="#boolean_literal-899"><span class="linenos">899</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> -</span><span id="boolean_literal-900"><a href="#boolean_literal-900"><span class="linenos">900</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="boolean_literal-942"><a href="#boolean_literal-942"><span class="linenos">942</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="boolean_literal-943"><a href="#boolean_literal-943"><span class="linenos">943</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> </span></pre></div> |