diff options
Diffstat (limited to 'docs/sqlglot/dialects/redshift.html')
-rw-r--r-- | docs/sqlglot/dialects/redshift.html | 799 |
1 files changed, 417 insertions, 382 deletions
diff --git a/docs/sqlglot/dialects/redshift.html b/docs/sqlglot/dialects/redshift.html index 7e69e65..7ec5882 100644 --- a/docs/sqlglot/dialects/redshift.html +++ b/docs/sqlglot/dialects/redshift.html @@ -111,124 +111,135 @@ </span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a> <span class="k">class</span> <span class="nc">Parser</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="p">):</span> </span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a> <span class="n">FUNCTIONS</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="o">.</span><span class="n">FUNCTIONS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> <span class="s2">"DATEADD"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">(</span> </span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> </span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> </span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> </span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> <span class="p">),</span> -</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="p">}</span> -</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> -</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> -</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> -</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> -</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="p">),</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="p">}</span> </span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> -</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="k">if</span> <span class="p">(</span> -</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> -</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> -</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> -</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="p">):</span> -</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> -</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> -</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="k">return</span> <span class="n">this</span> -</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> -</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> -</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="p">):</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> </span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> -</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> -</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> -</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> -</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> -</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> -</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> -</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> -</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> -</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> -</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="p">}</span> -</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> -</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> -</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> -</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="p">}</span> -</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> -</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="k">return</span> <span class="n">this</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="p">}</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> </span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="p">}</span> </span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> -</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> -</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">e</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">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="p">),</span> -</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> -</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> -</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="p">}</span> -</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> -</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> -</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> -</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> -</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> -</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a><span class="sd"> very slow.</span> -</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a><span class="sd"> """</span> -</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> -</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> -</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> -</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> -</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> -</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> -</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="p">]</span> -</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> -</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> -</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> -</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> -</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> -</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> -</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> -</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> -</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> -</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> -</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> -</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> -</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> -</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> -</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> -</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> -</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> -</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> -</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> -</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> -</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a><span class="sd"> """</span> -</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> -</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> -</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> -</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</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">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> -</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="p">}</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="s2">"DATEADD"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="p">),</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="p">),</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="p">}</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="c1"># Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres)</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">TRANSFORMS</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Pow</span><span class="p">)</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a><span class="sd"> very slow.</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a><span class="sd"> """</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="p">]</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</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 class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a><span class="sd"> """</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> +</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">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> </span></pre></div> @@ -255,124 +266,135 @@ </span><span id="Redshift-21"><a href="#Redshift-21"><span class="linenos"> 21</span></a> <span class="k">class</span> <span class="nc">Parser</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="p">):</span> </span><span id="Redshift-22"><a href="#Redshift-22"><span class="linenos"> 22</span></a> <span class="n">FUNCTIONS</span> <span class="o">=</span> <span class="p">{</span> </span><span id="Redshift-23"><a href="#Redshift-23"><span class="linenos"> 23</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="o">.</span><span class="n">FUNCTIONS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-24"><a href="#Redshift-24"><span class="linenos"> 24</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="Redshift-24"><a href="#Redshift-24"><span class="linenos"> 24</span></a> <span class="s2">"DATEADD"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">(</span> </span><span id="Redshift-25"><a href="#Redshift-25"><span class="linenos"> 25</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> </span><span id="Redshift-26"><a href="#Redshift-26"><span class="linenos"> 26</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> </span><span id="Redshift-27"><a href="#Redshift-27"><span class="linenos"> 27</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> </span><span id="Redshift-28"><a href="#Redshift-28"><span class="linenos"> 28</span></a> <span class="p">),</span> -</span><span id="Redshift-29"><a href="#Redshift-29"><span class="linenos"> 29</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="Redshift-30"><a href="#Redshift-30"><span class="linenos"> 30</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="Redshift-31"><a href="#Redshift-31"><span class="linenos"> 31</span></a> <span class="p">}</span> -</span><span id="Redshift-32"><a href="#Redshift-32"><span class="linenos"> 32</span></a> -</span><span id="Redshift-33"><a href="#Redshift-33"><span class="linenos"> 33</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> -</span><span id="Redshift-34"><a href="#Redshift-34"><span class="linenos"> 34</span></a> -</span><span id="Redshift-35"><a href="#Redshift-35"><span class="linenos"> 35</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> -</span><span id="Redshift-36"><a href="#Redshift-36"><span class="linenos"> 36</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="Redshift-29"><a href="#Redshift-29"><span class="linenos"> 29</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="Redshift-30"><a href="#Redshift-30"><span class="linenos"> 30</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> +</span><span id="Redshift-31"><a href="#Redshift-31"><span class="linenos"> 31</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> +</span><span id="Redshift-32"><a href="#Redshift-32"><span class="linenos"> 32</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> +</span><span id="Redshift-33"><a href="#Redshift-33"><span class="linenos"> 33</span></a> <span class="p">),</span> +</span><span id="Redshift-34"><a href="#Redshift-34"><span class="linenos"> 34</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="Redshift-35"><a href="#Redshift-35"><span class="linenos"> 35</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="Redshift-36"><a href="#Redshift-36"><span class="linenos"> 36</span></a> <span class="p">}</span> </span><span id="Redshift-37"><a href="#Redshift-37"><span class="linenos"> 37</span></a> -</span><span id="Redshift-38"><a href="#Redshift-38"><span class="linenos"> 38</span></a> <span class="k">if</span> <span class="p">(</span> -</span><span id="Redshift-39"><a href="#Redshift-39"><span class="linenos"> 39</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> -</span><span id="Redshift-40"><a href="#Redshift-40"><span class="linenos"> 40</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> -</span><span id="Redshift-41"><a href="#Redshift-41"><span class="linenos"> 41</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="Redshift-42"><a href="#Redshift-42"><span class="linenos"> 42</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> -</span><span id="Redshift-43"><a href="#Redshift-43"><span class="linenos"> 43</span></a> <span class="p">):</span> -</span><span id="Redshift-44"><a href="#Redshift-44"><span class="linenos"> 44</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> -</span><span id="Redshift-45"><a href="#Redshift-45"><span class="linenos"> 45</span></a> -</span><span id="Redshift-46"><a href="#Redshift-46"><span class="linenos"> 46</span></a> <span class="k">return</span> <span class="n">this</span> -</span><span id="Redshift-47"><a href="#Redshift-47"><span class="linenos"> 47</span></a> -</span><span id="Redshift-48"><a href="#Redshift-48"><span class="linenos"> 48</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> -</span><span id="Redshift-49"><a href="#Redshift-49"><span class="linenos"> 49</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> +</span><span id="Redshift-38"><a href="#Redshift-38"><span class="linenos"> 38</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="Redshift-39"><a href="#Redshift-39"><span class="linenos"> 39</span></a> +</span><span id="Redshift-40"><a href="#Redshift-40"><span class="linenos"> 40</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> +</span><span id="Redshift-41"><a href="#Redshift-41"><span class="linenos"> 41</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="Redshift-42"><a href="#Redshift-42"><span class="linenos"> 42</span></a> +</span><span id="Redshift-43"><a href="#Redshift-43"><span class="linenos"> 43</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="Redshift-44"><a href="#Redshift-44"><span class="linenos"> 44</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> +</span><span id="Redshift-45"><a href="#Redshift-45"><span class="linenos"> 45</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> +</span><span id="Redshift-46"><a href="#Redshift-46"><span class="linenos"> 46</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="Redshift-47"><a href="#Redshift-47"><span class="linenos"> 47</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> +</span><span id="Redshift-48"><a href="#Redshift-48"><span class="linenos"> 48</span></a> <span class="p">):</span> +</span><span id="Redshift-49"><a href="#Redshift-49"><span class="linenos"> 49</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> </span><span id="Redshift-50"><a href="#Redshift-50"><span class="linenos"> 50</span></a> -</span><span id="Redshift-51"><a href="#Redshift-51"><span class="linenos"> 51</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift-52"><a href="#Redshift-52"><span class="linenos"> 52</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-53"><a href="#Redshift-53"><span class="linenos"> 53</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> -</span><span id="Redshift-54"><a href="#Redshift-54"><span class="linenos"> 54</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> -</span><span id="Redshift-55"><a href="#Redshift-55"><span class="linenos"> 55</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> -</span><span id="Redshift-56"><a href="#Redshift-56"><span class="linenos"> 56</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> -</span><span id="Redshift-57"><a href="#Redshift-57"><span class="linenos"> 57</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> -</span><span id="Redshift-58"><a href="#Redshift-58"><span class="linenos"> 58</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> -</span><span id="Redshift-59"><a href="#Redshift-59"><span class="linenos"> 59</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> -</span><span id="Redshift-60"><a href="#Redshift-60"><span class="linenos"> 60</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> -</span><span id="Redshift-61"><a href="#Redshift-61"><span class="linenos"> 61</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> -</span><span id="Redshift-62"><a href="#Redshift-62"><span class="linenos"> 62</span></a> <span class="p">}</span> -</span><span id="Redshift-63"><a href="#Redshift-63"><span class="linenos"> 63</span></a> -</span><span id="Redshift-64"><a href="#Redshift-64"><span class="linenos"> 64</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> -</span><span id="Redshift-65"><a href="#Redshift-65"><span class="linenos"> 65</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift-66"><a href="#Redshift-66"><span class="linenos"> 66</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-67"><a href="#Redshift-67"><span class="linenos"> 67</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="Redshift-68"><a href="#Redshift-68"><span class="linenos"> 68</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="Redshift-69"><a href="#Redshift-69"><span class="linenos"> 69</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> -</span><span id="Redshift-70"><a href="#Redshift-70"><span class="linenos"> 70</span></a> <span class="p">}</span> -</span><span id="Redshift-71"><a href="#Redshift-71"><span class="linenos"> 71</span></a> -</span><span id="Redshift-72"><a href="#Redshift-72"><span class="linenos"> 72</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift-73"><a href="#Redshift-73"><span class="linenos"> 73</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-74"><a href="#Redshift-74"><span class="linenos"> 74</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> +</span><span id="Redshift-51"><a href="#Redshift-51"><span class="linenos"> 51</span></a> <span class="k">return</span> <span class="n">this</span> +</span><span id="Redshift-52"><a href="#Redshift-52"><span class="linenos"> 52</span></a> +</span><span id="Redshift-53"><a href="#Redshift-53"><span class="linenos"> 53</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> +</span><span id="Redshift-54"><a href="#Redshift-54"><span class="linenos"> 54</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> +</span><span id="Redshift-55"><a href="#Redshift-55"><span class="linenos"> 55</span></a> +</span><span id="Redshift-56"><a href="#Redshift-56"><span class="linenos"> 56</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift-57"><a href="#Redshift-57"><span class="linenos"> 57</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift-58"><a href="#Redshift-58"><span class="linenos"> 58</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> +</span><span id="Redshift-59"><a href="#Redshift-59"><span class="linenos"> 59</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> +</span><span id="Redshift-60"><a href="#Redshift-60"><span class="linenos"> 60</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> +</span><span id="Redshift-61"><a href="#Redshift-61"><span class="linenos"> 61</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> +</span><span id="Redshift-62"><a href="#Redshift-62"><span class="linenos"> 62</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> +</span><span id="Redshift-63"><a href="#Redshift-63"><span class="linenos"> 63</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="Redshift-64"><a href="#Redshift-64"><span class="linenos"> 64</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> +</span><span id="Redshift-65"><a href="#Redshift-65"><span class="linenos"> 65</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> +</span><span id="Redshift-66"><a href="#Redshift-66"><span class="linenos"> 66</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> +</span><span id="Redshift-67"><a href="#Redshift-67"><span class="linenos"> 67</span></a> <span class="p">}</span> +</span><span id="Redshift-68"><a href="#Redshift-68"><span class="linenos"> 68</span></a> +</span><span id="Redshift-69"><a href="#Redshift-69"><span class="linenos"> 69</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> +</span><span id="Redshift-70"><a href="#Redshift-70"><span class="linenos"> 70</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift-71"><a href="#Redshift-71"><span class="linenos"> 71</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift-72"><a href="#Redshift-72"><span class="linenos"> 72</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="Redshift-73"><a href="#Redshift-73"><span class="linenos"> 73</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="Redshift-74"><a href="#Redshift-74"><span class="linenos"> 74</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> </span><span id="Redshift-75"><a href="#Redshift-75"><span class="linenos"> 75</span></a> <span class="p">}</span> </span><span id="Redshift-76"><a href="#Redshift-76"><span class="linenos"> 76</span></a> -</span><span id="Redshift-77"><a href="#Redshift-77"><span class="linenos"> 77</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift-78"><a href="#Redshift-78"><span class="linenos"> 78</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-79"><a href="#Redshift-79"><span class="linenos"> 79</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift-80"><a href="#Redshift-80"><span class="linenos"> 80</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> -</span><span id="Redshift-81"><a href="#Redshift-81"><span class="linenos"> 81</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">e</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">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> -</span><span id="Redshift-82"><a href="#Redshift-82"><span class="linenos"> 82</span></a> <span class="p">),</span> -</span><span id="Redshift-83"><a href="#Redshift-83"><span class="linenos"> 83</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="Redshift-84"><a href="#Redshift-84"><span class="linenos"> 84</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="Redshift-85"><a href="#Redshift-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> -</span><span id="Redshift-86"><a href="#Redshift-86"><span class="linenos"> 86</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> -</span><span id="Redshift-87"><a href="#Redshift-87"><span class="linenos"> 87</span></a> <span class="p">}</span> -</span><span id="Redshift-88"><a href="#Redshift-88"><span class="linenos"> 88</span></a> -</span><span id="Redshift-89"><a href="#Redshift-89"><span class="linenos"> 89</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift-90"><a href="#Redshift-90"><span class="linenos"> 90</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift-91"><a href="#Redshift-91"><span class="linenos"> 91</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> -</span><span id="Redshift-92"><a href="#Redshift-92"><span class="linenos"> 92</span></a> -</span><span id="Redshift-93"><a href="#Redshift-93"><span class="linenos"> 93</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> -</span><span id="Redshift-94"><a href="#Redshift-94"><span class="linenos"> 94</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> -</span><span id="Redshift-95"><a href="#Redshift-95"><span class="linenos"> 95</span></a><span class="sd"> very slow.</span> -</span><span id="Redshift-96"><a href="#Redshift-96"><span class="linenos"> 96</span></a><span class="sd"> """</span> -</span><span id="Redshift-97"><a href="#Redshift-97"><span class="linenos"> 97</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> -</span><span id="Redshift-98"><a href="#Redshift-98"><span class="linenos"> 98</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="Redshift-99"><a href="#Redshift-99"><span class="linenos"> 99</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift-100"><a href="#Redshift-100"><span class="linenos">100</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="Redshift-101"><a href="#Redshift-101"><span class="linenos">101</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> -</span><span id="Redshift-102"><a href="#Redshift-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> -</span><span id="Redshift-103"><a href="#Redshift-103"><span class="linenos">103</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> -</span><span id="Redshift-104"><a href="#Redshift-104"><span class="linenos">104</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> -</span><span id="Redshift-105"><a href="#Redshift-105"><span class="linenos">105</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> -</span><span id="Redshift-106"><a href="#Redshift-106"><span class="linenos">106</span></a> <span class="p">]</span> -</span><span id="Redshift-107"><a href="#Redshift-107"><span class="linenos">107</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> -</span><span id="Redshift-108"><a href="#Redshift-108"><span class="linenos">108</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> -</span><span id="Redshift-109"><a href="#Redshift-109"><span class="linenos">109</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> -</span><span id="Redshift-110"><a href="#Redshift-110"><span class="linenos">110</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="Redshift-111"><a href="#Redshift-111"><span class="linenos">111</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> -</span><span id="Redshift-112"><a href="#Redshift-112"><span class="linenos">112</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> -</span><span id="Redshift-113"><a href="#Redshift-113"><span class="linenos">113</span></a> -</span><span id="Redshift-114"><a href="#Redshift-114"><span class="linenos">114</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift-115"><a href="#Redshift-115"><span class="linenos">115</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> -</span><span id="Redshift-116"><a href="#Redshift-116"><span class="linenos">116</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> -</span><span id="Redshift-117"><a href="#Redshift-117"><span class="linenos">117</span></a> -</span><span id="Redshift-118"><a href="#Redshift-118"><span class="linenos">118</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift-119"><a href="#Redshift-119"><span class="linenos">119</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> -</span><span id="Redshift-120"><a href="#Redshift-120"><span class="linenos">120</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift-121"><a href="#Redshift-121"><span class="linenos">121</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="Redshift-122"><a href="#Redshift-122"><span class="linenos">122</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> -</span><span id="Redshift-123"><a href="#Redshift-123"><span class="linenos">123</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> -</span><span id="Redshift-124"><a href="#Redshift-124"><span class="linenos">124</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> -</span><span id="Redshift-125"><a href="#Redshift-125"><span class="linenos">125</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> -</span><span id="Redshift-126"><a href="#Redshift-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> -</span><span id="Redshift-127"><a href="#Redshift-127"><span class="linenos">127</span></a> -</span><span id="Redshift-128"><a href="#Redshift-128"><span class="linenos">128</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift-129"><a href="#Redshift-129"><span class="linenos">129</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift-130"><a href="#Redshift-130"><span class="linenos">130</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> -</span><span id="Redshift-131"><a href="#Redshift-131"><span class="linenos">131</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> -</span><span id="Redshift-132"><a href="#Redshift-132"><span class="linenos">132</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> -</span><span id="Redshift-133"><a href="#Redshift-133"><span class="linenos">133</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> -</span><span id="Redshift-134"><a href="#Redshift-134"><span class="linenos">134</span></a><span class="sd"> """</span> -</span><span id="Redshift-135"><a href="#Redshift-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> -</span><span id="Redshift-136"><a href="#Redshift-136"><span class="linenos">136</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift-137"><a href="#Redshift-137"><span class="linenos">137</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> -</span><span id="Redshift-138"><a href="#Redshift-138"><span class="linenos">138</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> -</span><span id="Redshift-139"><a href="#Redshift-139"><span class="linenos">139</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> -</span><span id="Redshift-140"><a href="#Redshift-140"><span class="linenos">140</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> -</span><span id="Redshift-141"><a href="#Redshift-141"><span class="linenos">141</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="Redshift-77"><a href="#Redshift-77"><span class="linenos"> 77</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift-78"><a href="#Redshift-78"><span class="linenos"> 78</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift-79"><a href="#Redshift-79"><span class="linenos"> 79</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> +</span><span id="Redshift-80"><a href="#Redshift-80"><span class="linenos"> 80</span></a> <span class="p">}</span> +</span><span id="Redshift-81"><a href="#Redshift-81"><span class="linenos"> 81</span></a> +</span><span id="Redshift-82"><a href="#Redshift-82"><span class="linenos"> 82</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift-83"><a href="#Redshift-83"><span class="linenos"> 83</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift-84"><a href="#Redshift-84"><span class="linenos"> 84</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift-85"><a href="#Redshift-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="Redshift-86"><a href="#Redshift-86"><span class="linenos"> 86</span></a> <span class="s2">"DATEADD"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift-87"><a href="#Redshift-87"><span class="linenos"> 87</span></a> <span class="p">),</span> +</span><span id="Redshift-88"><a href="#Redshift-88"><span class="linenos"> 88</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="Redshift-89"><a href="#Redshift-89"><span class="linenos"> 89</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift-90"><a href="#Redshift-90"><span class="linenos"> 90</span></a> <span class="p">),</span> +</span><span id="Redshift-91"><a href="#Redshift-91"><span class="linenos"> 91</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="Redshift-92"><a href="#Redshift-92"><span class="linenos"> 92</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> +</span><span id="Redshift-93"><a href="#Redshift-93"><span class="linenos"> 93</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> +</span><span id="Redshift-94"><a href="#Redshift-94"><span class="linenos"> 94</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="Redshift-95"><a href="#Redshift-95"><span class="linenos"> 95</span></a> <span class="p">}</span> +</span><span id="Redshift-96"><a href="#Redshift-96"><span class="linenos"> 96</span></a> +</span><span id="Redshift-97"><a href="#Redshift-97"><span class="linenos"> 97</span></a> <span class="c1"># Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres)</span> +</span><span id="Redshift-98"><a href="#Redshift-98"><span class="linenos"> 98</span></a> <span class="n">TRANSFORMS</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Pow</span><span class="p">)</span> +</span><span id="Redshift-99"><a href="#Redshift-99"><span class="linenos"> 99</span></a> +</span><span id="Redshift-100"><a href="#Redshift-100"><span class="linenos">100</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift-101"><a href="#Redshift-101"><span class="linenos">101</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift-102"><a href="#Redshift-102"><span class="linenos">102</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> +</span><span id="Redshift-103"><a href="#Redshift-103"><span class="linenos">103</span></a> +</span><span id="Redshift-104"><a href="#Redshift-104"><span class="linenos">104</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> +</span><span id="Redshift-105"><a href="#Redshift-105"><span class="linenos">105</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> +</span><span id="Redshift-106"><a href="#Redshift-106"><span class="linenos">106</span></a><span class="sd"> very slow.</span> +</span><span id="Redshift-107"><a href="#Redshift-107"><span class="linenos">107</span></a><span class="sd"> """</span> +</span><span id="Redshift-108"><a href="#Redshift-108"><span class="linenos">108</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="Redshift-109"><a href="#Redshift-109"><span class="linenos">109</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="Redshift-110"><a href="#Redshift-110"><span class="linenos">110</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift-111"><a href="#Redshift-111"><span class="linenos">111</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Redshift-112"><a href="#Redshift-112"><span class="linenos">112</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> +</span><span id="Redshift-113"><a href="#Redshift-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="Redshift-114"><a href="#Redshift-114"><span class="linenos">114</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="Redshift-115"><a href="#Redshift-115"><span class="linenos">115</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> +</span><span id="Redshift-116"><a href="#Redshift-116"><span class="linenos">116</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> +</span><span id="Redshift-117"><a href="#Redshift-117"><span class="linenos">117</span></a> <span class="p">]</span> +</span><span id="Redshift-118"><a href="#Redshift-118"><span class="linenos">118</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> +</span><span id="Redshift-119"><a href="#Redshift-119"><span class="linenos">119</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="Redshift-120"><a href="#Redshift-120"><span class="linenos">120</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> +</span><span id="Redshift-121"><a href="#Redshift-121"><span class="linenos">121</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="Redshift-122"><a href="#Redshift-122"><span class="linenos">122</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="Redshift-123"><a href="#Redshift-123"><span class="linenos">123</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> +</span><span id="Redshift-124"><a href="#Redshift-124"><span class="linenos">124</span></a> +</span><span id="Redshift-125"><a href="#Redshift-125"><span class="linenos">125</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift-126"><a href="#Redshift-126"><span class="linenos">126</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> +</span><span id="Redshift-127"><a href="#Redshift-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> +</span><span id="Redshift-128"><a href="#Redshift-128"><span class="linenos">128</span></a> +</span><span id="Redshift-129"><a href="#Redshift-129"><span class="linenos">129</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift-130"><a href="#Redshift-130"><span class="linenos">130</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> +</span><span id="Redshift-131"><a href="#Redshift-131"><span class="linenos">131</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift-132"><a href="#Redshift-132"><span class="linenos">132</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift-133"><a href="#Redshift-133"><span class="linenos">133</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> +</span><span id="Redshift-134"><a href="#Redshift-134"><span class="linenos">134</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> +</span><span id="Redshift-135"><a href="#Redshift-135"><span class="linenos">135</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Redshift-136"><a href="#Redshift-136"><span class="linenos">136</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> +</span><span id="Redshift-137"><a href="#Redshift-137"><span class="linenos">137</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> +</span><span id="Redshift-138"><a href="#Redshift-138"><span class="linenos">138</span></a> +</span><span id="Redshift-139"><a href="#Redshift-139"><span class="linenos">139</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift-140"><a href="#Redshift-140"><span class="linenos">140</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift-141"><a href="#Redshift-141"><span class="linenos">141</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> +</span><span id="Redshift-142"><a href="#Redshift-142"><span class="linenos">142</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> +</span><span id="Redshift-143"><a href="#Redshift-143"><span class="linenos">143</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> +</span><span id="Redshift-144"><a href="#Redshift-144"><span class="linenos">144</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> +</span><span id="Redshift-145"><a href="#Redshift-145"><span class="linenos">145</span></a><span class="sd"> """</span> +</span><span id="Redshift-146"><a href="#Redshift-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> +</span><span id="Redshift-147"><a href="#Redshift-147"><span class="linenos">147</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift-148"><a href="#Redshift-148"><span class="linenos">148</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> +</span><span id="Redshift-149"><a href="#Redshift-149"><span class="linenos">149</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> +</span><span id="Redshift-150"><a href="#Redshift-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> +</span><span id="Redshift-151"><a href="#Redshift-151"><span class="linenos">151</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> +</span><span id="Redshift-152"><a href="#Redshift-152"><span class="linenos">152</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> </span></pre></div> @@ -388,6 +410,7 @@ <dd id="Redshift.parse_into" class="function"><a href="dialect.html#Dialect.parse_into">parse_into</a></dd> <dd id="Redshift.generate" class="function"><a href="dialect.html#Dialect.generate">generate</a></dd> <dd id="Redshift.transpile" class="function"><a href="dialect.html#Dialect.transpile">transpile</a></dd> + <dd id="Redshift.tokenize" class="function"><a href="dialect.html#Dialect.tokenize">tokenize</a></dd> <dd id="Redshift.parser" class="function"><a href="dialect.html#Dialect.parser">parser</a></dd> <dd id="Redshift.generator" class="function"><a href="dialect.html#Dialect.generator">generator</a></dd> @@ -409,29 +432,34 @@ <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Parser-21"><a href="#Redshift.Parser-21"><span class="linenos">21</span></a> <span class="k">class</span> <span class="nc">Parser</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="p">):</span> </span><span id="Redshift.Parser-22"><a href="#Redshift.Parser-22"><span class="linenos">22</span></a> <span class="n">FUNCTIONS</span> <span class="o">=</span> <span class="p">{</span> </span><span id="Redshift.Parser-23"><a href="#Redshift.Parser-23"><span class="linenos">23</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Parser</span><span class="o">.</span><span class="n">FUNCTIONS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Parser-24"><a href="#Redshift.Parser-24"><span class="linenos">24</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="Redshift.Parser-24"><a href="#Redshift.Parser-24"><span class="linenos">24</span></a> <span class="s2">"DATEADD"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">(</span> </span><span id="Redshift.Parser-25"><a href="#Redshift.Parser-25"><span class="linenos">25</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> </span><span id="Redshift.Parser-26"><a href="#Redshift.Parser-26"><span class="linenos">26</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> </span><span id="Redshift.Parser-27"><a href="#Redshift.Parser-27"><span class="linenos">27</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> </span><span id="Redshift.Parser-28"><a href="#Redshift.Parser-28"><span class="linenos">28</span></a> <span class="p">),</span> -</span><span id="Redshift.Parser-29"><a href="#Redshift.Parser-29"><span class="linenos">29</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="Redshift.Parser-30"><a href="#Redshift.Parser-30"><span class="linenos">30</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> -</span><span id="Redshift.Parser-31"><a href="#Redshift.Parser-31"><span class="linenos">31</span></a> <span class="p">}</span> -</span><span id="Redshift.Parser-32"><a href="#Redshift.Parser-32"><span class="linenos">32</span></a> -</span><span id="Redshift.Parser-33"><a href="#Redshift.Parser-33"><span class="linenos">33</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> -</span><span id="Redshift.Parser-34"><a href="#Redshift.Parser-34"><span class="linenos">34</span></a> -</span><span id="Redshift.Parser-35"><a href="#Redshift.Parser-35"><span class="linenos">35</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> -</span><span id="Redshift.Parser-36"><a href="#Redshift.Parser-36"><span class="linenos">36</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="Redshift.Parser-29"><a href="#Redshift.Parser-29"><span class="linenos">29</span></a> <span class="s2">"DATEDIFF"</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">args</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">(</span> +</span><span id="Redshift.Parser-30"><a href="#Redshift.Parser-30"><span class="linenos">30</span></a> <span class="n">this</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> +</span><span id="Redshift.Parser-31"><a href="#Redshift.Parser-31"><span class="linenos">31</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> +</span><span id="Redshift.Parser-32"><a href="#Redshift.Parser-32"><span class="linenos">32</span></a> <span class="n">unit</span><span class="o">=</span><span class="n">seq_get</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> +</span><span id="Redshift.Parser-33"><a href="#Redshift.Parser-33"><span class="linenos">33</span></a> <span class="p">),</span> +</span><span id="Redshift.Parser-34"><a href="#Redshift.Parser-34"><span class="linenos">34</span></a> <span class="s2">"DECODE"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="Redshift.Parser-35"><a href="#Redshift.Parser-35"><span class="linenos">35</span></a> <span class="s2">"NVL"</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="o">.</span><span class="n">from_arg_list</span><span class="p">,</span> +</span><span id="Redshift.Parser-36"><a href="#Redshift.Parser-36"><span class="linenos">36</span></a> <span class="p">}</span> </span><span id="Redshift.Parser-37"><a href="#Redshift.Parser-37"><span class="linenos">37</span></a> -</span><span id="Redshift.Parser-38"><a href="#Redshift.Parser-38"><span class="linenos">38</span></a> <span class="k">if</span> <span class="p">(</span> -</span><span id="Redshift.Parser-39"><a href="#Redshift.Parser-39"><span class="linenos">39</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> -</span><span id="Redshift.Parser-40"><a href="#Redshift.Parser-40"><span class="linenos">40</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> -</span><span id="Redshift.Parser-41"><a href="#Redshift.Parser-41"><span class="linenos">41</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> -</span><span id="Redshift.Parser-42"><a href="#Redshift.Parser-42"><span class="linenos">42</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> -</span><span id="Redshift.Parser-43"><a href="#Redshift.Parser-43"><span class="linenos">43</span></a> <span class="p">):</span> -</span><span id="Redshift.Parser-44"><a href="#Redshift.Parser-44"><span class="linenos">44</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> -</span><span id="Redshift.Parser-45"><a href="#Redshift.Parser-45"><span class="linenos">45</span></a> -</span><span id="Redshift.Parser-46"><a href="#Redshift.Parser-46"><span class="linenos">46</span></a> <span class="k">return</span> <span class="n">this</span> +</span><span id="Redshift.Parser-38"><a href="#Redshift.Parser-38"><span class="linenos">38</span></a> <span class="n">CONVERT_TYPE_FIRST</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="Redshift.Parser-39"><a href="#Redshift.Parser-39"><span class="linenos">39</span></a> +</span><span id="Redshift.Parser-40"><a href="#Redshift.Parser-40"><span class="linenos">40</span></a> <span class="k">def</span> <span class="nf">_parse_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_func</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]:</span> +</span><span id="Redshift.Parser-41"><a href="#Redshift.Parser-41"><span class="linenos">41</span></a> <span class="n">this</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_parse_types</span><span class="p">(</span><span class="n">check_func</span><span class="o">=</span><span class="n">check_func</span><span class="p">)</span> +</span><span id="Redshift.Parser-42"><a href="#Redshift.Parser-42"><span class="linenos">42</span></a> +</span><span id="Redshift.Parser-43"><a href="#Redshift.Parser-43"><span class="linenos">43</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="Redshift.Parser-44"><a href="#Redshift.Parser-44"><span class="linenos">44</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">)</span> +</span><span id="Redshift.Parser-45"><a href="#Redshift.Parser-45"><span class="linenos">45</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span> +</span><span id="Redshift.Parser-46"><a href="#Redshift.Parser-46"><span class="linenos">46</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="Redshift.Parser-47"><a href="#Redshift.Parser-47"><span class="linenos">47</span></a> <span class="ow">and</span> <span class="n">this</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">"MAX"</span><span class="p">)</span> +</span><span id="Redshift.Parser-48"><a href="#Redshift.Parser-48"><span class="linenos">48</span></a> <span class="p">):</span> +</span><span id="Redshift.Parser-49"><a href="#Redshift.Parser-49"><span class="linenos">49</span></a> <span class="n">this</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">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">)])</span> +</span><span id="Redshift.Parser-50"><a href="#Redshift.Parser-50"><span class="linenos">50</span></a> +</span><span id="Redshift.Parser-51"><a href="#Redshift.Parser-51"><span class="linenos">51</span></a> <span class="k">return</span> <span class="n">this</span> </span></pre></div> @@ -488,21 +516,21 @@ Default: "nulls_are_small"</li> </div> <a class="headerlink" href="#Redshift.Tokenizer"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Tokenizer-48"><a href="#Redshift.Tokenizer-48"><span class="linenos">48</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> -</span><span id="Redshift.Tokenizer-49"><a href="#Redshift.Tokenizer-49"><span class="linenos">49</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> -</span><span id="Redshift.Tokenizer-50"><a href="#Redshift.Tokenizer-50"><span class="linenos">50</span></a> -</span><span id="Redshift.Tokenizer-51"><a href="#Redshift.Tokenizer-51"><span class="linenos">51</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift.Tokenizer-52"><a href="#Redshift.Tokenizer-52"><span class="linenos">52</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Tokenizer-53"><a href="#Redshift.Tokenizer-53"><span class="linenos">53</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-54"><a href="#Redshift.Tokenizer-54"><span class="linenos">54</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-55"><a href="#Redshift.Tokenizer-55"><span class="linenos">55</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-56"><a href="#Redshift.Tokenizer-56"><span class="linenos">56</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-57"><a href="#Redshift.Tokenizer-57"><span class="linenos">57</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-58"><a href="#Redshift.Tokenizer-58"><span class="linenos">58</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-59"><a href="#Redshift.Tokenizer-59"><span class="linenos">59</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-60"><a href="#Redshift.Tokenizer-60"><span class="linenos">60</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-61"><a href="#Redshift.Tokenizer-61"><span class="linenos">61</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> -</span><span id="Redshift.Tokenizer-62"><a href="#Redshift.Tokenizer-62"><span class="linenos">62</span></a> <span class="p">}</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Tokenizer-53"><a href="#Redshift.Tokenizer-53"><span class="linenos">53</span></a> <span class="k">class</span> <span class="nc">Tokenizer</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="p">):</span> +</span><span id="Redshift.Tokenizer-54"><a href="#Redshift.Tokenizer-54"><span class="linenos">54</span></a> <span class="n">STRING_ESCAPES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"</span><span class="se">\\</span><span class="s2">"</span><span class="p">]</span> +</span><span id="Redshift.Tokenizer-55"><a href="#Redshift.Tokenizer-55"><span class="linenos">55</span></a> +</span><span id="Redshift.Tokenizer-56"><a href="#Redshift.Tokenizer-56"><span class="linenos">56</span></a> <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift.Tokenizer-57"><a href="#Redshift.Tokenizer-57"><span class="linenos">57</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Tokenizer</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift.Tokenizer-58"><a href="#Redshift.Tokenizer-58"><span class="linenos">58</span></a> <span class="s2">"GEOMETRY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOMETRY</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-59"><a href="#Redshift.Tokenizer-59"><span class="linenos">59</span></a> <span class="s2">"GEOGRAPHY"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">GEOGRAPHY</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-60"><a href="#Redshift.Tokenizer-60"><span class="linenos">60</span></a> <span class="s2">"HLLSKETCH"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">HLLSKETCH</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-61"><a href="#Redshift.Tokenizer-61"><span class="linenos">61</span></a> <span class="s2">"SUPER"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">SUPER</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-62"><a href="#Redshift.Tokenizer-62"><span class="linenos">62</span></a> <span class="s2">"TIME"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMP</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-63"><a href="#Redshift.Tokenizer-63"><span class="linenos">63</span></a> <span class="s2">"TIMETZ"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-64"><a href="#Redshift.Tokenizer-64"><span class="linenos">64</span></a> <span class="s2">"TOP"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">TOP</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-65"><a href="#Redshift.Tokenizer-65"><span class="linenos">65</span></a> <span class="s2">"UNLOAD"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">COMMAND</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-66"><a href="#Redshift.Tokenizer-66"><span class="linenos">66</span></a> <span class="s2">"VARBYTE"</span><span class="p">:</span> <span class="n">TokenType</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">,</span> +</span><span id="Redshift.Tokenizer-67"><a href="#Redshift.Tokenizer-67"><span class="linenos">67</span></a> <span class="p">}</span> </span></pre></div> @@ -530,84 +558,90 @@ Default: "nulls_are_small"</li> </div> <a class="headerlink" href="#Redshift.Generator"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator-64"><a href="#Redshift.Generator-64"><span class="linenos"> 64</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> -</span><span id="Redshift.Generator-65"><a href="#Redshift.Generator-65"><span class="linenos"> 65</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift.Generator-66"><a href="#Redshift.Generator-66"><span class="linenos"> 66</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Generator-67"><a href="#Redshift.Generator-67"><span class="linenos"> 67</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="Redshift.Generator-68"><a href="#Redshift.Generator-68"><span class="linenos"> 68</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> -</span><span id="Redshift.Generator-69"><a href="#Redshift.Generator-69"><span class="linenos"> 69</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> -</span><span id="Redshift.Generator-70"><a href="#Redshift.Generator-70"><span class="linenos"> 70</span></a> <span class="p">}</span> -</span><span id="Redshift.Generator-71"><a href="#Redshift.Generator-71"><span class="linenos"> 71</span></a> -</span><span id="Redshift.Generator-72"><a href="#Redshift.Generator-72"><span class="linenos"> 72</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift.Generator-73"><a href="#Redshift.Generator-73"><span class="linenos"> 73</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Generator-74"><a href="#Redshift.Generator-74"><span class="linenos"> 74</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator-69"><a href="#Redshift.Generator-69"><span class="linenos"> 69</span></a> <span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="p">):</span> +</span><span id="Redshift.Generator-70"><a href="#Redshift.Generator-70"><span class="linenos"> 70</span></a> <span class="n">TYPE_MAPPING</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift.Generator-71"><a href="#Redshift.Generator-71"><span class="linenos"> 71</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TYPE_MAPPING</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift.Generator-72"><a href="#Redshift.Generator-72"><span class="linenos"> 72</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">BINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="Redshift.Generator-73"><a href="#Redshift.Generator-73"><span class="linenos"> 73</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARBINARY</span><span class="p">:</span> <span class="s2">"VARBYTE"</span><span class="p">,</span> +</span><span id="Redshift.Generator-74"><a href="#Redshift.Generator-74"><span class="linenos"> 74</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">INT</span><span class="p">:</span> <span class="s2">"INTEGER"</span><span class="p">,</span> </span><span id="Redshift.Generator-75"><a href="#Redshift.Generator-75"><span class="linenos"> 75</span></a> <span class="p">}</span> </span><span id="Redshift.Generator-76"><a href="#Redshift.Generator-76"><span class="linenos"> 76</span></a> -</span><span id="Redshift.Generator-77"><a href="#Redshift.Generator-77"><span class="linenos"> 77</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> -</span><span id="Redshift.Generator-78"><a href="#Redshift.Generator-78"><span class="linenos"> 78</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Generator-79"><a href="#Redshift.Generator-79"><span class="linenos"> 79</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> -</span><span id="Redshift.Generator-80"><a href="#Redshift.Generator-80"><span class="linenos"> 80</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> -</span><span id="Redshift.Generator-81"><a href="#Redshift.Generator-81"><span class="linenos"> 81</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">e</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">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> -</span><span id="Redshift.Generator-82"><a href="#Redshift.Generator-82"><span class="linenos"> 82</span></a> <span class="p">),</span> -</span><span id="Redshift.Generator-83"><a href="#Redshift.Generator-83"><span class="linenos"> 83</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="Redshift.Generator-84"><a href="#Redshift.Generator-84"><span class="linenos"> 84</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> -</span><span id="Redshift.Generator-85"><a href="#Redshift.Generator-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> -</span><span id="Redshift.Generator-86"><a href="#Redshift.Generator-86"><span class="linenos"> 86</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> -</span><span id="Redshift.Generator-87"><a href="#Redshift.Generator-87"><span class="linenos"> 87</span></a> <span class="p">}</span> -</span><span id="Redshift.Generator-88"><a href="#Redshift.Generator-88"><span class="linenos"> 88</span></a> -</span><span id="Redshift.Generator-89"><a href="#Redshift.Generator-89"><span class="linenos"> 89</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator-90"><a href="#Redshift.Generator-90"><span class="linenos"> 90</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift.Generator-91"><a href="#Redshift.Generator-91"><span class="linenos"> 91</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> -</span><span id="Redshift.Generator-92"><a href="#Redshift.Generator-92"><span class="linenos"> 92</span></a> -</span><span id="Redshift.Generator-93"><a href="#Redshift.Generator-93"><span class="linenos"> 93</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> -</span><span id="Redshift.Generator-94"><a href="#Redshift.Generator-94"><span class="linenos"> 94</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> -</span><span id="Redshift.Generator-95"><a href="#Redshift.Generator-95"><span class="linenos"> 95</span></a><span class="sd"> very slow.</span> -</span><span id="Redshift.Generator-96"><a href="#Redshift.Generator-96"><span class="linenos"> 96</span></a><span class="sd"> """</span> -</span><span id="Redshift.Generator-97"><a href="#Redshift.Generator-97"><span class="linenos"> 97</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> -</span><span id="Redshift.Generator-98"><a href="#Redshift.Generator-98"><span class="linenos"> 98</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="Redshift.Generator-99"><a href="#Redshift.Generator-99"><span class="linenos"> 99</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift.Generator-100"><a href="#Redshift.Generator-100"><span class="linenos">100</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="Redshift.Generator-101"><a href="#Redshift.Generator-101"><span class="linenos">101</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> -</span><span id="Redshift.Generator-102"><a href="#Redshift.Generator-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> -</span><span id="Redshift.Generator-103"><a href="#Redshift.Generator-103"><span class="linenos">103</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> -</span><span id="Redshift.Generator-104"><a href="#Redshift.Generator-104"><span class="linenos">104</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> -</span><span id="Redshift.Generator-105"><a href="#Redshift.Generator-105"><span class="linenos">105</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> -</span><span id="Redshift.Generator-106"><a href="#Redshift.Generator-106"><span class="linenos">106</span></a> <span class="p">]</span> -</span><span id="Redshift.Generator-107"><a href="#Redshift.Generator-107"><span class="linenos">107</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> -</span><span id="Redshift.Generator-108"><a href="#Redshift.Generator-108"><span class="linenos">108</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> -</span><span id="Redshift.Generator-109"><a href="#Redshift.Generator-109"><span class="linenos">109</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> -</span><span id="Redshift.Generator-110"><a href="#Redshift.Generator-110"><span class="linenos">110</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="Redshift.Generator-111"><a href="#Redshift.Generator-111"><span class="linenos">111</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> -</span><span id="Redshift.Generator-112"><a href="#Redshift.Generator-112"><span class="linenos">112</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> -</span><span id="Redshift.Generator-113"><a href="#Redshift.Generator-113"><span class="linenos">113</span></a> -</span><span id="Redshift.Generator-114"><a href="#Redshift.Generator-114"><span class="linenos">114</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator-115"><a href="#Redshift.Generator-115"><span class="linenos">115</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> -</span><span id="Redshift.Generator-116"><a href="#Redshift.Generator-116"><span class="linenos">116</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> -</span><span id="Redshift.Generator-117"><a href="#Redshift.Generator-117"><span class="linenos">117</span></a> -</span><span id="Redshift.Generator-118"><a href="#Redshift.Generator-118"><span class="linenos">118</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator-119"><a href="#Redshift.Generator-119"><span class="linenos">119</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> -</span><span id="Redshift.Generator-120"><a href="#Redshift.Generator-120"><span class="linenos">120</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift.Generator-121"><a href="#Redshift.Generator-121"><span class="linenos">121</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="Redshift.Generator-122"><a href="#Redshift.Generator-122"><span class="linenos">122</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> -</span><span id="Redshift.Generator-123"><a href="#Redshift.Generator-123"><span class="linenos">123</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> -</span><span id="Redshift.Generator-124"><a href="#Redshift.Generator-124"><span class="linenos">124</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> -</span><span id="Redshift.Generator-125"><a href="#Redshift.Generator-125"><span class="linenos">125</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> -</span><span id="Redshift.Generator-126"><a href="#Redshift.Generator-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> -</span><span id="Redshift.Generator-127"><a href="#Redshift.Generator-127"><span class="linenos">127</span></a> -</span><span id="Redshift.Generator-128"><a href="#Redshift.Generator-128"><span class="linenos">128</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator-129"><a href="#Redshift.Generator-129"><span class="linenos">129</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift.Generator-130"><a href="#Redshift.Generator-130"><span class="linenos">130</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> -</span><span id="Redshift.Generator-131"><a href="#Redshift.Generator-131"><span class="linenos">131</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> -</span><span id="Redshift.Generator-132"><a href="#Redshift.Generator-132"><span class="linenos">132</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> -</span><span id="Redshift.Generator-133"><a href="#Redshift.Generator-133"><span class="linenos">133</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> -</span><span id="Redshift.Generator-134"><a href="#Redshift.Generator-134"><span class="linenos">134</span></a><span class="sd"> """</span> -</span><span id="Redshift.Generator-135"><a href="#Redshift.Generator-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> -</span><span id="Redshift.Generator-136"><a href="#Redshift.Generator-136"><span class="linenos">136</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift.Generator-137"><a href="#Redshift.Generator-137"><span class="linenos">137</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> -</span><span id="Redshift.Generator-138"><a href="#Redshift.Generator-138"><span class="linenos">138</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> -</span><span id="Redshift.Generator-139"><a href="#Redshift.Generator-139"><span class="linenos">139</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> -</span><span id="Redshift.Generator-140"><a href="#Redshift.Generator-140"><span class="linenos">140</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> -</span><span id="Redshift.Generator-141"><a href="#Redshift.Generator-141"><span class="linenos">141</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="Redshift.Generator-77"><a href="#Redshift.Generator-77"><span class="linenos"> 77</span></a> <span class="n">PROPERTIES_LOCATION</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift.Generator-78"><a href="#Redshift.Generator-78"><span class="linenos"> 78</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">PROPERTIES_LOCATION</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift.Generator-79"><a href="#Redshift.Generator-79"><span class="linenos"> 79</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LikeProperty</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="o">.</span><span class="n">Location</span><span class="o">.</span><span class="n">POST_WITH</span><span class="p">,</span> +</span><span id="Redshift.Generator-80"><a href="#Redshift.Generator-80"><span class="linenos"> 80</span></a> <span class="p">}</span> +</span><span id="Redshift.Generator-81"><a href="#Redshift.Generator-81"><span class="linenos"> 81</span></a> +</span><span id="Redshift.Generator-82"><a href="#Redshift.Generator-82"><span class="linenos"> 82</span></a> <span class="n">TRANSFORMS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="Redshift.Generator-83"><a href="#Redshift.Generator-83"><span class="linenos"> 83</span></a> <span class="o">**</span><span class="n">Postgres</span><span class="o">.</span><span class="n">Generator</span><span class="o">.</span><span class="n">TRANSFORMS</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift.Generator-84"><a href="#Redshift.Generator-84"><span class="linenos"> 84</span></a> <span class="o">**</span><span class="n">transforms</span><span class="o">.</span><span class="n">ELIMINATE_DISTINCT_ON</span><span class="p">,</span> <span class="c1"># type: ignore</span> +</span><span id="Redshift.Generator-85"><a href="#Redshift.Generator-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="Redshift.Generator-86"><a href="#Redshift.Generator-86"><span class="linenos"> 86</span></a> <span class="s2">"DATEADD"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift.Generator-87"><a href="#Redshift.Generator-87"><span class="linenos"> 87</span></a> <span class="p">),</span> +</span><span id="Redshift.Generator-88"><a href="#Redshift.Generator-88"><span class="linenos"> 88</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span> +</span><span id="Redshift.Generator-89"><a href="#Redshift.Generator-89"><span class="linenos"> 89</span></a> <span class="s2">"DATEDIFF"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"day"</span><span class="p">),</span> <span class="n">e</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift.Generator-90"><a href="#Redshift.Generator-90"><span class="linenos"> 90</span></a> <span class="p">),</span> +</span><span id="Redshift.Generator-91"><a href="#Redshift.Generator-91"><span class="linenos"> 91</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"DISTKEY(</span><span class="si">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="Redshift.Generator-92"><a href="#Redshift.Generator-92"><span class="linenos"> 92</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DistStyleProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">naked_property</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> +</span><span id="Redshift.Generator-93"><a href="#Redshift.Generator-93"><span class="linenos"> 93</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Matches</span><span class="p">:</span> <span class="n">rename_func</span><span class="p">(</span><span class="s2">"DECODE"</span><span class="p">),</span> +</span><span id="Redshift.Generator-94"><a href="#Redshift.Generator-94"><span class="linenos"> 94</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SortKeyProperty</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="s1">'COMPOUND '</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">'compound'</span><span class="p">]</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">''</span><span class="si">}</span><span class="s2">SORTKEY(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">format_args</span><span class="p">(</span><span class="o">*</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="p">)</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="Redshift.Generator-95"><a href="#Redshift.Generator-95"><span class="linenos"> 95</span></a> <span class="p">}</span> +</span><span id="Redshift.Generator-96"><a href="#Redshift.Generator-96"><span class="linenos"> 96</span></a> +</span><span id="Redshift.Generator-97"><a href="#Redshift.Generator-97"><span class="linenos"> 97</span></a> <span class="c1"># Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres)</span> +</span><span id="Redshift.Generator-98"><a href="#Redshift.Generator-98"><span class="linenos"> 98</span></a> <span class="n">TRANSFORMS</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Pow</span><span class="p">)</span> +</span><span id="Redshift.Generator-99"><a href="#Redshift.Generator-99"><span class="linenos"> 99</span></a> +</span><span id="Redshift.Generator-100"><a href="#Redshift.Generator-100"><span class="linenos">100</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator-101"><a href="#Redshift.Generator-101"><span class="linenos">101</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift.Generator-102"><a href="#Redshift.Generator-102"><span class="linenos">102</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> +</span><span id="Redshift.Generator-103"><a href="#Redshift.Generator-103"><span class="linenos">103</span></a> +</span><span id="Redshift.Generator-104"><a href="#Redshift.Generator-104"><span class="linenos">104</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> +</span><span id="Redshift.Generator-105"><a href="#Redshift.Generator-105"><span class="linenos">105</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> +</span><span id="Redshift.Generator-106"><a href="#Redshift.Generator-106"><span class="linenos">106</span></a><span class="sd"> very slow.</span> +</span><span id="Redshift.Generator-107"><a href="#Redshift.Generator-107"><span class="linenos">107</span></a><span class="sd"> """</span> +</span><span id="Redshift.Generator-108"><a href="#Redshift.Generator-108"><span class="linenos">108</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="Redshift.Generator-109"><a href="#Redshift.Generator-109"><span class="linenos">109</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="Redshift.Generator-110"><a href="#Redshift.Generator-110"><span class="linenos">110</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift.Generator-111"><a href="#Redshift.Generator-111"><span class="linenos">111</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Redshift.Generator-112"><a href="#Redshift.Generator-112"><span class="linenos">112</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> +</span><span id="Redshift.Generator-113"><a href="#Redshift.Generator-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="Redshift.Generator-114"><a href="#Redshift.Generator-114"><span class="linenos">114</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="Redshift.Generator-115"><a href="#Redshift.Generator-115"><span class="linenos">115</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> +</span><span id="Redshift.Generator-116"><a href="#Redshift.Generator-116"><span class="linenos">116</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> +</span><span id="Redshift.Generator-117"><a href="#Redshift.Generator-117"><span class="linenos">117</span></a> <span class="p">]</span> +</span><span id="Redshift.Generator-118"><a href="#Redshift.Generator-118"><span class="linenos">118</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> +</span><span id="Redshift.Generator-119"><a href="#Redshift.Generator-119"><span class="linenos">119</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="Redshift.Generator-120"><a href="#Redshift.Generator-120"><span class="linenos">120</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> +</span><span id="Redshift.Generator-121"><a href="#Redshift.Generator-121"><span class="linenos">121</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="Redshift.Generator-122"><a href="#Redshift.Generator-122"><span class="linenos">122</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="Redshift.Generator-123"><a href="#Redshift.Generator-123"><span class="linenos">123</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> +</span><span id="Redshift.Generator-124"><a href="#Redshift.Generator-124"><span class="linenos">124</span></a> +</span><span id="Redshift.Generator-125"><a href="#Redshift.Generator-125"><span class="linenos">125</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator-126"><a href="#Redshift.Generator-126"><span class="linenos">126</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> +</span><span id="Redshift.Generator-127"><a href="#Redshift.Generator-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> +</span><span id="Redshift.Generator-128"><a href="#Redshift.Generator-128"><span class="linenos">128</span></a> +</span><span id="Redshift.Generator-129"><a href="#Redshift.Generator-129"><span class="linenos">129</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator-130"><a href="#Redshift.Generator-130"><span class="linenos">130</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> +</span><span id="Redshift.Generator-131"><a href="#Redshift.Generator-131"><span class="linenos">131</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift.Generator-132"><a href="#Redshift.Generator-132"><span class="linenos">132</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift.Generator-133"><a href="#Redshift.Generator-133"><span class="linenos">133</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> +</span><span id="Redshift.Generator-134"><a href="#Redshift.Generator-134"><span class="linenos">134</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> +</span><span id="Redshift.Generator-135"><a href="#Redshift.Generator-135"><span class="linenos">135</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Redshift.Generator-136"><a href="#Redshift.Generator-136"><span class="linenos">136</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> +</span><span id="Redshift.Generator-137"><a href="#Redshift.Generator-137"><span class="linenos">137</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> +</span><span id="Redshift.Generator-138"><a href="#Redshift.Generator-138"><span class="linenos">138</span></a> +</span><span id="Redshift.Generator-139"><a href="#Redshift.Generator-139"><span class="linenos">139</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator-140"><a href="#Redshift.Generator-140"><span class="linenos">140</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift.Generator-141"><a href="#Redshift.Generator-141"><span class="linenos">141</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> +</span><span id="Redshift.Generator-142"><a href="#Redshift.Generator-142"><span class="linenos">142</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> +</span><span id="Redshift.Generator-143"><a href="#Redshift.Generator-143"><span class="linenos">143</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> +</span><span id="Redshift.Generator-144"><a href="#Redshift.Generator-144"><span class="linenos">144</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> +</span><span id="Redshift.Generator-145"><a href="#Redshift.Generator-145"><span class="linenos">145</span></a><span class="sd"> """</span> +</span><span id="Redshift.Generator-146"><a href="#Redshift.Generator-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> +</span><span id="Redshift.Generator-147"><a href="#Redshift.Generator-147"><span class="linenos">147</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift.Generator-148"><a href="#Redshift.Generator-148"><span class="linenos">148</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> +</span><span id="Redshift.Generator-149"><a href="#Redshift.Generator-149"><span class="linenos">149</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> +</span><span id="Redshift.Generator-150"><a href="#Redshift.Generator-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> +</span><span id="Redshift.Generator-151"><a href="#Redshift.Generator-151"><span class="linenos">151</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> +</span><span id="Redshift.Generator-152"><a href="#Redshift.Generator-152"><span class="linenos">152</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> </span></pre></div> @@ -624,8 +658,7 @@ represents a python time format and the output the target time format</li> <li><strong>quote_end (str):</strong> specifies which ending character to use to delimit quotes. Default: '.</li> <li><strong>identifier_start (str):</strong> specifies which starting character to use to delimit identifiers. Default: ".</li> <li><strong>identifier_end (str):</strong> specifies which ending character to use to delimit identifiers. Default: ".</li> -<li><strong>identify (bool):</strong> if set to True all identifiers will be delimited by the corresponding -character.</li> +<li><strong>identify (bool | str):</strong> 'always': always quote, 'safe': quote identifiers if they don't contain an upcase, True defaults to always.</li> <li><strong>normalize (bool):</strong> if set to True all identifiers will lower cased</li> <li><strong>string_escape (str):</strong> specifies a string escape character. Default: '.</li> <li><strong>identifier_escape (str):</strong> specifies an identifier escape character. Default: ".</li> @@ -667,30 +700,30 @@ Default: True</li> </div> <a class="headerlink" href="#Redshift.Generator.values_sql"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.values_sql-89"><a href="#Redshift.Generator.values_sql-89"><span class="linenos"> 89</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator.values_sql-90"><a href="#Redshift.Generator.values_sql-90"><span class="linenos"> 90</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift.Generator.values_sql-91"><a href="#Redshift.Generator.values_sql-91"><span class="linenos"> 91</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> -</span><span id="Redshift.Generator.values_sql-92"><a href="#Redshift.Generator.values_sql-92"><span class="linenos"> 92</span></a> -</span><span id="Redshift.Generator.values_sql-93"><a href="#Redshift.Generator.values_sql-93"><span class="linenos"> 93</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> -</span><span id="Redshift.Generator.values_sql-94"><a href="#Redshift.Generator.values_sql-94"><span class="linenos"> 94</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> -</span><span id="Redshift.Generator.values_sql-95"><a href="#Redshift.Generator.values_sql-95"><span class="linenos"> 95</span></a><span class="sd"> very slow.</span> -</span><span id="Redshift.Generator.values_sql-96"><a href="#Redshift.Generator.values_sql-96"><span class="linenos"> 96</span></a><span class="sd"> """</span> -</span><span id="Redshift.Generator.values_sql-97"><a href="#Redshift.Generator.values_sql-97"><span class="linenos"> 97</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> -</span><span id="Redshift.Generator.values_sql-98"><a href="#Redshift.Generator.values_sql-98"><span class="linenos"> 98</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> -</span><span id="Redshift.Generator.values_sql-99"><a href="#Redshift.Generator.values_sql-99"><span class="linenos"> 99</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift.Generator.values_sql-100"><a href="#Redshift.Generator.values_sql-100"><span class="linenos">100</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> -</span><span id="Redshift.Generator.values_sql-101"><a href="#Redshift.Generator.values_sql-101"><span class="linenos">101</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> -</span><span id="Redshift.Generator.values_sql-102"><a href="#Redshift.Generator.values_sql-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> -</span><span id="Redshift.Generator.values_sql-103"><a href="#Redshift.Generator.values_sql-103"><span class="linenos">103</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> -</span><span id="Redshift.Generator.values_sql-104"><a href="#Redshift.Generator.values_sql-104"><span class="linenos">104</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> -</span><span id="Redshift.Generator.values_sql-105"><a href="#Redshift.Generator.values_sql-105"><span class="linenos">105</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> -</span><span id="Redshift.Generator.values_sql-106"><a href="#Redshift.Generator.values_sql-106"><span class="linenos">106</span></a> <span class="p">]</span> -</span><span id="Redshift.Generator.values_sql-107"><a href="#Redshift.Generator.values_sql-107"><span class="linenos">107</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> -</span><span id="Redshift.Generator.values_sql-108"><a href="#Redshift.Generator.values_sql-108"><span class="linenos">108</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> -</span><span id="Redshift.Generator.values_sql-109"><a href="#Redshift.Generator.values_sql-109"><span class="linenos">109</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> -</span><span id="Redshift.Generator.values_sql-110"><a href="#Redshift.Generator.values_sql-110"><span class="linenos">110</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> -</span><span id="Redshift.Generator.values_sql-111"><a href="#Redshift.Generator.values_sql-111"><span class="linenos">111</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> -</span><span id="Redshift.Generator.values_sql-112"><a href="#Redshift.Generator.values_sql-112"><span class="linenos">112</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.values_sql-100"><a href="#Redshift.Generator.values_sql-100"><span class="linenos">100</span></a> <span class="k">def</span> <span class="nf">values_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">Values</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator.values_sql-101"><a href="#Redshift.Generator.values_sql-101"><span class="linenos">101</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift.Generator.values_sql-102"><a href="#Redshift.Generator.values_sql-102"><span class="linenos">102</span></a><span class="sd"> Converts `VALUES...` expression into a series of unions.</span> +</span><span id="Redshift.Generator.values_sql-103"><a href="#Redshift.Generator.values_sql-103"><span class="linenos">103</span></a> +</span><span id="Redshift.Generator.values_sql-104"><a href="#Redshift.Generator.values_sql-104"><span class="linenos">104</span></a><span class="sd"> Note: If you have a lot of unions then this will result in a large number of recursive statements to</span> +</span><span id="Redshift.Generator.values_sql-105"><a href="#Redshift.Generator.values_sql-105"><span class="linenos">105</span></a><span class="sd"> evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be</span> +</span><span id="Redshift.Generator.values_sql-106"><a href="#Redshift.Generator.values_sql-106"><span class="linenos">106</span></a><span class="sd"> very slow.</span> +</span><span id="Redshift.Generator.values_sql-107"><a href="#Redshift.Generator.values_sql-107"><span class="linenos">107</span></a><span class="sd"> """</span> +</span><span id="Redshift.Generator.values_sql-108"><a href="#Redshift.Generator.values_sql-108"><span class="linenos">108</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="Redshift.Generator.values_sql-109"><a href="#Redshift.Generator.values_sql-109"><span class="linenos">109</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">values_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="Redshift.Generator.values_sql-110"><a href="#Redshift.Generator.values_sql-110"><span class="linenos">110</span></a> <span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="n">tuple_exp</span><span class="o">.</span><span class="n">expressions</span> <span class="k">for</span> <span class="n">tuple_exp</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="Redshift.Generator.values_sql-111"><a href="#Redshift.Generator.values_sql-111"><span class="linenos">111</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Redshift.Generator.values_sql-112"><a href="#Redshift.Generator.values_sql-112"><span class="linenos">112</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span> +</span><span id="Redshift.Generator.values_sql-113"><a href="#Redshift.Generator.values_sql-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="Redshift.Generator.values_sql-114"><a href="#Redshift.Generator.values_sql-114"><span class="linenos">114</span></a> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="Redshift.Generator.values_sql-115"><a href="#Redshift.Generator.values_sql-115"><span class="linenos">115</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">column_name</span><span class="p">)</span> +</span><span id="Redshift.Generator.values_sql-116"><a href="#Redshift.Generator.values_sql-116"><span class="linenos">116</span></a> <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">column_name</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"alias"</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"columns"</span><span class="p">])</span> +</span><span id="Redshift.Generator.values_sql-117"><a href="#Redshift.Generator.values_sql-117"><span class="linenos">117</span></a> <span class="p">]</span> +</span><span id="Redshift.Generator.values_sql-118"><a href="#Redshift.Generator.values_sql-118"><span class="linenos">118</span></a> <span class="n">selects</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">row</span><span class="p">))</span> +</span><span id="Redshift.Generator.values_sql-119"><a href="#Redshift.Generator.values_sql-119"><span class="linenos">119</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="Redshift.Generator.values_sql-120"><a href="#Redshift.Generator.values_sql-120"><span class="linenos">120</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">selects</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span> +</span><span id="Redshift.Generator.values_sql-121"><a href="#Redshift.Generator.values_sql-121"><span class="linenos">121</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="Redshift.Generator.values_sql-122"><a href="#Redshift.Generator.values_sql-122"><span class="linenos">122</span></a> <span class="n">subquery_expression</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">subquery_expression</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">distinct</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="Redshift.Generator.values_sql-123"><a href="#Redshift.Generator.values_sql-123"><span class="linenos">123</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_sql</span><span class="p">(</span><span class="n">subquery_expression</span><span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">alias</span><span class="p">))</span> </span></pre></div> @@ -714,9 +747,9 @@ very slow.</p> </div> <a class="headerlink" href="#Redshift.Generator.with_properties"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.with_properties-114"><a href="#Redshift.Generator.with_properties-114"><span class="linenos">114</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator.with_properties-115"><a href="#Redshift.Generator.with_properties-115"><span class="linenos">115</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> -</span><span id="Redshift.Generator.with_properties-116"><a href="#Redshift.Generator.with_properties-116"><span class="linenos">116</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.with_properties-125"><a href="#Redshift.Generator.with_properties-125"><span class="linenos">125</span></a> <span class="k">def</span> <span class="nf">with_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Properties</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator.with_properties-126"><a href="#Redshift.Generator.with_properties-126"><span class="linenos">126</span></a><span class="w"> </span><span class="sd">"""Redshift doesn't have `WITH` as part of their with_properties so we remove it"""</span> +</span><span id="Redshift.Generator.with_properties-127"><a href="#Redshift.Generator.with_properties-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="n">properties</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s2">" "</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span> </span></pre></div> @@ -736,15 +769,15 @@ very slow.</p> </div> <a class="headerlink" href="#Redshift.Generator.renametable_sql"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.renametable_sql-118"><a href="#Redshift.Generator.renametable_sql-118"><span class="linenos">118</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator.renametable_sql-119"><a href="#Redshift.Generator.renametable_sql-119"><span class="linenos">119</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> -</span><span id="Redshift.Generator.renametable_sql-120"><a href="#Redshift.Generator.renametable_sql-120"><span class="linenos">120</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift.Generator.renametable_sql-121"><a href="#Redshift.Generator.renametable_sql-121"><span class="linenos">121</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> -</span><span id="Redshift.Generator.renametable_sql-122"><a href="#Redshift.Generator.renametable_sql-122"><span class="linenos">122</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> -</span><span id="Redshift.Generator.renametable_sql-123"><a href="#Redshift.Generator.renametable_sql-123"><span class="linenos">123</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> -</span><span id="Redshift.Generator.renametable_sql-124"><a href="#Redshift.Generator.renametable_sql-124"><span class="linenos">124</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> -</span><span id="Redshift.Generator.renametable_sql-125"><a href="#Redshift.Generator.renametable_sql-125"><span class="linenos">125</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> -</span><span id="Redshift.Generator.renametable_sql-126"><a href="#Redshift.Generator.renametable_sql-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.renametable_sql-129"><a href="#Redshift.Generator.renametable_sql-129"><span class="linenos">129</span></a> <span class="k">def</span> <span class="nf">renametable_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">RenameTable</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator.renametable_sql-130"><a href="#Redshift.Generator.renametable_sql-130"><span class="linenos">130</span></a><span class="w"> </span><span class="sd">"""Redshift only supports defining the table name itself (not the db) when renaming tables"""</span> +</span><span id="Redshift.Generator.renametable_sql-131"><a href="#Redshift.Generator.renametable_sql-131"><span class="linenos">131</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift.Generator.renametable_sql-132"><a href="#Redshift.Generator.renametable_sql-132"><span class="linenos">132</span></a> <span class="n">target_table</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="Redshift.Generator.renametable_sql-133"><a href="#Redshift.Generator.renametable_sql-133"><span class="linenos">133</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">target_table</span><span class="o">.</span><span class="n">args</span><span class="p">:</span> +</span><span id="Redshift.Generator.renametable_sql-134"><a href="#Redshift.Generator.renametable_sql-134"><span class="linenos">134</span></a> <span class="k">if</span> <span class="n">arg</span> <span class="o">!=</span> <span class="s2">"this"</span><span class="p">:</span> +</span><span id="Redshift.Generator.renametable_sql-135"><a href="#Redshift.Generator.renametable_sql-135"><span class="linenos">135</span></a> <span class="n">target_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Redshift.Generator.renametable_sql-136"><a href="#Redshift.Generator.renametable_sql-136"><span class="linenos">136</span></a> <span class="n">this</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">)</span> +</span><span id="Redshift.Generator.renametable_sql-137"><a href="#Redshift.Generator.renametable_sql-137"><span class="linenos">137</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"RENAME TO </span><span class="si">{</span><span class="n">this</span><span class="si">}</span><span class="s2">"</span> </span></pre></div> @@ -764,20 +797,20 @@ very slow.</p> </div> <a class="headerlink" href="#Redshift.Generator.datatype_sql"></a> - <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.datatype_sql-128"><a href="#Redshift.Generator.datatype_sql-128"><span class="linenos">128</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> -</span><span id="Redshift.Generator.datatype_sql-129"><a href="#Redshift.Generator.datatype_sql-129"><span class="linenos">129</span></a><span class="w"> </span><span class="sd">"""</span> -</span><span id="Redshift.Generator.datatype_sql-130"><a href="#Redshift.Generator.datatype_sql-130"><span class="linenos">130</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> -</span><span id="Redshift.Generator.datatype_sql-131"><a href="#Redshift.Generator.datatype_sql-131"><span class="linenos">131</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> -</span><span id="Redshift.Generator.datatype_sql-132"><a href="#Redshift.Generator.datatype_sql-132"><span class="linenos">132</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> -</span><span id="Redshift.Generator.datatype_sql-133"><a href="#Redshift.Generator.datatype_sql-133"><span class="linenos">133</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> -</span><span id="Redshift.Generator.datatype_sql-134"><a href="#Redshift.Generator.datatype_sql-134"><span class="linenos">134</span></a><span class="sd"> """</span> -</span><span id="Redshift.Generator.datatype_sql-135"><a href="#Redshift.Generator.datatype_sql-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> -</span><span id="Redshift.Generator.datatype_sql-136"><a href="#Redshift.Generator.datatype_sql-136"><span class="linenos">136</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> -</span><span id="Redshift.Generator.datatype_sql-137"><a href="#Redshift.Generator.datatype_sql-137"><span class="linenos">137</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> -</span><span id="Redshift.Generator.datatype_sql-138"><a href="#Redshift.Generator.datatype_sql-138"><span class="linenos">138</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> -</span><span id="Redshift.Generator.datatype_sql-139"><a href="#Redshift.Generator.datatype_sql-139"><span class="linenos">139</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> -</span><span id="Redshift.Generator.datatype_sql-140"><a href="#Redshift.Generator.datatype_sql-140"><span class="linenos">140</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> -</span><span id="Redshift.Generator.datatype_sql-141"><a href="#Redshift.Generator.datatype_sql-141"><span class="linenos">141</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> + <div class="pdoc-code codehilite"><pre><span></span><span id="Redshift.Generator.datatype_sql-139"><a href="#Redshift.Generator.datatype_sql-139"><span class="linenos">139</span></a> <span class="k">def</span> <span class="nf">datatype_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">DataType</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span> +</span><span id="Redshift.Generator.datatype_sql-140"><a href="#Redshift.Generator.datatype_sql-140"><span class="linenos">140</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Redshift.Generator.datatype_sql-141"><a href="#Redshift.Generator.datatype_sql-141"><span class="linenos">141</span></a><span class="sd"> Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean</span> +</span><span id="Redshift.Generator.datatype_sql-142"><a href="#Redshift.Generator.datatype_sql-142"><span class="linenos">142</span></a><span class="sd"> VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type</span> +</span><span id="Redshift.Generator.datatype_sql-143"><a href="#Redshift.Generator.datatype_sql-143"><span class="linenos">143</span></a><span class="sd"> without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert</span> +</span><span id="Redshift.Generator.datatype_sql-144"><a href="#Redshift.Generator.datatype_sql-144"><span class="linenos">144</span></a><span class="sd"> `TEXT` to `VARCHAR`.</span> +</span><span id="Redshift.Generator.datatype_sql-145"><a href="#Redshift.Generator.datatype_sql-145"><span class="linenos">145</span></a><span class="sd"> """</span> +</span><span id="Redshift.Generator.datatype_sql-146"><a href="#Redshift.Generator.datatype_sql-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">TEXT</span><span class="p">:</span> +</span><span id="Redshift.Generator.datatype_sql-147"><a href="#Redshift.Generator.datatype_sql-147"><span class="linenos">147</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="Redshift.Generator.datatype_sql-148"><a href="#Redshift.Generator.datatype_sql-148"><span class="linenos">148</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">VARCHAR</span><span class="p">)</span> +</span><span id="Redshift.Generator.datatype_sql-149"><a href="#Redshift.Generator.datatype_sql-149"><span class="linenos">149</span></a> <span class="n">precision</span> <span class="o">=</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">"expressions"</span><span class="p">)</span> +</span><span id="Redshift.Generator.datatype_sql-150"><a href="#Redshift.Generator.datatype_sql-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">precision</span><span class="p">:</span> +</span><span id="Redshift.Generator.datatype_sql-151"><a href="#Redshift.Generator.datatype_sql-151"><span class="linenos">151</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Var</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="s2">"MAX"</span><span class="p">))</span> +</span><span id="Redshift.Generator.datatype_sql-152"><a href="#Redshift.Generator.datatype_sql-152"><span class="linenos">152</span></a> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">datatype_sql</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> </span></pre></div> @@ -876,6 +909,8 @@ without precision we convert it to <code>VARCHAR(max)</code> and if it does have <dd id="Redshift.Generator.lateral_sql" class="function"><a href="../generator.html#Generator.lateral_sql">lateral_sql</a></dd> <dd id="Redshift.Generator.limit_sql" class="function"><a href="../generator.html#Generator.limit_sql">limit_sql</a></dd> <dd id="Redshift.Generator.offset_sql" class="function"><a href="../generator.html#Generator.offset_sql">offset_sql</a></dd> + <dd id="Redshift.Generator.setitem_sql" class="function"><a href="../generator.html#Generator.setitem_sql">setitem_sql</a></dd> + <dd id="Redshift.Generator.set_sql" class="function"><a href="../generator.html#Generator.set_sql">set_sql</a></dd> <dd id="Redshift.Generator.lock_sql" class="function"><a href="../generator.html#Generator.lock_sql">lock_sql</a></dd> <dd id="Redshift.Generator.literal_sql" class="function"><a href="../generator.html#Generator.literal_sql">literal_sql</a></dd> <dd id="Redshift.Generator.loaddata_sql" class="function"><a href="../generator.html#Generator.loaddata_sql">loaddata_sql</a></dd> @@ -959,7 +994,6 @@ without precision we convert it to <code>VARCHAR(max)</code> and if it does have <dd id="Redshift.Generator.intdiv_sql" class="function"><a href="../generator.html#Generator.intdiv_sql">intdiv_sql</a></dd> <dd id="Redshift.Generator.dpipe_sql" class="function"><a href="../generator.html#Generator.dpipe_sql">dpipe_sql</a></dd> <dd id="Redshift.Generator.div_sql" class="function"><a href="../generator.html#Generator.div_sql">div_sql</a></dd> - <dd id="Redshift.Generator.floatdiv_sql" class="function"><a href="../generator.html#Generator.floatdiv_sql">floatdiv_sql</a></dd> <dd id="Redshift.Generator.overlaps_sql" class="function"><a href="../generator.html#Generator.overlaps_sql">overlaps_sql</a></dd> <dd id="Redshift.Generator.distance_sql" class="function"><a href="../generator.html#Generator.distance_sql">distance_sql</a></dd> <dd id="Redshift.Generator.dot_sql" class="function"><a href="../generator.html#Generator.dot_sql">dot_sql</a></dd> @@ -1001,6 +1035,7 @@ without precision we convert it to <code>VARCHAR(max)</code> and if it does have <dd id="Redshift.Generator.kwarg_sql" class="function"><a href="../generator.html#Generator.kwarg_sql">kwarg_sql</a></dd> <dd id="Redshift.Generator.when_sql" class="function"><a href="../generator.html#Generator.when_sql">when_sql</a></dd> <dd id="Redshift.Generator.merge_sql" class="function"><a href="../generator.html#Generator.merge_sql">merge_sql</a></dd> + <dd id="Redshift.Generator.tochar_sql" class="function"><a href="../generator.html#Generator.tochar_sql">tochar_sql</a></dd> </div> </dl> |