diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-02-19 13:44:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-02-19 13:44:59 +0000 |
commit | ef2db38de92f2329c1c366318bddfc7e3dee8415 (patch) | |
tree | dee41de1eb0e05f2f6805b77df41a71b3aa66ec2 /docs/sqlglot/optimizer/optimizer.html | |
parent | Adding upstream version 11.0.1. (diff) | |
download | sqlglot-ef2db38de92f2329c1c366318bddfc7e3dee8415.tar.xz sqlglot-ef2db38de92f2329c1c366318bddfc7e3dee8415.zip |
Adding upstream version 11.1.3.upstream/11.1.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/sqlglot/optimizer/optimizer.html')
-rw-r--r-- | docs/sqlglot/optimizer/optimizer.html | 257 |
1 files changed, 141 insertions, 116 deletions
diff --git a/docs/sqlglot/optimizer/optimizer.html b/docs/sqlglot/optimizer/optimizer.html index ca82c2f..6334f9d 100644 --- a/docs/sqlglot/optimizer/optimizer.html +++ b/docs/sqlglot/optimizer/optimizer.html @@ -56,82 +56,97 @@ <label class="view-source-button" for="mod-optimizer-view-source"><span>View Source</span></label> - <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">sqlglot</span> -</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.annotate_types</span> <span class="kn">import</span> <span class="n">annotate_types</span> -</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.canonicalize</span> <span class="kn">import</span> <span class="n">canonicalize</span> -</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_ctes</span> <span class="kn">import</span> <span class="n">eliminate_ctes</span> -</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_joins</span> <span class="kn">import</span> <span class="n">eliminate_joins</span> -</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_subqueries</span> <span class="kn">import</span> <span class="n">eliminate_subqueries</span> -</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_laterals</span> <span class="kn">import</span> <span class="n">expand_laterals</span> -</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_multi_table_selects</span> <span class="kn">import</span> <span class="n">expand_multi_table_selects</span> -</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.isolate_table_selects</span> <span class="kn">import</span> <span class="n">isolate_table_selects</span> -</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.lower_identities</span> <span class="kn">import</span> <span class="n">lower_identities</span> -</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.merge_subqueries</span> <span class="kn">import</span> <span class="n">merge_subqueries</span> -</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.normalize</span> <span class="kn">import</span> <span class="n">normalize</span> -</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.optimize_joins</span> <span class="kn">import</span> <span class="n">optimize_joins</span> -</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_predicates</span> <span class="kn">import</span> <span class="n">pushdown_predicates</span> -</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_projections</span> <span class="kn">import</span> <span class="n">pushdown_projections</span> -</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_columns</span> <span class="kn">import</span> <span class="n">qualify_columns</span><span class="p">,</span> <span class="n">validate_qualify_columns</span> -</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_tables</span> <span class="kn">import</span> <span class="n">qualify_tables</span> -</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.unnest_subqueries</span> <span class="kn">import</span> <span class="n">unnest_subqueries</span> -</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> -</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> -</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="n">RULES</span> <span class="o">=</span> <span class="p">(</span> -</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">lower_identities</span><span class="p">,</span> -</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="n">qualify_tables</span><span class="p">,</span> -</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="n">isolate_table_selects</span><span class="p">,</span> -</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="n">qualify_columns</span><span class="p">,</span> -</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="n">expand_laterals</span><span class="p">,</span> -</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">validate_qualify_columns</span><span class="p">,</span> -</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="n">pushdown_projections</span><span class="p">,</span> -</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">normalize</span><span class="p">,</span> -</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="n">unnest_subqueries</span><span class="p">,</span> -</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">expand_multi_table_selects</span><span class="p">,</span> -</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="n">pushdown_predicates</span><span class="p">,</span> -</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="n">optimize_joins</span><span class="p">,</span> -</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">eliminate_subqueries</span><span class="p">,</span> -</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">merge_subqueries</span><span class="p">,</span> -</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="n">eliminate_joins</span><span class="p">,</span> -</span><span id="L-37"><a href="#L-37"><span class="linenos">37</span></a> <span class="n">eliminate_ctes</span><span class="p">,</span> -</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="n">annotate_types</span><span class="p">,</span> -</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="n">canonicalize</span><span class="p">,</span> -</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a><span class="p">)</span> -</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> -</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> -</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">rules</span><span class="o">=</span><span class="n">RULES</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> -</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> -</span><span id="L-46"><a href="#L-46"><span class="linenos">46</span></a> -</span><span id="L-47"><a href="#L-47"><span class="linenos">47</span></a><span class="sd"> Args:</span> -</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> -</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): database schema.</span> -</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> -</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a><span class="sd"> the following forms:</span> -</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a><span class="sd"> 1. {table: {col: type}}</span> -</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> -</span><span id="L-54"><a href="#L-54"><span class="linenos">54</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> -</span><span id="L-55"><a href="#L-55"><span class="linenos">55</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> -</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a><span class="sd"> db (str): specify the default database, as might be set by a `USE DATABASE db` statement</span> -</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a><span class="sd"> catalog (str): specify the default catalog, as might be set by a `USE CATALOG c` statement</span> -</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a><span class="sd"> rules (sequence): sequence of optimizer rules to use.</span> -</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> -</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> -</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a><span class="sd"> what you're doing!</span> -</span><span id="L-62"><a href="#L-62"><span class="linenos">62</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> -</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a><span class="sd"> Returns:</span> -</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> -</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a><span class="sd"> """</span> -</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> -</span><span id="L-67"><a href="#L-67"><span class="linenos">67</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> -</span><span id="L-68"><a href="#L-68"><span class="linenos">68</span></a> <span class="n">expression</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-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> -</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> -</span><span id="L-71"><a href="#L-71"><span class="linenos">71</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> -</span><span id="L-72"><a href="#L-72"><span class="linenos">72</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-73"><a href="#L-73"><span class="linenos">73</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> -</span><span id="L-74"><a href="#L-74"><span class="linenos">74</span></a> <span class="p">}</span> -</span><span id="L-75"><a href="#L-75"><span class="linenos">75</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> -</span><span id="L-76"><a href="#L-76"><span class="linenos">76</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">annotations</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">import</span> <span class="nn">sqlglot</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">Schema</span><span class="p">,</span> <span class="n">exp</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="kn">from</span> <span class="nn">sqlglot.dialects.dialect</span> <span class="kn">import</span> <span class="n">DialectType</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.annotate_types</span> <span class="kn">import</span> <span class="n">annotate_types</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.canonicalize</span> <span class="kn">import</span> <span class="n">canonicalize</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_ctes</span> <span class="kn">import</span> <span class="n">eliminate_ctes</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_joins</span> <span class="kn">import</span> <span class="n">eliminate_joins</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_subqueries</span> <span class="kn">import</span> <span class="n">eliminate_subqueries</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_laterals</span> <span class="kn">import</span> <span class="n">expand_laterals</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_multi_table_selects</span> <span class="kn">import</span> <span class="n">expand_multi_table_selects</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.isolate_table_selects</span> <span class="kn">import</span> <span class="n">isolate_table_selects</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.lower_identities</span> <span class="kn">import</span> <span class="n">lower_identities</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.merge_subqueries</span> <span class="kn">import</span> <span class="n">merge_subqueries</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.normalize</span> <span class="kn">import</span> <span class="n">normalize</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.optimize_joins</span> <span class="kn">import</span> <span class="n">optimize_joins</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_predicates</span> <span class="kn">import</span> <span class="n">pushdown_predicates</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_projections</span> <span class="kn">import</span> <span class="n">pushdown_projections</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_columns</span> <span class="kn">import</span> <span class="n">qualify_columns</span><span class="p">,</span> <span class="n">validate_qualify_columns</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_tables</span> <span class="kn">import</span> <span class="n">qualify_tables</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.unnest_subqueries</span> <span class="kn">import</span> <span class="n">unnest_subqueries</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a><span class="n">RULES</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="n">lower_identities</span><span class="p">,</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">qualify_tables</span><span class="p">,</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="n">isolate_table_selects</span><span class="p">,</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">qualify_columns</span><span class="p">,</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="n">expand_laterals</span><span class="p">,</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="n">validate_qualify_columns</span><span class="p">,</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">pushdown_projections</span><span class="p">,</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">normalize</span><span class="p">,</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="n">unnest_subqueries</span><span class="p">,</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos">37</span></a> <span class="n">expand_multi_table_selects</span><span class="p">,</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="n">pushdown_predicates</span><span class="p">,</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="n">optimize_joins</span><span class="p">,</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> <span class="n">eliminate_subqueries</span><span class="p">,</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> <span class="n">merge_subqueries</span><span class="p">,</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> <span class="n">eliminate_joins</span><span class="p">,</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a> <span class="n">eliminate_ctes</span><span class="p">,</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a> <span class="n">annotate_types</span><span class="p">,</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a> <span class="n">canonicalize</span><span class="p">,</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos">46</span></a><span class="p">)</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos">47</span></a> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a> +</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a> <span class="n">expression</span><span class="p">:</span> <span class="nb">str</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-51"><a href="#L-51"><span class="linenos">51</span></a> <span class="n">schema</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="nb">dict</span> <span class="o">|</span> <span class="n">Schema</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a> <span class="n">db</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="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a> <span class="n">catalog</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="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos">54</span></a> <span class="n">dialect</span><span class="p">:</span> <span class="n">DialectType</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos">55</span></a> <span class="n">rules</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Sequence</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="n">RULES</span><span class="p">,</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a><span class="p">):</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a> +</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a><span class="sd"> Args:</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos">62</span></a><span class="sd"> expression: expression to optimize</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a><span class="sd"> schema: database schema.</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a><span class="sd"> the following forms:</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a><span class="sd"> 1. {table: {col: type}}</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos">67</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos">68</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a><span class="sd"> db: specify the default database, as might be set by a `USE DATABASE db` statement</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos">71</span></a><span class="sd"> catalog: specify the default catalog, as might be set by a `USE CATALOG c` statement</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos">72</span></a><span class="sd"> dialect: The dialect to parse the sql string.</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos">73</span></a><span class="sd"> rules: sequence of optimizer rules to use.</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos">74</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos">75</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos">76</span></a><span class="sd"> what you're doing!</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos">77</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos">78</span></a><span class="sd"> Returns:</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos">79</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos">80</span></a><span class="sd"> """</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos">81</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos">82</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos">83</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">maybe_parse</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dialect</span><span class="o">=</span><span class="n">dialect</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos">84</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos">85</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos">86</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos">87</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos">88</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos">89</span></a> <span class="p">}</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos">90</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos">91</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -141,46 +156,55 @@ <div class="attr function"> <span class="def">def</span> - <span class="name">optimize</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span>,</span><span class="param"> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">db</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">rules</span><span class="o">=</span><span class="p">(</span><span class="o"><</span><span class="n">function</span> <span class="n">lower_identities</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2f80</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_tables</span> <span class="n">at</span> <span class="mh">0x7ff75a9d9240</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">isolate_table_selects</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2e60</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_columns</span> <span class="n">at</span> <span class="mh">0x7ff75a9d8820</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_laterals</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2b90</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">validate_qualify_columns</span> <span class="n">at</span> <span class="mh">0x7ff75a9d8c10</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_projections</span> <span class="n">at</span> <span class="mh">0x7ff75a9d85e0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">normalize</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0820</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">unnest_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9d9900</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_multi_table_selects</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2dd0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_predicates</span> <span class="n">at</span> <span class="mh">0x7ff75a9d81f0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">optimize_joins</span> <span class="n">at</span> <span class="mh">0x7ff75a9b3d00</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2830</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">merge_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9b35b0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_joins</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0700</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_ctes</span> <span class="n">at</span> <span class="mh">0x7ff75a9b05e0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">annotate_types</span> <span class="n">at</span> <span class="mh">0x7ff75a989480</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">canonicalize</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0160</span><span class="o">></span><span class="p">)</span>,</span><span class="param"> <span class="o">**</span><span class="n">kwargs</span></span><span class="return-annotation">):</span></span> + <span class="name">optimize</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span><span class="p">:</span> <span class="nb">str</span> <span class="o">|</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>,</span><span class="param"> <span class="n">schema</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">dict</span><span class="p">,</span> <span class="n"><a href="../schema.html#Schema">sqlglot.schema.Schema</a></span><span class="p">,</span> <span class="n">NoneType</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>,</span><span class="param"> <span class="n">db</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>,</span><span class="param"> <span class="n">catalog</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>,</span><span class="param"> <span class="n">dialect</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n"><a href="../dialects/dialect.html#Dialect">sqlglot.dialects.dialect.Dialect</a></span><span class="p">,</span> <span class="n">Type</span><span class="p">[</span><span class="n"><a href="../dialects/dialect.html#Dialect">sqlglot.dialects.dialect.Dialect</a></span><span class="p">],</span> <span class="n">NoneType</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>,</span><span class="param"> <span class="n">rules</span><span class="p">:</span> <span class="n">Sequence</span><span class="p">[</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="o"><</span><span class="n">function</span> <span class="n">lower_identities</span> <span class="n">at</span> <span class="mh">0x7f145108f370</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_tables</span> <span class="n">at</span> <span class="mh">0x7f14510fd510</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">isolate_table_selects</span> <span class="n">at</span> <span class="mh">0x7f145108f2e0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_columns</span> <span class="n">at</span> <span class="mh">0x7f14510fcb80</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_laterals</span> <span class="n">at</span> <span class="mh">0x7f145108f0a0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">validate_qualify_columns</span> <span class="n">at</span> <span class="mh">0x7f14510fcc10</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_projections</span> <span class="n">at</span> <span class="mh">0x7f14510fc940</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">normalize</span> <span class="n">at</span> <span class="mh">0x7f145108d090</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">unnest_subqueries</span> <span class="n">at</span> <span class="mh">0x7f14510fdab0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_multi_table_selects</span> <span class="n">at</span> <span class="mh">0x7f145108f250</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_predicates</span> <span class="n">at</span> <span class="mh">0x7f14510fc4c0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">optimize_joins</span> <span class="n">at</span> <span class="mh">0x7f14510fc1f0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_subqueries</span> <span class="n">at</span> <span class="mh">0x7f145108ecb0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">merge_subqueries</span> <span class="n">at</span> <span class="mh">0x7f145108f640</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_joins</span> <span class="n">at</span> <span class="mh">0x7f145108cee0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_ctes</span> <span class="n">at</span> <span class="mh">0x7f145108cdc0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">annotate_types</span> <span class="n">at</span> <span class="mh">0x7f1451065cf0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">canonicalize</span> <span class="n">at</span> <span class="mh">0x7f145108c9d0</span><span class="o">></span><span class="p">)</span>,</span><span class="param"> <span class="o">**</span><span class="n">kwargs</span></span><span class="return-annotation">):</span></span> <label class="view-source-button" for="optimize-view-source"><span>View Source</span></label> </div> <a class="headerlink" href="#optimize"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="optimize-44"><a href="#optimize-44"><span class="linenos">44</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">rules</span><span class="o">=</span><span class="n">RULES</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> -</span><span id="optimize-45"><a href="#optimize-45"><span class="linenos">45</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="optimize-46"><a href="#optimize-46"><span class="linenos">46</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> -</span><span id="optimize-47"><a href="#optimize-47"><span class="linenos">47</span></a> -</span><span id="optimize-48"><a href="#optimize-48"><span class="linenos">48</span></a><span class="sd"> Args:</span> -</span><span id="optimize-49"><a href="#optimize-49"><span class="linenos">49</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> -</span><span id="optimize-50"><a href="#optimize-50"><span class="linenos">50</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): database schema.</span> -</span><span id="optimize-51"><a href="#optimize-51"><span class="linenos">51</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> -</span><span id="optimize-52"><a href="#optimize-52"><span class="linenos">52</span></a><span class="sd"> the following forms:</span> -</span><span id="optimize-53"><a href="#optimize-53"><span class="linenos">53</span></a><span class="sd"> 1. {table: {col: type}}</span> -</span><span id="optimize-54"><a href="#optimize-54"><span class="linenos">54</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> -</span><span id="optimize-55"><a href="#optimize-55"><span class="linenos">55</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> -</span><span id="optimize-56"><a href="#optimize-56"><span class="linenos">56</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> -</span><span id="optimize-57"><a href="#optimize-57"><span class="linenos">57</span></a><span class="sd"> db (str): specify the default database, as might be set by a `USE DATABASE db` statement</span> -</span><span id="optimize-58"><a href="#optimize-58"><span class="linenos">58</span></a><span class="sd"> catalog (str): specify the default catalog, as might be set by a `USE CATALOG c` statement</span> -</span><span id="optimize-59"><a href="#optimize-59"><span class="linenos">59</span></a><span class="sd"> rules (sequence): sequence of optimizer rules to use.</span> -</span><span id="optimize-60"><a href="#optimize-60"><span class="linenos">60</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> -</span><span id="optimize-61"><a href="#optimize-61"><span class="linenos">61</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> -</span><span id="optimize-62"><a href="#optimize-62"><span class="linenos">62</span></a><span class="sd"> what you're doing!</span> -</span><span id="optimize-63"><a href="#optimize-63"><span class="linenos">63</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> -</span><span id="optimize-64"><a href="#optimize-64"><span class="linenos">64</span></a><span class="sd"> Returns:</span> -</span><span id="optimize-65"><a href="#optimize-65"><span class="linenos">65</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> -</span><span id="optimize-66"><a href="#optimize-66"><span class="linenos">66</span></a><span class="sd"> """</span> -</span><span id="optimize-67"><a href="#optimize-67"><span class="linenos">67</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> -</span><span id="optimize-68"><a href="#optimize-68"><span class="linenos">68</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> -</span><span id="optimize-69"><a href="#optimize-69"><span class="linenos">69</span></a> <span class="n">expression</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="optimize-70"><a href="#optimize-70"><span class="linenos">70</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> -</span><span id="optimize-71"><a href="#optimize-71"><span class="linenos">71</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> -</span><span id="optimize-72"><a href="#optimize-72"><span class="linenos">72</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> -</span><span id="optimize-73"><a href="#optimize-73"><span class="linenos">73</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="optimize-74"><a href="#optimize-74"><span class="linenos">74</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> -</span><span id="optimize-75"><a href="#optimize-75"><span class="linenos">75</span></a> <span class="p">}</span> -</span><span id="optimize-76"><a href="#optimize-76"><span class="linenos">76</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> -</span><span id="optimize-77"><a href="#optimize-77"><span class="linenos">77</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="optimize-50"><a href="#optimize-50"><span class="linenos">50</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span> +</span><span id="optimize-51"><a href="#optimize-51"><span class="linenos">51</span></a> <span class="n">expression</span><span class="p">:</span> <span class="nb">str</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="optimize-52"><a href="#optimize-52"><span class="linenos">52</span></a> <span class="n">schema</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="nb">dict</span> <span class="o">|</span> <span class="n">Schema</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="optimize-53"><a href="#optimize-53"><span class="linenos">53</span></a> <span class="n">db</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="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="optimize-54"><a href="#optimize-54"><span class="linenos">54</span></a> <span class="n">catalog</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="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="optimize-55"><a href="#optimize-55"><span class="linenos">55</span></a> <span class="n">dialect</span><span class="p">:</span> <span class="n">DialectType</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="optimize-56"><a href="#optimize-56"><span class="linenos">56</span></a> <span class="n">rules</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Sequence</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="n">RULES</span><span class="p">,</span> +</span><span id="optimize-57"><a href="#optimize-57"><span class="linenos">57</span></a> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> +</span><span id="optimize-58"><a href="#optimize-58"><span class="linenos">58</span></a><span class="p">):</span> +</span><span id="optimize-59"><a href="#optimize-59"><span class="linenos">59</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="optimize-60"><a href="#optimize-60"><span class="linenos">60</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> +</span><span id="optimize-61"><a href="#optimize-61"><span class="linenos">61</span></a> +</span><span id="optimize-62"><a href="#optimize-62"><span class="linenos">62</span></a><span class="sd"> Args:</span> +</span><span id="optimize-63"><a href="#optimize-63"><span class="linenos">63</span></a><span class="sd"> expression: expression to optimize</span> +</span><span id="optimize-64"><a href="#optimize-64"><span class="linenos">64</span></a><span class="sd"> schema: database schema.</span> +</span><span id="optimize-65"><a href="#optimize-65"><span class="linenos">65</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> +</span><span id="optimize-66"><a href="#optimize-66"><span class="linenos">66</span></a><span class="sd"> the following forms:</span> +</span><span id="optimize-67"><a href="#optimize-67"><span class="linenos">67</span></a><span class="sd"> 1. {table: {col: type}}</span> +</span><span id="optimize-68"><a href="#optimize-68"><span class="linenos">68</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> +</span><span id="optimize-69"><a href="#optimize-69"><span class="linenos">69</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> +</span><span id="optimize-70"><a href="#optimize-70"><span class="linenos">70</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> +</span><span id="optimize-71"><a href="#optimize-71"><span class="linenos">71</span></a><span class="sd"> db: specify the default database, as might be set by a `USE DATABASE db` statement</span> +</span><span id="optimize-72"><a href="#optimize-72"><span class="linenos">72</span></a><span class="sd"> catalog: specify the default catalog, as might be set by a `USE CATALOG c` statement</span> +</span><span id="optimize-73"><a href="#optimize-73"><span class="linenos">73</span></a><span class="sd"> dialect: The dialect to parse the sql string.</span> +</span><span id="optimize-74"><a href="#optimize-74"><span class="linenos">74</span></a><span class="sd"> rules: sequence of optimizer rules to use.</span> +</span><span id="optimize-75"><a href="#optimize-75"><span class="linenos">75</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> +</span><span id="optimize-76"><a href="#optimize-76"><span class="linenos">76</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> +</span><span id="optimize-77"><a href="#optimize-77"><span class="linenos">77</span></a><span class="sd"> what you're doing!</span> +</span><span id="optimize-78"><a href="#optimize-78"><span class="linenos">78</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> +</span><span id="optimize-79"><a href="#optimize-79"><span class="linenos">79</span></a><span class="sd"> Returns:</span> +</span><span id="optimize-80"><a href="#optimize-80"><span class="linenos">80</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="optimize-81"><a href="#optimize-81"><span class="linenos">81</span></a><span class="sd"> """</span> +</span><span id="optimize-82"><a href="#optimize-82"><span class="linenos">82</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> +</span><span id="optimize-83"><a href="#optimize-83"><span class="linenos">83</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> +</span><span id="optimize-84"><a href="#optimize-84"><span class="linenos">84</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">maybe_parse</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dialect</span><span class="o">=</span><span class="n">dialect</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="optimize-85"><a href="#optimize-85"><span class="linenos">85</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> +</span><span id="optimize-86"><a href="#optimize-86"><span class="linenos">86</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> +</span><span id="optimize-87"><a href="#optimize-87"><span class="linenos">87</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> +</span><span id="optimize-88"><a href="#optimize-88"><span class="linenos">88</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="optimize-89"><a href="#optimize-89"><span class="linenos">89</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> +</span><span id="optimize-90"><a href="#optimize-90"><span class="linenos">90</span></a> <span class="p">}</span> +</span><span id="optimize-91"><a href="#optimize-91"><span class="linenos">91</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> +</span><span id="optimize-92"><a href="#optimize-92"><span class="linenos">92</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -189,17 +213,18 @@ <h6 id="arguments">Arguments:</h6> <ul> -<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> -<li><strong>schema (dict|sqlglot.optimizer.Schema):</strong> database schema. +<li><strong>expression:</strong> expression to optimize</li> +<li><strong>schema:</strong> database schema. This can either be an instance of <code>sqlglot.optimizer.Schema</code> or a mapping in one of the following forms: 1. {table: {col: type}} 2. {db: {table: {col: type}}} 3. {catalog: {db: {table: {col: type}}}} If no schema is provided then the default schema defined at <code>sqlgot.schema</code> will be used</li> -<li><strong>db (str):</strong> specify the default database, as might be set by a <code>USE DATABASE db</code> statement</li> -<li><strong>catalog (str):</strong> specify the default catalog, as might be set by a <code>USE CATALOG c</code> statement</li> -<li><strong>rules (sequence):</strong> sequence of optimizer rules to use. +<li><strong>db:</strong> specify the default database, as might be set by a <code>USE DATABASE db</code> statement</li> +<li><strong>catalog:</strong> specify the default catalog, as might be set by a <code>USE CATALOG c</code> statement</li> +<li><strong>dialect:</strong> The dialect to parse the sql string.</li> +<li><strong>rules:</strong> sequence of optimizer rules to use. Many of the rules require tables and columns to be qualified. Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know what you're doing!</li> |