diff options
Diffstat (limited to 'docs/sqlglot/optimizer/normalize_identifiers.html')
-rw-r--r-- | docs/sqlglot/optimizer/normalize_identifiers.html | 114 |
1 files changed, 58 insertions, 56 deletions
diff --git a/docs/sqlglot/optimizer/normalize_identifiers.html b/docs/sqlglot/optimizer/normalize_identifiers.html index 2366979..834c33e 100644 --- a/docs/sqlglot/optimizer/normalize_identifiers.html +++ b/docs/sqlglot/optimizer/normalize_identifiers.html @@ -56,42 +56,33 @@ <label class="view-source-button" for="mod-normalize_identifiers-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">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</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._typing</span> <span class="kn">import</span> <span class="n">E</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.dialects.dialect</span> <span class="kn">import</span> <span class="n">RESOLVES_IDENTIFIERS_AS_UPPERCASE</span><span class="p">,</span> <span class="n">DialectType</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">sqlglot._typing</span> <span class="kn">import</span> <span class="n">E</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.dialects.dialect</span> <span class="kn">import</span> <span class="n">Dialect</span><span class="p">,</span> <span class="n">DialectType</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a> </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><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">normalize_identifiers</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">E</span><span class="p">,</span> <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 class="o">-></span> <span class="n">E</span><span class="p">:</span> -</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> Normalize all unquoted identifiers to either lower or upper case, depending on</span> -</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> the dialect. This essentially makes those identifiers case-insensitive.</span> -</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a> -</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> Example:</span> -</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> >>> import sqlglot</span> -</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> -</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> >>> normalize_identifiers(expression).sql()</span> -</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> 'SELECT bar.a AS a FROM "Foo".bar'</span> -</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> -</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="sd"> Args:</span> -</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> expression: The expression to transform.</span> -</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="sd"> dialect: The dialect to use in order to decide how to normalize identifiers.</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="sd"> Returns:</span> -</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a><span class="sd"> The transformed expression.</span> -</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a><span class="sd"> """</span> -</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_normalize</span><span class="p">,</span> <span class="n">dialect</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-25"><a href="#L-25"><span class="linenos">25</span></a> -</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="k">def</span> <span class="nf">_normalize</span><span class="p">(</span><span class="n">node</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">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 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-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">node</span><span class="o">.</span><span class="n">quoted</span><span class="p">:</span> -</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">node</span><span class="o">.</span><span class="n">set</span><span class="p">(</span> -</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="s2">"this"</span><span class="p">,</span> -</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> -</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="k">if</span> <span class="n">dialect</span> <span class="ow">in</span> <span class="n">RESOLVES_IDENTIFIERS_AS_UPPERCASE</span> -</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="k">else</span> <span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> -</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="p">)</span> -</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> -</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="k">def</span> <span class="nf">normalize_identifiers</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">E</span><span class="p">,</span> <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 class="o">-></span> <span class="n">E</span><span class="p">:</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="sd"> Normalize all unquoted identifiers to either lower or upper case, depending</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> on the dialect. This essentially makes those identifiers case-insensitive.</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> Note:</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> when they're quoted, so in these cases all identifiers are normalized.</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> Example:</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="sd"> >>> normalize_identifiers(expression).sql()</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> 'SELECT bar.a AS a FROM "Foo".bar'</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a><span class="sd"> Args:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="sd"> expression: The expression to transform.</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a><span class="sd"> dialect: The dialect to use in order to decide how to normalize identifiers.</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a><span class="sd"> Returns:</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a><span class="sd"> The transformed expression.</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a><span class="sd"> """</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">Dialect</span><span class="o">.</span><span class="n">get_or_raise</span><span class="p">(</span><span class="n">dialect</span><span class="p">)</span><span class="o">.</span><span class="n">normalize_identifier</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> </span></pre></div> @@ -107,30 +98,41 @@ </div> <a class="headerlink" href="#normalize_identifiers"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="normalize_identifiers-7"><a href="#normalize_identifiers-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">normalize_identifiers</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">E</span><span class="p">,</span> <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 class="o">-></span> <span class="n">E</span><span class="p">:</span> -</span><span id="normalize_identifiers-8"><a href="#normalize_identifiers-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="normalize_identifiers-9"><a href="#normalize_identifiers-9"><span class="linenos"> 9</span></a><span class="sd"> Normalize all unquoted identifiers to either lower or upper case, depending on</span> -</span><span id="normalize_identifiers-10"><a href="#normalize_identifiers-10"><span class="linenos">10</span></a><span class="sd"> the dialect. This essentially makes those identifiers case-insensitive.</span> -</span><span id="normalize_identifiers-11"><a href="#normalize_identifiers-11"><span class="linenos">11</span></a> -</span><span id="normalize_identifiers-12"><a href="#normalize_identifiers-12"><span class="linenos">12</span></a><span class="sd"> Example:</span> -</span><span id="normalize_identifiers-13"><a href="#normalize_identifiers-13"><span class="linenos">13</span></a><span class="sd"> >>> import sqlglot</span> -</span><span id="normalize_identifiers-14"><a href="#normalize_identifiers-14"><span class="linenos">14</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> -</span><span id="normalize_identifiers-15"><a href="#normalize_identifiers-15"><span class="linenos">15</span></a><span class="sd"> >>> normalize_identifiers(expression).sql()</span> -</span><span id="normalize_identifiers-16"><a href="#normalize_identifiers-16"><span class="linenos">16</span></a><span class="sd"> 'SELECT bar.a AS a FROM "Foo".bar'</span> -</span><span id="normalize_identifiers-17"><a href="#normalize_identifiers-17"><span class="linenos">17</span></a> -</span><span id="normalize_identifiers-18"><a href="#normalize_identifiers-18"><span class="linenos">18</span></a><span class="sd"> Args:</span> -</span><span id="normalize_identifiers-19"><a href="#normalize_identifiers-19"><span class="linenos">19</span></a><span class="sd"> expression: The expression to transform.</span> -</span><span id="normalize_identifiers-20"><a href="#normalize_identifiers-20"><span class="linenos">20</span></a><span class="sd"> dialect: The dialect to use in order to decide how to normalize identifiers.</span> -</span><span id="normalize_identifiers-21"><a href="#normalize_identifiers-21"><span class="linenos">21</span></a> -</span><span id="normalize_identifiers-22"><a href="#normalize_identifiers-22"><span class="linenos">22</span></a><span class="sd"> Returns:</span> -</span><span id="normalize_identifiers-23"><a href="#normalize_identifiers-23"><span class="linenos">23</span></a><span class="sd"> The transformed expression.</span> -</span><span id="normalize_identifiers-24"><a href="#normalize_identifiers-24"><span class="linenos">24</span></a><span class="sd"> """</span> -</span><span id="normalize_identifiers-25"><a href="#normalize_identifiers-25"><span class="linenos">25</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_normalize</span><span class="p">,</span> <span class="n">dialect</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="normalize_identifiers-6"><a href="#normalize_identifiers-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">normalize_identifiers</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">E</span><span class="p">,</span> <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 class="o">-></span> <span class="n">E</span><span class="p">:</span> +</span><span id="normalize_identifiers-7"><a href="#normalize_identifiers-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="normalize_identifiers-8"><a href="#normalize_identifiers-8"><span class="linenos"> 8</span></a><span class="sd"> Normalize all unquoted identifiers to either lower or upper case, depending</span> +</span><span id="normalize_identifiers-9"><a href="#normalize_identifiers-9"><span class="linenos"> 9</span></a><span class="sd"> on the dialect. This essentially makes those identifiers case-insensitive.</span> +</span><span id="normalize_identifiers-10"><a href="#normalize_identifiers-10"><span class="linenos">10</span></a> +</span><span id="normalize_identifiers-11"><a href="#normalize_identifiers-11"><span class="linenos">11</span></a><span class="sd"> Note:</span> +</span><span id="normalize_identifiers-12"><a href="#normalize_identifiers-12"><span class="linenos">12</span></a><span class="sd"> Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even</span> +</span><span id="normalize_identifiers-13"><a href="#normalize_identifiers-13"><span class="linenos">13</span></a><span class="sd"> when they're quoted, so in these cases all identifiers are normalized.</span> +</span><span id="normalize_identifiers-14"><a href="#normalize_identifiers-14"><span class="linenos">14</span></a> +</span><span id="normalize_identifiers-15"><a href="#normalize_identifiers-15"><span class="linenos">15</span></a><span class="sd"> Example:</span> +</span><span id="normalize_identifiers-16"><a href="#normalize_identifiers-16"><span class="linenos">16</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="normalize_identifiers-17"><a href="#normalize_identifiers-17"><span class="linenos">17</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> +</span><span id="normalize_identifiers-18"><a href="#normalize_identifiers-18"><span class="linenos">18</span></a><span class="sd"> >>> normalize_identifiers(expression).sql()</span> +</span><span id="normalize_identifiers-19"><a href="#normalize_identifiers-19"><span class="linenos">19</span></a><span class="sd"> 'SELECT bar.a AS a FROM "Foo".bar'</span> +</span><span id="normalize_identifiers-20"><a href="#normalize_identifiers-20"><span class="linenos">20</span></a> +</span><span id="normalize_identifiers-21"><a href="#normalize_identifiers-21"><span class="linenos">21</span></a><span class="sd"> Args:</span> +</span><span id="normalize_identifiers-22"><a href="#normalize_identifiers-22"><span class="linenos">22</span></a><span class="sd"> expression: The expression to transform.</span> +</span><span id="normalize_identifiers-23"><a href="#normalize_identifiers-23"><span class="linenos">23</span></a><span class="sd"> dialect: The dialect to use in order to decide how to normalize identifiers.</span> +</span><span id="normalize_identifiers-24"><a href="#normalize_identifiers-24"><span class="linenos">24</span></a> +</span><span id="normalize_identifiers-25"><a href="#normalize_identifiers-25"><span class="linenos">25</span></a><span class="sd"> Returns:</span> +</span><span id="normalize_identifiers-26"><a href="#normalize_identifiers-26"><span class="linenos">26</span></a><span class="sd"> The transformed expression.</span> +</span><span id="normalize_identifiers-27"><a href="#normalize_identifiers-27"><span class="linenos">27</span></a><span class="sd"> """</span> +</span><span id="normalize_identifiers-28"><a href="#normalize_identifiers-28"><span class="linenos">28</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">Dialect</span><span class="o">.</span><span class="n">get_or_raise</span><span class="p">(</span><span class="n">dialect</span><span class="p">)</span><span class="o">.</span><span class="n">normalize_identifier</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> </span></pre></div> - <div class="docstring"><p>Normalize all unquoted identifiers to either lower or upper case, depending on -the dialect. This essentially makes those identifiers case-insensitive.</p> + <div class="docstring"><p>Normalize all unquoted identifiers to either lower or upper case, depending +on the dialect. This essentially makes those identifiers case-insensitive.</p> + +<h6 id="note">Note:</h6> + +<blockquote> + <p>Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even + when they're quoted, so in these cases all identifiers are normalized.</p> +</blockquote> <h6 id="example">Example:</h6> |