diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-05-10 06:44:54 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-05-10 06:44:54 +0000 |
commit | d2e9401b18925b5702c5c758af7d4f5b61deb493 (patch) | |
tree | 58dbf490c0457c2908751b3e4b63af13287381ee /docs/sqlglot/transforms.html | |
parent | Adding upstream version 11.7.1. (diff) | |
download | sqlglot-d2e9401b18925b5702c5c758af7d4f5b61deb493.tar.xz sqlglot-d2e9401b18925b5702c5c758af7d4f5b61deb493.zip |
Adding upstream version 12.2.0.upstream/12.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | docs/sqlglot/transforms.html | 504 |
1 files changed, 237 insertions, 267 deletions
diff --git a/docs/sqlglot/transforms.html b/docs/sqlglot/transforms.html index 9694adf..a077ed0 100644 --- a/docs/sqlglot/transforms.html +++ b/docs/sqlglot/transforms.html @@ -200,154 +200,135 @@ </span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a><span class="sd"> Some dialects only allow the precision for parameterized types to be defined in the DDL and not in</span> </span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a><span class="sd"> other expressions. This transforms removes the precision from parameterized types in expressions.</span> </span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a><span class="sd"> """</span> -</span><span id="L-124"><a href="#L-124"><span class="linenos">124</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><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">(</span> -</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="o">**</span><span class="p">{</span> -</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="o">**</span><span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> -</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="s2">"expressions"</span><span class="p">:</span> <span class="p">[</span> -</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="n">node_expression</span> -</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="k">for</span> <span class="n">node_expression</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node_expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> -</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="p">],</span> -</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="p">}</span> -</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="p">)</span> -</span><span id="L-135"><a href="#L-135"><span class="linenos">135</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">DataType</span><span class="p">)</span> -</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> -</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="p">)</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">):</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">node</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="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">expressions</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)])</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a><span class="k">def</span> <span class="nf">unnest_to_explode</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-130"><a href="#L-130"><span class="linenos">130</span></a><span class="w"> </span><span class="sd">"""Convert cross join unnest into lateral view explode (used in presto -> hive)."""</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</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">Select</span><span class="p">):</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">):</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span> <span class="k">if</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"ordinality"</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span> </span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> -</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> -</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a><span class="k">def</span> <span class="nf">unnest_to_explode</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-141"><a href="#L-141"><span class="linenos">141</span></a><span class="w"> </span><span class="sd">"""Convert cross join unnest into lateral view explode (used in presto -> hive)."""</span> -</span><span id="L-142"><a href="#L-142"><span class="linenos">142</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">Select</span><span class="p">):</span> -</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> -</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> -</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">):</span> -</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> -</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span> <span class="k">if</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"ordinality"</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span> -</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> -</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"joins"</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"joins"</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="k">for</span> <span class="n">e</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">unnest</span><span class="o">.</span><span class="n">expressions</span><span class="p">,</span> <span class="n">alias</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">alias</span> <span class="k">else</span> <span class="p">[]):</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> <span class="s2">"laterals"</span><span class="p">,</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">(</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="n">this</span><span class="o">=</span><span class="n">udtf</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">e</span><span class="p">),</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="n">view</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="n">column</span><span class="p">]),</span> <span class="c1"># type: ignore</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="p">),</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="p">)</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="k">return</span> <span class="n">expression</span> </span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> -</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="k">for</span> <span class="n">e</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">unnest</span><span class="o">.</span><span class="n">expressions</span><span class="p">,</span> <span class="n">alias</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">alias</span> <span class="k">else</span> <span class="p">[]):</span> -</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> -</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="s2">"laterals"</span><span class="p">,</span> -</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">(</span> -</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="n">this</span><span class="o">=</span><span class="n">udtf</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">e</span><span class="p">),</span> -</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="n">view</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> -</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="n">column</span><span class="p">]),</span> <span class="c1"># type: ignore</span> -</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="p">),</span> -</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="p">)</span> -</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a><span class="k">def</span> <span class="nf">explode_to_unnest</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-154"><a href="#L-154"><span class="linenos">154</span></a><span class="w"> </span><span class="sd">"""Convert explode/posexplode into unnest (used in hive -> presto)."""</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</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">Select</span><span class="p">):</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">taken_select_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="n">taken_source_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> <span class="n">to_replace</span> <span class="o">=</span> <span class="n">select</span> </span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> -</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a><span class="k">def</span> <span class="nf">explode_to_unnest</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-165"><a href="#L-165"><span class="linenos">165</span></a><span class="w"> </span><span class="sd">"""Convert explode/posexplode into unnest (used in hive -> presto)."""</span> -</span><span id="L-166"><a href="#L-166"><span class="linenos">166</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">Select</span><span class="p">):</span> -</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> -</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> -</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">taken_select_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> -</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="n">taken_source_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> -</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> -</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> -</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="n">to_replace</span> <span class="o">=</span> <span class="n">select</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Aliases</span><span class="p">):</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> </span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> -</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="s2">""</span> -</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)):</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="n">is_posexplode</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)</span> </span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> -</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> -</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">alias</span> -</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Aliases</span><span class="p">):</span> -</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> -</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> -</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> -</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)):</span> -</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="n">is_posexplode</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)</span> -</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> -</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="n">explode_arg</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">copy</span><span class="p">()],</span> <span class="n">ordinality</span><span class="o">=</span><span class="n">is_posexplode</span><span class="p">)</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="n">explode_arg</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">copy</span><span class="p">()],</span> <span class="n">ordinality</span><span class="o">=</span><span class="n">is_posexplode</span><span class="p">)</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="c1"># This ensures that we won't use [POS]EXPLODE's argument as a new selection</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">explode_arg</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">output_name</span><span class="p">)</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="n">unnest_source_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_source_names</span><span class="p">,</span> <span class="s2">"_u"</span><span class="p">)</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="n">taken_source_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">unnest_source_alias</span><span class="p">)</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">explode_alias</span><span class="p">:</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"col"</span><span class="p">)</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">)</span> </span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a> -</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="c1"># This ensures that we won't use [POS]EXPLODE's argument as a new selection</span> -</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">explode_arg</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> -</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">output_name</span><span class="p">)</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"pos"</span><span class="p">)</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">)</span> </span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> -</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a> <span class="n">unnest_source_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_source_names</span><span class="p">,</span> <span class="s2">"_u"</span><span class="p">)</span> -</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> <span class="n">taken_source_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">unnest_source_alias</span><span class="p">)</span> -</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a> -</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">explode_alias</span><span class="p">:</span> -</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"col"</span><span class="p">)</span> -</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">)</span> -</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> -</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> -</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"pos"</span><span class="p">)</span> -</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">)</span> -</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> -</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> -</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">,</span> <span class="n">pos_alias</span><span class="p">]</span> -</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> -</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">,</span> <span class="n">explode_alias</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-211"><a href="#L-211"><span class="linenos">211</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">]</span> -</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">))</span> -</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> -</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">unnest_source_alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">column_names</span><span class="p">)</span> -</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> -</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">):</span> -</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">from_</span><span class="p">(</span><span class="n">unnest</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-219"><a href="#L-219"><span class="linenos">219</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</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-196"><a href="#L-196"><span class="linenos">196</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">,</span> <span class="n">pos_alias</span><span class="p">]</span> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">,</span> <span class="n">explode_alias</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-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">]</span> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">))</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">unnest_source_alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">column_names</span><span class="p">)</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">):</span> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">from_</span><span class="p">(</span><span class="n">unnest</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-208"><a href="#L-208"><span class="linenos">208</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</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-210"><a href="#L-210"><span class="linenos">210</span></a> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a><span class="k">def</span> <span class="nf">remove_target_from_merge</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-215"><a href="#L-215"><span class="linenos">215</span></a><span class="w"> </span><span class="sd">"""Remove table refs from columns in when statements."""</span> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</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">Merge</span><span class="p">):</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> <span class="n">targets</span> <span class="o">=</span> <span class="p">{</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">}</span> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a> <span class="k">if</span> <span class="n">alias</span><span class="p">:</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> <span class="n">targets</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> </span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a> -</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a> <span class="k">return</span> <span class="n">expression</span> -</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a> -</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a> -</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a><span class="k">def</span> <span class="nf">remove_target_from_merge</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-226"><a href="#L-226"><span class="linenos">226</span></a><span class="w"> </span><span class="sd">"""Remove table refs from columns in when statements."""</span> -</span><span id="L-227"><a href="#L-227"><span class="linenos">227</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">Merge</span><span class="p">):</span> -</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> -</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a> <span class="n">targets</span> <span class="o">=</span> <span class="p">{</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">}</span> -</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="k">if</span> <span class="n">alias</span><span class="p">:</span> -</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="n">targets</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> -</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> -</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="k">for</span> <span class="n">when</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> -</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> <span class="n">when</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span> -</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="L-236"><a href="#L-236"><span class="linenos">236</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">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"table"</span><span class="p">)</span> <span class="ow">in</span> <span class="n">targets</span> -</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> -</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a> <span class="p">)</span> -</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a> <span class="k">for</span> <span class="n">when</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a> <span class="n">when</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</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">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"table"</span><span class="p">)</span> <span class="ow">in</span> <span class="n">targets</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a> <span class="p">)</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a><span class="k">def</span> <span class="nf">preprocess</span><span class="p">(</span> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="n">transforms</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">List</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="n">exp</span><span class="o">.</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><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Generator</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="nb">str</span><span class="p">]:</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a><span class="sd"> Creates a new transform by chaining a sequence of transformations and converts the resulting</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a><span class="sd"> expression to SQL, using an appropriate `Generator.TRANSFORMS` function.</span> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a><span class="sd"> Args:</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a><span class="sd"> transforms: sequence of transform functions. These will be called in order.</span> </span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> -</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> -</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a><span class="k">def</span> <span class="nf">preprocess</span><span class="p">(</span> -</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> <span class="n">transforms</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">List</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="n">exp</span><span class="o">.</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><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Generator</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="nb">str</span><span class="p">]:</span> -</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a><span class="sd"> Creates a new transform by chaining a sequence of transformations and converts the resulting</span> -</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a><span class="sd"> expression to SQL, using an appropriate `Generator.TRANSFORMS` function.</span> -</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> -</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a><span class="sd"> Args:</span> -</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a><span class="sd"> transforms: sequence of transform functions. These will be called in order.</span> -</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> -</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a><span class="sd"> Returns:</span> -</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a><span class="sd"> Function that can be used as a generator transform.</span> -</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a><span class="sd"> """</span> -</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> -</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="k">def</span> <span class="nf">_to_sql</span><span class="p">(</span><span class="bp">self</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">str</span><span class="p">:</span> -</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">0</span><span class="p">](</span><span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> -</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">t</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">key</span> <span class="o">+</span> <span class="s2">"_sql"</span><span class="p">)(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> -</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="k">return</span> <span class="n">_to_sql</span> -</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> -</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> -</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a><span class="n">UNALIAS_GROUP</span> <span class="o">=</span> <span class="p">{</span><span class="n">exp</span><span class="o">.</span><span class="n">Group</span><span class="p">:</span> <span class="n">preprocess</span><span class="p">([</span><span class="n">unalias_group</span><span class="p">])}</span> -</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a><span class="n">ELIMINATE_DISTINCT_ON</span> <span class="o">=</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="n">preprocess</span><span class="p">([</span><span class="n">eliminate_distinct_on</span><span class="p">])}</span> -</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a><span class="n">ELIMINATE_QUALIFY</span> <span class="o">=</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="n">preprocess</span><span class="p">([</span><span class="n">eliminate_qualify</span><span class="p">])}</span> -</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a><span class="n">REMOVE_PRECISION_PARAMETERIZED_TYPES</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">:</span> <span class="n">preprocess</span><span class="p">([</span><span class="n">remove_precision_parameterized_types</span><span class="p">])</span> -</span><span id="L-271"><a href="#L-271"><span class="linenos">271</span></a><span class="p">}</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a><span class="sd"> Returns:</span> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a><span class="sd"> Function that can be used as a generator transform.</span> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a><span class="sd"> """</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> <span class="k">def</span> <span class="nf">_to_sql</span><span class="p">(</span><span class="bp">self</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">str</span><span class="p">:</span> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">0</span><span class="p">](</span><span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">t</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">key</span> <span class="o">+</span> <span class="s2">"_sql"</span><span class="p">)(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> <span class="k">return</span> <span class="n">_to_sql</span> </span></pre></div> @@ -571,20 +552,9 @@ otherwise we won't be able to refer to it in the outer query's WHERE clause.</p> </span><span id="remove_precision_parameterized_types-122"><a href="#remove_precision_parameterized_types-122"><span class="linenos">122</span></a><span class="sd"> Some dialects only allow the precision for parameterized types to be defined in the DDL and not in</span> </span><span id="remove_precision_parameterized_types-123"><a href="#remove_precision_parameterized_types-123"><span class="linenos">123</span></a><span class="sd"> other expressions. This transforms removes the precision from parameterized types in expressions.</span> </span><span id="remove_precision_parameterized_types-124"><a href="#remove_precision_parameterized_types-124"><span class="linenos">124</span></a><span class="sd"> """</span> -</span><span id="remove_precision_parameterized_types-125"><a href="#remove_precision_parameterized_types-125"><span class="linenos">125</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><span id="remove_precision_parameterized_types-126"><a href="#remove_precision_parameterized_types-126"><span class="linenos">126</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">(</span> -</span><span id="remove_precision_parameterized_types-127"><a href="#remove_precision_parameterized_types-127"><span class="linenos">127</span></a> <span class="o">**</span><span class="p">{</span> -</span><span id="remove_precision_parameterized_types-128"><a href="#remove_precision_parameterized_types-128"><span class="linenos">128</span></a> <span class="o">**</span><span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> -</span><span id="remove_precision_parameterized_types-129"><a href="#remove_precision_parameterized_types-129"><span class="linenos">129</span></a> <span class="s2">"expressions"</span><span class="p">:</span> <span class="p">[</span> -</span><span id="remove_precision_parameterized_types-130"><a href="#remove_precision_parameterized_types-130"><span class="linenos">130</span></a> <span class="n">node_expression</span> -</span><span id="remove_precision_parameterized_types-131"><a href="#remove_precision_parameterized_types-131"><span class="linenos">131</span></a> <span class="k">for</span> <span class="n">node_expression</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="remove_precision_parameterized_types-132"><a href="#remove_precision_parameterized_types-132"><span class="linenos">132</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node_expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> -</span><span id="remove_precision_parameterized_types-133"><a href="#remove_precision_parameterized_types-133"><span class="linenos">133</span></a> <span class="p">],</span> -</span><span id="remove_precision_parameterized_types-134"><a href="#remove_precision_parameterized_types-134"><span class="linenos">134</span></a> <span class="p">}</span> -</span><span id="remove_precision_parameterized_types-135"><a href="#remove_precision_parameterized_types-135"><span class="linenos">135</span></a> <span class="p">)</span> -</span><span id="remove_precision_parameterized_types-136"><a href="#remove_precision_parameterized_types-136"><span class="linenos">136</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">DataType</span><span class="p">)</span> -</span><span id="remove_precision_parameterized_types-137"><a href="#remove_precision_parameterized_types-137"><span class="linenos">137</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> -</span><span id="remove_precision_parameterized_types-138"><a href="#remove_precision_parameterized_types-138"><span class="linenos">138</span></a> <span class="p">)</span> +</span><span id="remove_precision_parameterized_types-125"><a href="#remove_precision_parameterized_types-125"><span class="linenos">125</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">):</span> +</span><span id="remove_precision_parameterized_types-126"><a href="#remove_precision_parameterized_types-126"><span class="linenos">126</span></a> <span class="n">node</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="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">expressions</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)])</span> +</span><span id="remove_precision_parameterized_types-127"><a href="#remove_precision_parameterized_types-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -605,28 +575,28 @@ other expressions. This transforms removes the precision from parameterized type </div> <a class="headerlink" href="#unnest_to_explode"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="unnest_to_explode-141"><a href="#unnest_to_explode-141"><span class="linenos">141</span></a><span class="k">def</span> <span class="nf">unnest_to_explode</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="unnest_to_explode-142"><a href="#unnest_to_explode-142"><span class="linenos">142</span></a><span class="w"> </span><span class="sd">"""Convert cross join unnest into lateral view explode (used in presto -> hive)."""</span> -</span><span id="unnest_to_explode-143"><a href="#unnest_to_explode-143"><span class="linenos">143</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">Select</span><span class="p">):</span> -</span><span id="unnest_to_explode-144"><a href="#unnest_to_explode-144"><span class="linenos">144</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> -</span><span id="unnest_to_explode-145"><a href="#unnest_to_explode-145"><span class="linenos">145</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span> -</span><span id="unnest_to_explode-146"><a href="#unnest_to_explode-146"><span class="linenos">146</span></a> -</span><span id="unnest_to_explode-147"><a href="#unnest_to_explode-147"><span class="linenos">147</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">):</span> -</span><span id="unnest_to_explode-148"><a href="#unnest_to_explode-148"><span class="linenos">148</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> -</span><span id="unnest_to_explode-149"><a href="#unnest_to_explode-149"><span class="linenos">149</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span> <span class="k">if</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"ordinality"</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span> -</span><span id="unnest_to_explode-150"><a href="#unnest_to_explode-150"><span class="linenos">150</span></a> -</span><span id="unnest_to_explode-151"><a href="#unnest_to_explode-151"><span class="linenos">151</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"joins"</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> -</span><span id="unnest_to_explode-152"><a href="#unnest_to_explode-152"><span class="linenos">152</span></a> -</span><span id="unnest_to_explode-153"><a href="#unnest_to_explode-153"><span class="linenos">153</span></a> <span class="k">for</span> <span class="n">e</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">unnest</span><span class="o">.</span><span class="n">expressions</span><span class="p">,</span> <span class="n">alias</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">alias</span> <span class="k">else</span> <span class="p">[]):</span> -</span><span id="unnest_to_explode-154"><a href="#unnest_to_explode-154"><span class="linenos">154</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> -</span><span id="unnest_to_explode-155"><a href="#unnest_to_explode-155"><span class="linenos">155</span></a> <span class="s2">"laterals"</span><span class="p">,</span> -</span><span id="unnest_to_explode-156"><a href="#unnest_to_explode-156"><span class="linenos">156</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">(</span> -</span><span id="unnest_to_explode-157"><a href="#unnest_to_explode-157"><span class="linenos">157</span></a> <span class="n">this</span><span class="o">=</span><span class="n">udtf</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">e</span><span class="p">),</span> -</span><span id="unnest_to_explode-158"><a href="#unnest_to_explode-158"><span class="linenos">158</span></a> <span class="n">view</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> -</span><span id="unnest_to_explode-159"><a href="#unnest_to_explode-159"><span class="linenos">159</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="n">column</span><span class="p">]),</span> <span class="c1"># type: ignore</span> -</span><span id="unnest_to_explode-160"><a href="#unnest_to_explode-160"><span class="linenos">160</span></a> <span class="p">),</span> -</span><span id="unnest_to_explode-161"><a href="#unnest_to_explode-161"><span class="linenos">161</span></a> <span class="p">)</span> -</span><span id="unnest_to_explode-162"><a href="#unnest_to_explode-162"><span class="linenos">162</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="unnest_to_explode-130"><a href="#unnest_to_explode-130"><span class="linenos">130</span></a><span class="k">def</span> <span class="nf">unnest_to_explode</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="unnest_to_explode-131"><a href="#unnest_to_explode-131"><span class="linenos">131</span></a><span class="w"> </span><span class="sd">"""Convert cross join unnest into lateral view explode (used in presto -> hive)."""</span> +</span><span id="unnest_to_explode-132"><a href="#unnest_to_explode-132"><span class="linenos">132</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">Select</span><span class="p">):</span> +</span><span id="unnest_to_explode-133"><a href="#unnest_to_explode-133"><span class="linenos">133</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> +</span><span id="unnest_to_explode-134"><a href="#unnest_to_explode-134"><span class="linenos">134</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span> +</span><span id="unnest_to_explode-135"><a href="#unnest_to_explode-135"><span class="linenos">135</span></a> +</span><span id="unnest_to_explode-136"><a href="#unnest_to_explode-136"><span class="linenos">136</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">):</span> +</span><span id="unnest_to_explode-137"><a href="#unnest_to_explode-137"><span class="linenos">137</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> +</span><span id="unnest_to_explode-138"><a href="#unnest_to_explode-138"><span class="linenos">138</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span> <span class="k">if</span> <span class="n">unnest</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"ordinality"</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span> +</span><span id="unnest_to_explode-139"><a href="#unnest_to_explode-139"><span class="linenos">139</span></a> +</span><span id="unnest_to_explode-140"><a href="#unnest_to_explode-140"><span class="linenos">140</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"joins"</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> +</span><span id="unnest_to_explode-141"><a href="#unnest_to_explode-141"><span class="linenos">141</span></a> +</span><span id="unnest_to_explode-142"><a href="#unnest_to_explode-142"><span class="linenos">142</span></a> <span class="k">for</span> <span class="n">e</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">unnest</span><span class="o">.</span><span class="n">expressions</span><span class="p">,</span> <span class="n">alias</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">alias</span> <span class="k">else</span> <span class="p">[]):</span> +</span><span id="unnest_to_explode-143"><a href="#unnest_to_explode-143"><span class="linenos">143</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> +</span><span id="unnest_to_explode-144"><a href="#unnest_to_explode-144"><span class="linenos">144</span></a> <span class="s2">"laterals"</span><span class="p">,</span> +</span><span id="unnest_to_explode-145"><a href="#unnest_to_explode-145"><span class="linenos">145</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">(</span> +</span><span id="unnest_to_explode-146"><a href="#unnest_to_explode-146"><span class="linenos">146</span></a> <span class="n">this</span><span class="o">=</span><span class="n">udtf</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">e</span><span class="p">),</span> +</span><span id="unnest_to_explode-147"><a href="#unnest_to_explode-147"><span class="linenos">147</span></a> <span class="n">view</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> +</span><span id="unnest_to_explode-148"><a href="#unnest_to_explode-148"><span class="linenos">148</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="n">column</span><span class="p">]),</span> <span class="c1"># type: ignore</span> +</span><span id="unnest_to_explode-149"><a href="#unnest_to_explode-149"><span class="linenos">149</span></a> <span class="p">),</span> +</span><span id="unnest_to_explode-150"><a href="#unnest_to_explode-150"><span class="linenos">150</span></a> <span class="p">)</span> +</span><span id="unnest_to_explode-151"><a href="#unnest_to_explode-151"><span class="linenos">151</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -646,65 +616,65 @@ other expressions. This transforms removes the precision from parameterized type </div> <a class="headerlink" href="#explode_to_unnest"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="explode_to_unnest-165"><a href="#explode_to_unnest-165"><span class="linenos">165</span></a><span class="k">def</span> <span class="nf">explode_to_unnest</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="explode_to_unnest-166"><a href="#explode_to_unnest-166"><span class="linenos">166</span></a><span class="w"> </span><span class="sd">"""Convert explode/posexplode into unnest (used in hive -> presto)."""</span> -</span><span id="explode_to_unnest-167"><a href="#explode_to_unnest-167"><span class="linenos">167</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">Select</span><span class="p">):</span> -</span><span id="explode_to_unnest-168"><a href="#explode_to_unnest-168"><span class="linenos">168</span></a> <span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> -</span><span id="explode_to_unnest-169"><a href="#explode_to_unnest-169"><span class="linenos">169</span></a> -</span><span id="explode_to_unnest-170"><a href="#explode_to_unnest-170"><span class="linenos">170</span></a> <span class="n">taken_select_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> -</span><span id="explode_to_unnest-171"><a href="#explode_to_unnest-171"><span class="linenos">171</span></a> <span class="n">taken_source_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> -</span><span id="explode_to_unnest-172"><a href="#explode_to_unnest-172"><span class="linenos">172</span></a> -</span><span id="explode_to_unnest-173"><a href="#explode_to_unnest-173"><span class="linenos">173</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> -</span><span id="explode_to_unnest-174"><a href="#explode_to_unnest-174"><span class="linenos">174</span></a> <span class="n">to_replace</span> <span class="o">=</span> <span class="n">select</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="explode_to_unnest-154"><a href="#explode_to_unnest-154"><span class="linenos">154</span></a><span class="k">def</span> <span class="nf">explode_to_unnest</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="explode_to_unnest-155"><a href="#explode_to_unnest-155"><span class="linenos">155</span></a><span class="w"> </span><span class="sd">"""Convert explode/posexplode into unnest (used in hive -> presto)."""</span> +</span><span id="explode_to_unnest-156"><a href="#explode_to_unnest-156"><span class="linenos">156</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">Select</span><span class="p">):</span> +</span><span id="explode_to_unnest-157"><a href="#explode_to_unnest-157"><span class="linenos">157</span></a> <span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> +</span><span id="explode_to_unnest-158"><a href="#explode_to_unnest-158"><span class="linenos">158</span></a> +</span><span id="explode_to_unnest-159"><a href="#explode_to_unnest-159"><span class="linenos">159</span></a> <span class="n">taken_select_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="explode_to_unnest-160"><a href="#explode_to_unnest-160"><span class="linenos">160</span></a> <span class="n">taken_source_names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> +</span><span id="explode_to_unnest-161"><a href="#explode_to_unnest-161"><span class="linenos">161</span></a> +</span><span id="explode_to_unnest-162"><a href="#explode_to_unnest-162"><span class="linenos">162</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="explode_to_unnest-163"><a href="#explode_to_unnest-163"><span class="linenos">163</span></a> <span class="n">to_replace</span> <span class="o">=</span> <span class="n">select</span> +</span><span id="explode_to_unnest-164"><a href="#explode_to_unnest-164"><span class="linenos">164</span></a> +</span><span id="explode_to_unnest-165"><a href="#explode_to_unnest-165"><span class="linenos">165</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="explode_to_unnest-166"><a href="#explode_to_unnest-166"><span class="linenos">166</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="explode_to_unnest-167"><a href="#explode_to_unnest-167"><span class="linenos">167</span></a> +</span><span id="explode_to_unnest-168"><a href="#explode_to_unnest-168"><span class="linenos">168</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="explode_to_unnest-169"><a href="#explode_to_unnest-169"><span class="linenos">169</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">alias</span> +</span><span id="explode_to_unnest-170"><a href="#explode_to_unnest-170"><span class="linenos">170</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="explode_to_unnest-171"><a href="#explode_to_unnest-171"><span class="linenos">171</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Aliases</span><span class="p">):</span> +</span><span id="explode_to_unnest-172"><a href="#explode_to_unnest-172"><span class="linenos">172</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> +</span><span id="explode_to_unnest-173"><a href="#explode_to_unnest-173"><span class="linenos">173</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> +</span><span id="explode_to_unnest-174"><a href="#explode_to_unnest-174"><span class="linenos">174</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> </span><span id="explode_to_unnest-175"><a href="#explode_to_unnest-175"><span class="linenos">175</span></a> -</span><span id="explode_to_unnest-176"><a href="#explode_to_unnest-176"><span class="linenos">176</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="s2">""</span> -</span><span id="explode_to_unnest-177"><a href="#explode_to_unnest-177"><span class="linenos">177</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="s2">""</span> +</span><span id="explode_to_unnest-176"><a href="#explode_to_unnest-176"><span class="linenos">176</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)):</span> +</span><span id="explode_to_unnest-177"><a href="#explode_to_unnest-177"><span class="linenos">177</span></a> <span class="n">is_posexplode</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)</span> </span><span id="explode_to_unnest-178"><a href="#explode_to_unnest-178"><span class="linenos">178</span></a> -</span><span id="explode_to_unnest-179"><a href="#explode_to_unnest-179"><span class="linenos">179</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> -</span><span id="explode_to_unnest-180"><a href="#explode_to_unnest-180"><span class="linenos">180</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">alias</span> -</span><span id="explode_to_unnest-181"><a href="#explode_to_unnest-181"><span class="linenos">181</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="explode_to_unnest-182"><a href="#explode_to_unnest-182"><span class="linenos">182</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Aliases</span><span class="p">):</span> -</span><span id="explode_to_unnest-183"><a href="#explode_to_unnest-183"><span class="linenos">183</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> -</span><span id="explode_to_unnest-184"><a href="#explode_to_unnest-184"><span class="linenos">184</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">aliases</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> -</span><span id="explode_to_unnest-185"><a href="#explode_to_unnest-185"><span class="linenos">185</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="explode_to_unnest-186"><a href="#explode_to_unnest-186"><span class="linenos">186</span></a> -</span><span id="explode_to_unnest-187"><a href="#explode_to_unnest-187"><span class="linenos">187</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)):</span> -</span><span id="explode_to_unnest-188"><a href="#explode_to_unnest-188"><span class="linenos">188</span></a> <span class="n">is_posexplode</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Posexplode</span><span class="p">)</span> -</span><span id="explode_to_unnest-189"><a href="#explode_to_unnest-189"><span class="linenos">189</span></a> -</span><span id="explode_to_unnest-190"><a href="#explode_to_unnest-190"><span class="linenos">190</span></a> <span class="n">explode_arg</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> -</span><span id="explode_to_unnest-191"><a href="#explode_to_unnest-191"><span class="linenos">191</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">copy</span><span class="p">()],</span> <span class="n">ordinality</span><span class="o">=</span><span class="n">is_posexplode</span><span class="p">)</span> +</span><span id="explode_to_unnest-179"><a href="#explode_to_unnest-179"><span class="linenos">179</span></a> <span class="n">explode_arg</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="explode_to_unnest-180"><a href="#explode_to_unnest-180"><span class="linenos">180</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unnest</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">copy</span><span class="p">()],</span> <span class="n">ordinality</span><span class="o">=</span><span class="n">is_posexplode</span><span class="p">)</span> +</span><span id="explode_to_unnest-181"><a href="#explode_to_unnest-181"><span class="linenos">181</span></a> +</span><span id="explode_to_unnest-182"><a href="#explode_to_unnest-182"><span class="linenos">182</span></a> <span class="c1"># This ensures that we won't use [POS]EXPLODE's argument as a new selection</span> +</span><span id="explode_to_unnest-183"><a href="#explode_to_unnest-183"><span class="linenos">183</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">explode_arg</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="explode_to_unnest-184"><a href="#explode_to_unnest-184"><span class="linenos">184</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">output_name</span><span class="p">)</span> +</span><span id="explode_to_unnest-185"><a href="#explode_to_unnest-185"><span class="linenos">185</span></a> +</span><span id="explode_to_unnest-186"><a href="#explode_to_unnest-186"><span class="linenos">186</span></a> <span class="n">unnest_source_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_source_names</span><span class="p">,</span> <span class="s2">"_u"</span><span class="p">)</span> +</span><span id="explode_to_unnest-187"><a href="#explode_to_unnest-187"><span class="linenos">187</span></a> <span class="n">taken_source_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">unnest_source_alias</span><span class="p">)</span> +</span><span id="explode_to_unnest-188"><a href="#explode_to_unnest-188"><span class="linenos">188</span></a> +</span><span id="explode_to_unnest-189"><a href="#explode_to_unnest-189"><span class="linenos">189</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">explode_alias</span><span class="p">:</span> +</span><span id="explode_to_unnest-190"><a href="#explode_to_unnest-190"><span class="linenos">190</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"col"</span><span class="p">)</span> +</span><span id="explode_to_unnest-191"><a href="#explode_to_unnest-191"><span class="linenos">191</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">)</span> </span><span id="explode_to_unnest-192"><a href="#explode_to_unnest-192"><span class="linenos">192</span></a> -</span><span id="explode_to_unnest-193"><a href="#explode_to_unnest-193"><span class="linenos">193</span></a> <span class="c1"># This ensures that we won't use [POS]EXPLODE's argument as a new selection</span> -</span><span id="explode_to_unnest-194"><a href="#explode_to_unnest-194"><span class="linenos">194</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">explode_arg</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> -</span><span id="explode_to_unnest-195"><a href="#explode_to_unnest-195"><span class="linenos">195</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_arg</span><span class="o">.</span><span class="n">output_name</span><span class="p">)</span> +</span><span id="explode_to_unnest-193"><a href="#explode_to_unnest-193"><span class="linenos">193</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> +</span><span id="explode_to_unnest-194"><a href="#explode_to_unnest-194"><span class="linenos">194</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"pos"</span><span class="p">)</span> +</span><span id="explode_to_unnest-195"><a href="#explode_to_unnest-195"><span class="linenos">195</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">)</span> </span><span id="explode_to_unnest-196"><a href="#explode_to_unnest-196"><span class="linenos">196</span></a> -</span><span id="explode_to_unnest-197"><a href="#explode_to_unnest-197"><span class="linenos">197</span></a> <span class="n">unnest_source_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_source_names</span><span class="p">,</span> <span class="s2">"_u"</span><span class="p">)</span> -</span><span id="explode_to_unnest-198"><a href="#explode_to_unnest-198"><span class="linenos">198</span></a> <span class="n">taken_source_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">unnest_source_alias</span><span class="p">)</span> -</span><span id="explode_to_unnest-199"><a href="#explode_to_unnest-199"><span class="linenos">199</span></a> -</span><span id="explode_to_unnest-200"><a href="#explode_to_unnest-200"><span class="linenos">200</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">explode_alias</span><span class="p">:</span> -</span><span id="explode_to_unnest-201"><a href="#explode_to_unnest-201"><span class="linenos">201</span></a> <span class="n">explode_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"col"</span><span class="p">)</span> -</span><span id="explode_to_unnest-202"><a href="#explode_to_unnest-202"><span class="linenos">202</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">)</span> -</span><span id="explode_to_unnest-203"><a href="#explode_to_unnest-203"><span class="linenos">203</span></a> -</span><span id="explode_to_unnest-204"><a href="#explode_to_unnest-204"><span class="linenos">204</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> -</span><span id="explode_to_unnest-205"><a href="#explode_to_unnest-205"><span class="linenos">205</span></a> <span class="n">pos_alias</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken_select_names</span><span class="p">,</span> <span class="s2">"pos"</span><span class="p">)</span> -</span><span id="explode_to_unnest-206"><a href="#explode_to_unnest-206"><span class="linenos">206</span></a> <span class="n">taken_select_names</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">)</span> -</span><span id="explode_to_unnest-207"><a href="#explode_to_unnest-207"><span class="linenos">207</span></a> -</span><span id="explode_to_unnest-208"><a href="#explode_to_unnest-208"><span class="linenos">208</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> -</span><span id="explode_to_unnest-209"><a href="#explode_to_unnest-209"><span class="linenos">209</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">,</span> <span class="n">pos_alias</span><span class="p">]</span> -</span><span id="explode_to_unnest-210"><a href="#explode_to_unnest-210"><span class="linenos">210</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> -</span><span id="explode_to_unnest-211"><a href="#explode_to_unnest-211"><span class="linenos">211</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">,</span> <span class="n">explode_alias</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="explode_to_unnest-212"><a href="#explode_to_unnest-212"><span class="linenos">212</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="explode_to_unnest-213"><a href="#explode_to_unnest-213"><span class="linenos">213</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">]</span> -</span><span id="explode_to_unnest-214"><a href="#explode_to_unnest-214"><span class="linenos">214</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">))</span> -</span><span id="explode_to_unnest-215"><a href="#explode_to_unnest-215"><span class="linenos">215</span></a> -</span><span id="explode_to_unnest-216"><a href="#explode_to_unnest-216"><span class="linenos">216</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">unnest_source_alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">column_names</span><span class="p">)</span> -</span><span id="explode_to_unnest-217"><a href="#explode_to_unnest-217"><span class="linenos">217</span></a> -</span><span id="explode_to_unnest-218"><a href="#explode_to_unnest-218"><span class="linenos">218</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">):</span> -</span><span id="explode_to_unnest-219"><a href="#explode_to_unnest-219"><span class="linenos">219</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">from_</span><span class="p">(</span><span class="n">unnest</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="explode_to_unnest-220"><a href="#explode_to_unnest-220"><span class="linenos">220</span></a> <span class="k">else</span><span class="p">:</span> -</span><span id="explode_to_unnest-221"><a href="#explode_to_unnest-221"><span class="linenos">221</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</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="explode_to_unnest-222"><a href="#explode_to_unnest-222"><span class="linenos">222</span></a> -</span><span id="explode_to_unnest-223"><a href="#explode_to_unnest-223"><span class="linenos">223</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="explode_to_unnest-197"><a href="#explode_to_unnest-197"><span class="linenos">197</span></a> <span class="k">if</span> <span class="n">is_posexplode</span><span class="p">:</span> +</span><span id="explode_to_unnest-198"><a href="#explode_to_unnest-198"><span class="linenos">198</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">,</span> <span class="n">pos_alias</span><span class="p">]</span> +</span><span id="explode_to_unnest-199"><a href="#explode_to_unnest-199"><span class="linenos">199</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="explode_to_unnest-200"><a href="#explode_to_unnest-200"><span class="linenos">200</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">pos_alias</span><span class="p">,</span> <span class="n">explode_alias</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="explode_to_unnest-201"><a href="#explode_to_unnest-201"><span class="linenos">201</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="explode_to_unnest-202"><a href="#explode_to_unnest-202"><span class="linenos">202</span></a> <span class="n">column_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">explode_alias</span><span class="p">]</span> +</span><span id="explode_to_unnest-203"><a href="#explode_to_unnest-203"><span class="linenos">203</span></a> <span class="n">to_replace</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">explode_alias</span><span class="p">))</span> +</span><span id="explode_to_unnest-204"><a href="#explode_to_unnest-204"><span class="linenos">204</span></a> +</span><span id="explode_to_unnest-205"><a href="#explode_to_unnest-205"><span class="linenos">205</span></a> <span class="n">unnest</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">unnest_source_alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">column_names</span><span class="p">)</span> +</span><span id="explode_to_unnest-206"><a href="#explode_to_unnest-206"><span class="linenos">206</span></a> +</span><span id="explode_to_unnest-207"><a href="#explode_to_unnest-207"><span class="linenos">207</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">):</span> +</span><span id="explode_to_unnest-208"><a href="#explode_to_unnest-208"><span class="linenos">208</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">from_</span><span class="p">(</span><span class="n">unnest</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="explode_to_unnest-209"><a href="#explode_to_unnest-209"><span class="linenos">209</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="explode_to_unnest-210"><a href="#explode_to_unnest-210"><span class="linenos">210</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">unnest</span><span class="p">,</span> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</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="explode_to_unnest-211"><a href="#explode_to_unnest-211"><span class="linenos">211</span></a> +</span><span id="explode_to_unnest-212"><a href="#explode_to_unnest-212"><span class="linenos">212</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -724,22 +694,22 @@ other expressions. This transforms removes the precision from parameterized type </div> <a class="headerlink" href="#remove_target_from_merge"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="remove_target_from_merge-226"><a href="#remove_target_from_merge-226"><span class="linenos">226</span></a><span class="k">def</span> <span class="nf">remove_target_from_merge</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="remove_target_from_merge-227"><a href="#remove_target_from_merge-227"><span class="linenos">227</span></a><span class="w"> </span><span class="sd">"""Remove table refs from columns in when statements."""</span> -</span><span id="remove_target_from_merge-228"><a href="#remove_target_from_merge-228"><span class="linenos">228</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">Merge</span><span class="p">):</span> -</span><span id="remove_target_from_merge-229"><a href="#remove_target_from_merge-229"><span class="linenos">229</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> -</span><span id="remove_target_from_merge-230"><a href="#remove_target_from_merge-230"><span class="linenos">230</span></a> <span class="n">targets</span> <span class="o">=</span> <span class="p">{</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">}</span> -</span><span id="remove_target_from_merge-231"><a href="#remove_target_from_merge-231"><span class="linenos">231</span></a> <span class="k">if</span> <span class="n">alias</span><span class="p">:</span> -</span><span id="remove_target_from_merge-232"><a href="#remove_target_from_merge-232"><span class="linenos">232</span></a> <span class="n">targets</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> -</span><span id="remove_target_from_merge-233"><a href="#remove_target_from_merge-233"><span class="linenos">233</span></a> -</span><span id="remove_target_from_merge-234"><a href="#remove_target_from_merge-234"><span class="linenos">234</span></a> <span class="k">for</span> <span class="n">when</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> -</span><span id="remove_target_from_merge-235"><a href="#remove_target_from_merge-235"><span class="linenos">235</span></a> <span class="n">when</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span> -</span><span id="remove_target_from_merge-236"><a href="#remove_target_from_merge-236"><span class="linenos">236</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> -</span><span id="remove_target_from_merge-237"><a href="#remove_target_from_merge-237"><span class="linenos">237</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">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"table"</span><span class="p">)</span> <span class="ow">in</span> <span class="n">targets</span> -</span><span id="remove_target_from_merge-238"><a href="#remove_target_from_merge-238"><span class="linenos">238</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> -</span><span id="remove_target_from_merge-239"><a href="#remove_target_from_merge-239"><span class="linenos">239</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> -</span><span id="remove_target_from_merge-240"><a href="#remove_target_from_merge-240"><span class="linenos">240</span></a> <span class="p">)</span> -</span><span id="remove_target_from_merge-241"><a href="#remove_target_from_merge-241"><span class="linenos">241</span></a> <span class="k">return</span> <span class="n">expression</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="remove_target_from_merge-215"><a href="#remove_target_from_merge-215"><span class="linenos">215</span></a><span class="k">def</span> <span class="nf">remove_target_from_merge</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="remove_target_from_merge-216"><a href="#remove_target_from_merge-216"><span class="linenos">216</span></a><span class="w"> </span><span class="sd">"""Remove table refs from columns in when statements."""</span> +</span><span id="remove_target_from_merge-217"><a href="#remove_target_from_merge-217"><span class="linenos">217</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">Merge</span><span class="p">):</span> +</span><span id="remove_target_from_merge-218"><a href="#remove_target_from_merge-218"><span class="linenos">218</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> +</span><span id="remove_target_from_merge-219"><a href="#remove_target_from_merge-219"><span class="linenos">219</span></a> <span class="n">targets</span> <span class="o">=</span> <span class="p">{</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">}</span> +</span><span id="remove_target_from_merge-220"><a href="#remove_target_from_merge-220"><span class="linenos">220</span></a> <span class="k">if</span> <span class="n">alias</span><span class="p">:</span> +</span><span id="remove_target_from_merge-221"><a href="#remove_target_from_merge-221"><span class="linenos">221</span></a> <span class="n">targets</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">alias</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="remove_target_from_merge-222"><a href="#remove_target_from_merge-222"><span class="linenos">222</span></a> +</span><span id="remove_target_from_merge-223"><a href="#remove_target_from_merge-223"><span class="linenos">223</span></a> <span class="k">for</span> <span class="n">when</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="remove_target_from_merge-224"><a href="#remove_target_from_merge-224"><span class="linenos">224</span></a> <span class="n">when</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span> +</span><span id="remove_target_from_merge-225"><a href="#remove_target_from_merge-225"><span class="linenos">225</span></a> <span class="k">lambda</span> <span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="remove_target_from_merge-226"><a href="#remove_target_from_merge-226"><span class="linenos">226</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">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"table"</span><span class="p">)</span> <span class="ow">in</span> <span class="n">targets</span> +</span><span id="remove_target_from_merge-227"><a href="#remove_target_from_merge-227"><span class="linenos">227</span></a> <span class="k">else</span> <span class="n">node</span><span class="p">,</span> +</span><span id="remove_target_from_merge-228"><a href="#remove_target_from_merge-228"><span class="linenos">228</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="remove_target_from_merge-229"><a href="#remove_target_from_merge-229"><span class="linenos">229</span></a> <span class="p">)</span> +</span><span id="remove_target_from_merge-230"><a href="#remove_target_from_merge-230"><span class="linenos">230</span></a> <span class="k">return</span> <span class="n">expression</span> </span></pre></div> @@ -759,27 +729,27 @@ other expressions. This transforms removes the precision from parameterized type </div> <a class="headerlink" href="#preprocess"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="preprocess-244"><a href="#preprocess-244"><span class="linenos">244</span></a><span class="k">def</span> <span class="nf">preprocess</span><span class="p">(</span> -</span><span id="preprocess-245"><a href="#preprocess-245"><span class="linenos">245</span></a> <span class="n">transforms</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">List</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="n">exp</span><span class="o">.</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><span id="preprocess-246"><a href="#preprocess-246"><span class="linenos">246</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Generator</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="nb">str</span><span class="p">]:</span> -</span><span id="preprocess-247"><a href="#preprocess-247"><span class="linenos">247</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="preprocess-248"><a href="#preprocess-248"><span class="linenos">248</span></a><span class="sd"> Creates a new transform by chaining a sequence of transformations and converts the resulting</span> -</span><span id="preprocess-249"><a href="#preprocess-249"><span class="linenos">249</span></a><span class="sd"> expression to SQL, using an appropriate `Generator.TRANSFORMS` function.</span> -</span><span id="preprocess-250"><a href="#preprocess-250"><span class="linenos">250</span></a> -</span><span id="preprocess-251"><a href="#preprocess-251"><span class="linenos">251</span></a><span class="sd"> Args:</span> -</span><span id="preprocess-252"><a href="#preprocess-252"><span class="linenos">252</span></a><span class="sd"> transforms: sequence of transform functions. These will be called in order.</span> -</span><span id="preprocess-253"><a href="#preprocess-253"><span class="linenos">253</span></a> -</span><span id="preprocess-254"><a href="#preprocess-254"><span class="linenos">254</span></a><span class="sd"> Returns:</span> -</span><span id="preprocess-255"><a href="#preprocess-255"><span class="linenos">255</span></a><span class="sd"> Function that can be used as a generator transform.</span> -</span><span id="preprocess-256"><a href="#preprocess-256"><span class="linenos">256</span></a><span class="sd"> """</span> -</span><span id="preprocess-257"><a href="#preprocess-257"><span class="linenos">257</span></a> -</span><span id="preprocess-258"><a href="#preprocess-258"><span class="linenos">258</span></a> <span class="k">def</span> <span class="nf">_to_sql</span><span class="p">(</span><span class="bp">self</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">str</span><span class="p">:</span> -</span><span id="preprocess-259"><a href="#preprocess-259"><span class="linenos">259</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">0</span><span class="p">](</span><span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> -</span><span id="preprocess-260"><a href="#preprocess-260"><span class="linenos">260</span></a> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="preprocess-261"><a href="#preprocess-261"><span class="linenos">261</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">t</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="preprocess-262"><a href="#preprocess-262"><span class="linenos">262</span></a> <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">key</span> <span class="o">+</span> <span class="s2">"_sql"</span><span class="p">)(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="preprocess-263"><a href="#preprocess-263"><span class="linenos">263</span></a> -</span><span id="preprocess-264"><a href="#preprocess-264"><span class="linenos">264</span></a> <span class="k">return</span> <span class="n">_to_sql</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="preprocess-233"><a href="#preprocess-233"><span class="linenos">233</span></a><span class="k">def</span> <span class="nf">preprocess</span><span class="p">(</span> +</span><span id="preprocess-234"><a href="#preprocess-234"><span class="linenos">234</span></a> <span class="n">transforms</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">List</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="n">exp</span><span class="o">.</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><span id="preprocess-235"><a href="#preprocess-235"><span class="linenos">235</span></a><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Generator</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="nb">str</span><span class="p">]:</span> +</span><span id="preprocess-236"><a href="#preprocess-236"><span class="linenos">236</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="preprocess-237"><a href="#preprocess-237"><span class="linenos">237</span></a><span class="sd"> Creates a new transform by chaining a sequence of transformations and converts the resulting</span> +</span><span id="preprocess-238"><a href="#preprocess-238"><span class="linenos">238</span></a><span class="sd"> expression to SQL, using an appropriate `Generator.TRANSFORMS` function.</span> +</span><span id="preprocess-239"><a href="#preprocess-239"><span class="linenos">239</span></a> +</span><span id="preprocess-240"><a href="#preprocess-240"><span class="linenos">240</span></a><span class="sd"> Args:</span> +</span><span id="preprocess-241"><a href="#preprocess-241"><span class="linenos">241</span></a><span class="sd"> transforms: sequence of transform functions. These will be called in order.</span> +</span><span id="preprocess-242"><a href="#preprocess-242"><span class="linenos">242</span></a> +</span><span id="preprocess-243"><a href="#preprocess-243"><span class="linenos">243</span></a><span class="sd"> Returns:</span> +</span><span id="preprocess-244"><a href="#preprocess-244"><span class="linenos">244</span></a><span class="sd"> Function that can be used as a generator transform.</span> +</span><span id="preprocess-245"><a href="#preprocess-245"><span class="linenos">245</span></a><span class="sd"> """</span> +</span><span id="preprocess-246"><a href="#preprocess-246"><span class="linenos">246</span></a> +</span><span id="preprocess-247"><a href="#preprocess-247"><span class="linenos">247</span></a> <span class="k">def</span> <span class="nf">_to_sql</span><span class="p">(</span><span class="bp">self</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">str</span><span class="p">:</span> +</span><span id="preprocess-248"><a href="#preprocess-248"><span class="linenos">248</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">0</span><span class="p">](</span><span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="preprocess-249"><a href="#preprocess-249"><span class="linenos">249</span></a> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">transforms</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="preprocess-250"><a href="#preprocess-250"><span class="linenos">250</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">t</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="preprocess-251"><a href="#preprocess-251"><span class="linenos">251</span></a> <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">key</span> <span class="o">+</span> <span class="s2">"_sql"</span><span class="p">)(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="preprocess-252"><a href="#preprocess-252"><span class="linenos">252</span></a> +</span><span id="preprocess-253"><a href="#preprocess-253"><span class="linenos">253</span></a> <span class="k">return</span> <span class="n">_to_sql</span> </span></pre></div> |