diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-02-12 10:03:37 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-02-12 10:03:37 +0000 |
commit | 24e839c04c39d1f4423b267c371e8e5b5bc33867 (patch) | |
tree | 2395ec71424fe1ff783e0dc7ca0f9b4e16def8a9 /docs/sqlglot/optimizer | |
parent | Adding upstream version 10.6.3. (diff) | |
download | sqlglot-24e839c04c39d1f4423b267c371e8e5b5bc33867.tar.xz sqlglot-24e839c04c39d1f4423b267c371e8e5b5bc33867.zip |
Adding upstream version 11.0.1.upstream/11.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/sqlglot/optimizer')
20 files changed, 14133 insertions, 0 deletions
diff --git a/docs/sqlglot/optimizer/annotate_types.html b/docs/sqlglot/optimizer/annotate_types.html new file mode 100644 index 0000000..2d84d3e --- /dev/null +++ b/docs/sqlglot/optimizer/annotate_types.html @@ -0,0 +1,1179 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.annotate_types API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#annotate_types">annotate_types</a> + </li> + <li> + <a class="class" href="#TypeAnnotator">TypeAnnotator</a> + <ul class="memberlist"> + <li> + <a class="function" href="#TypeAnnotator.__init__">TypeAnnotator</a> + </li> + <li> + <a class="function" href="#TypeAnnotator.annotate">annotate</a> + </li> + </ul> + + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/annotate_types.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.annotate_types </h1> + + + <input id="mod-annotate_types-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-annotate_types-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">ensure_collection</span><span class="p">,</span> <span class="n">ensure_list</span><span class="p">,</span> <span class="n">subclasses</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">annotate_types</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">annotators</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Recursively infer & annotate types in an expression syntax tree against a schema.</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="sd"> Assumes that we've already executed the optimizer's qualify_columns step.</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="sd"> Example:</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> >>> schema = {"y": {"cola": "SMALLINT"}}</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> sql = "SELECT x.cola + 2.5 AS cola FROM (SELECT y.cola AS cola FROM y AS y) AS x"</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> annotated_expr = annotate_types(sqlglot.parse_one(sql), schema=schema)</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> >>> annotated_expr.expressions[0].type.this # Get the type of "x.cola + 2.5 AS cola"</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> <Type.DOUBLE: 'DOUBLE'></span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Args:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> expression (sqlglot.Expression): Expression to annotate.</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): Database schema.</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> annotators (dict): Maps expression type to corresponding annotation function.</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> coerces_to (dict): Maps expression type to set of types that it can be coerced into.</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> Returns:</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> sqlglot.Expression: expression annotated with types</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> """</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="k">return</span> <span class="n">TypeAnnotator</span><span class="p">(</span><span class="n">schema</span><span class="p">,</span> <span class="n">annotators</span><span class="p">,</span> <span class="n">coerces_to</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> +</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">class</span> <span class="nc">TypeAnnotator</span><span class="p">:</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">ANNOTATORS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="o">**</span><span class="p">{</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="n">expr_type</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_unary</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="k">for</span> <span class="n">expr_type</span> <span class="ow">in</span> <span class="n">subclasses</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unary</span><span class="p">)</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="p">},</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="o">**</span><span class="p">{</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="n">expr_type</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_binary</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="k">for</span> <span class="n">expr_type</span> <span class="ow">in</span> <span class="n">subclasses</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="p">},</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]),</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TryCast</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]),</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_unary</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_literal</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">NULL</span><span class="p">),</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Anonymous</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">UNKNOWN</span><span class="p">),</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ApproxDistinct</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="n">expr</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">BIGINT</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="p">),</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Avg</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Min</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">),</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">),</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Sum</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="n">promote</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Ceil</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BIGINT</span><span class="p">),</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentDatetime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="n">expr</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">DATETIME</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 class="n">exp</span><span class="o">.</span><span class="n">CurrentTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> <span class="n">expr</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">TIMESTAMP</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 class="n">exp</span><span class="o">.</span><span class="n">CurrentTimestamp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <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">DateAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</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">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DatetimeAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="n">expr</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">DATETIME</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="p">),</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">DatetimeSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="n">expr</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">DATETIME</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">DatetimeDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</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">Extract</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</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">TimestampAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">expr</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">TIMESTAMP</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">TimestampSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="n">expr</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">TIMESTAMP</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">TimestampDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</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">TimeAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TIMESTAMP</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">TimeSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TIMESTAMP</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">TimeDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateStrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="p">),</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateToDateStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="n">expr</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><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="p">),</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateToDi</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Day</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DiToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Exp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Floor</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Case</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"default"</span><span class="p">,</span> <span class="s2">"ifs"</span><span class="p">),</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">If</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"true"</span><span class="p">,</span> <span class="s2">"false"</span><span class="p">),</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expressions"</span><span class="p">),</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">IfNull</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expression"</span><span class="p">),</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GroupConcat</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="n">expr</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><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="p">),</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ArrayConcat</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="n">expr</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><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="p">),</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Initcap</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Length</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BIGINT</span><span class="p">),</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Levenshtein</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Ln</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log2</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log10</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lower</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Month</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Pow</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Quantile</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ApproxQuantile</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="n">expr</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">DOUBLE</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="p">),</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">RegexpLike</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="n">expr</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">BOOLEAN</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="p">),</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Round</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SafeDivide</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Substring</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrPosition</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="p">),</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Sqrt</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Stddev</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StddevPop</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StddevSamp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeToStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeToTimeStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">expr</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><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="p">),</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeStrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="p">),</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeStrToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="p">),</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Trim</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDateStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="n">expr</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><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="p">),</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDiToDi</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-162"><a href="#L-162"><span class="linenos">162</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="p">),</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToTimeStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="n">expr</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><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="p">),</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Upper</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Variance</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">VariancePop</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="n">expr</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">DOUBLE</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="p">),</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Week</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Year</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="p">}</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="c1"># Reference: https://spark.apache.org/docs/3.2.0/sql-ref-ansi-compliance.html</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="n">COERCES_TO</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="c1"># CHAR < NCHAR < VARCHAR < NVARCHAR < TEXT</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</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">TEXT</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</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">NVARCHAR</span><span class="p">:</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">TEXT</span><span class="p">},</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</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">VARCHAR</span><span class="p">:</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">NVARCHAR</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">TEXT</span><span class="p">},</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</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">NCHAR</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</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">VARCHAR</span><span class="p">,</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</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">NVARCHAR</span><span class="p">,</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</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">TEXT</span><span class="p">,</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="p">},</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</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">CHAR</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</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">NCHAR</span><span class="p">,</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</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">VARCHAR</span><span class="p">,</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</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">NVARCHAR</span><span class="p">,</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</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">TEXT</span><span class="p">,</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="p">},</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="c1"># TINYINT < SMALLINT < INT < BIGINT < DECIMAL < FLOAT < DOUBLE</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</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">DOUBLE</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</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">FLOAT</span><span class="p">:</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">DOUBLE</span><span class="p">},</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</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">DECIMAL</span><span class="p">:</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">FLOAT</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">DOUBLE</span><span class="p">},</span> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</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">BIGINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</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">DECIMAL</span><span class="p">,</span> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</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">FLOAT</span><span class="p">,</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</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">DOUBLE</span><span class="p">,</span> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> <span class="p">},</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</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="p">{</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</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">BIGINT</span><span class="p">,</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</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">DECIMAL</span><span class="p">,</span> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</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">FLOAT</span><span class="p">,</span> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</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">DOUBLE</span><span class="p">,</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> <span class="p">},</span> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</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">SMALLINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</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><span id="L-211"><a href="#L-211"><span class="linenos">211</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">BIGINT</span><span class="p">,</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</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">DECIMAL</span><span class="p">,</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</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">FLOAT</span><span class="p">,</span> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</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">DOUBLE</span><span class="p">,</span> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> <span class="p">},</span> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</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">TINYINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</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">SMALLINT</span><span class="p">,</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</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><span id="L-219"><a href="#L-219"><span class="linenos">219</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">BIGINT</span><span class="p">,</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</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">DECIMAL</span><span class="p">,</span> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</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">FLOAT</span><span class="p">,</span> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</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">DOUBLE</span><span class="p">,</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a> <span class="p">},</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a> <span class="c1"># DATE < DATETIME < TIMESTAMP < TIMESTAMPTZ < TIMESTAMPLTZ</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</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">TIMESTAMPLTZ</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</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">TIMESTAMPTZ</span><span class="p">:</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">TIMESTAMPLTZ</span><span class="p">},</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</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">TIMESTAMP</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="p">},</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</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">TIMESTAMP</span><span class="p">,</span> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="p">},</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</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">DATE</span><span class="p">:</span> <span class="p">{</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">,</span> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</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">TIMESTAMP</span><span class="p">,</span> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> <span class="p">},</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> <span class="p">}</span> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> <span class="n">TRAVERSABLES</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">annotators</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">annotators</span> <span class="o">=</span> <span class="n">annotators</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">ANNOTATORS</span> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">coerces_to</span> <span class="o">=</span> <span class="n">coerces_to</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">COERCES_TO</span> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a> <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TRAVERSABLES</span><span class="p">):</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="k">continue</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">):</span> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">):</span> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">]</span> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> <span class="n">values</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</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">expressions</span> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">values</span><span class="p">:</span> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a> <span class="k">continue</span> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a> +</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-271"><a href="#L-271"><span class="linenos">271</span></a> <span class="n">alias</span><span class="p">:</span> <span class="n">column</span> +</span><span id="L-272"><a href="#L-272"><span class="linenos">272</span></a> <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span> +</span><span id="L-273"><a href="#L-273"><span class="linenos">273</span></a> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">alias_column_names</span><span class="p">,</span> +</span><span id="L-274"><a href="#L-274"><span class="linenos">274</span></a> <span class="n">values</span><span class="p">,</span> +</span><span id="L-275"><a href="#L-275"><span class="linenos">275</span></a> <span class="p">)</span> +</span><span id="L-276"><a href="#L-276"><span class="linenos">276</span></a> <span class="p">}</span> +</span><span id="L-277"><a href="#L-277"><span class="linenos">277</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-278"><a href="#L-278"><span class="linenos">278</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-279"><a href="#L-279"><span class="linenos">279</span></a> <span class="n">select</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">select</span> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">selects</span> +</span><span id="L-280"><a href="#L-280"><span class="linenos">280</span></a> <span class="p">}</span> +</span><span id="L-281"><a href="#L-281"><span class="linenos">281</span></a> <span class="c1"># First annotate the current scope's column references</span> +</span><span id="L-282"><a href="#L-282"><span class="linenos">282</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="L-283"><a href="#L-283"><span class="linenos">283</span></a> <span class="n">source</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">)</span> +</span><span id="L-284"><a href="#L-284"><span class="linenos">284</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-285"><a href="#L-285"><span class="linenos">285</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="o">.</span><span class="n">get_column_type</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> +</span><span id="L-286"><a href="#L-286"><span class="linenos">286</span></a> <span class="k">elif</span> <span class="n">source</span> <span class="ow">and</span> <span class="n">col</span><span class="o">.</span><span class="n">table</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">:</span> +</span><span id="L-287"><a href="#L-287"><span class="linenos">287</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">][</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-288"><a href="#L-288"><span class="linenos">288</span></a> <span class="c1"># Then (possibly) annotate the remaining expressions in the scope</span> +</span><span id="L-289"><a href="#L-289"><span class="linenos">289</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-290"><a href="#L-290"><span class="linenos">290</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="c1"># This takes care of non-traversable expressions</span> +</span><span id="L-291"><a href="#L-291"><span class="linenos">291</span></a> +</span><span id="L-292"><a href="#L-292"><span class="linenos">292</span></a> <span class="k">def</span> <span class="nf">_maybe_annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-293"><a href="#L-293"><span class="linenos">293</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">):</span> +</span><span id="L-294"><a href="#L-294"><span class="linenos">294</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-295"><a href="#L-295"><span class="linenos">295</span></a> +</span><span id="L-296"><a href="#L-296"><span class="linenos">296</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="p">:</span> +</span><span id="L-297"><a href="#L-297"><span class="linenos">297</span></a> <span class="k">return</span> <span class="n">expression</span> <span class="c1"># We've already inferred the expression's type</span> +</span><span id="L-298"><a href="#L-298"><span class="linenos">298</span></a> +</span><span id="L-299"><a href="#L-299"><span class="linenos">299</span></a> <span class="n">annotator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">annotators</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> +</span><span id="L-300"><a href="#L-300"><span class="linenos">300</span></a> +</span><span id="L-301"><a href="#L-301"><span class="linenos">301</span></a> <span class="k">return</span> <span class="p">(</span> +</span><span id="L-302"><a href="#L-302"><span class="linenos">302</span></a> <span class="n">annotator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span> +</span><span id="L-303"><a href="#L-303"><span class="linenos">303</span></a> <span class="k">if</span> <span class="n">annotator</span> +</span><span id="L-304"><a href="#L-304"><span class="linenos">304</span></a> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</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="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">UNKNOWN</span><span class="p">)</span> +</span><span id="L-305"><a href="#L-305"><span class="linenos">305</span></a> <span class="p">)</span> +</span><span id="L-306"><a href="#L-306"><span class="linenos">306</span></a> +</span><span id="L-307"><a href="#L-307"><span class="linenos">307</span></a> <span class="k">def</span> <span class="nf">_annotate_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-308"><a href="#L-308"><span class="linenos">308</span></a> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-309"><a href="#L-309"><span class="linenos">309</span></a> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">ensure_collection</span><span class="p">(</span><span class="n">value</span><span class="p">):</span> +</span><span id="L-310"><a href="#L-310"><span class="linenos">310</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> +</span><span id="L-311"><a href="#L-311"><span class="linenos">311</span></a> +</span><span id="L-312"><a href="#L-312"><span class="linenos">312</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-313"><a href="#L-313"><span class="linenos">313</span></a> +</span><span id="L-314"><a href="#L-314"><span class="linenos">314</span></a> <span class="k">def</span> <span class="nf">_maybe_coerce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="L-315"><a href="#L-315"><span class="linenos">315</span></a> <span class="c1"># We propagate the NULL / UNKNOWN types upwards if found</span> +</span><span id="L-316"><a href="#L-316"><span class="linenos">316</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">type1</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-317"><a href="#L-317"><span class="linenos">317</span></a> <span class="n">type1</span> <span class="o">=</span> <span class="n">type1</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-318"><a href="#L-318"><span class="linenos">318</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">type2</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-319"><a href="#L-319"><span class="linenos">319</span></a> <span class="n">type2</span> <span class="o">=</span> <span class="n">type2</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-320"><a href="#L-320"><span class="linenos">320</span></a> +</span><span id="L-321"><a href="#L-321"><span class="linenos">321</span></a> <span class="k">if</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">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="L-322"><a href="#L-322"><span class="linenos">322</span></a> <span class="k">return</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">NULL</span> +</span><span id="L-323"><a href="#L-323"><span class="linenos">323</span></a> <span class="k">if</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">UNKNOWN</span> <span class="ow">in</span> <span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="L-324"><a href="#L-324"><span class="linenos">324</span></a> <span class="k">return</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">UNKNOWN</span> +</span><span id="L-325"><a href="#L-325"><span class="linenos">325</span></a> +</span><span id="L-326"><a href="#L-326"><span class="linenos">326</span></a> <span class="k">return</span> <span class="n">type2</span> <span class="k">if</span> <span class="n">type2</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">coerces_to</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="p">{})</span> <span class="k">else</span> <span class="n">type1</span> +</span><span id="L-327"><a href="#L-327"><span class="linenos">327</span></a> +</span><span id="L-328"><a href="#L-328"><span class="linenos">328</span></a> <span class="k">def</span> <span class="nf">_annotate_binary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-329"><a href="#L-329"><span class="linenos">329</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-330"><a href="#L-330"><span class="linenos">330</span></a> +</span><span id="L-331"><a href="#L-331"><span class="linenos">331</span></a> <span class="n">left_type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-332"><a href="#L-332"><span class="linenos">332</span></a> <span class="n">right_type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-333"><a href="#L-333"><span class="linenos">333</span></a> +</span><span id="L-334"><a href="#L-334"><span class="linenos">334</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)):</span> +</span><span id="L-335"><a href="#L-335"><span class="linenos">335</span></a> <span class="k">if</span> <span class="n">left_type</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">NULL</span> <span class="ow">and</span> <span class="n">right_type</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">NULL</span><span class="p">:</span> +</span><span id="L-336"><a href="#L-336"><span class="linenos">336</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">NULL</span> +</span><span id="L-337"><a href="#L-337"><span class="linenos">337</span></a> <span class="k">elif</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">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left_type</span><span class="p">,</span> <span class="n">right_type</span><span class="p">):</span> +</span><span id="L-338"><a href="#L-338"><span class="linenos">338</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span> +</span><span id="L-339"><a href="#L-339"><span class="linenos">339</span></a> <span class="s2">"NULLABLE"</span><span class="p">,</span> <span class="n">expressions</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">"BOOLEAN"</span><span class="p">)</span> +</span><span id="L-340"><a href="#L-340"><span class="linenos">340</span></a> <span class="p">)</span> +</span><span id="L-341"><a href="#L-341"><span class="linenos">341</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-342"><a href="#L-342"><span class="linenos">342</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="L-343"><a href="#L-343"><span class="linenos">343</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)):</span> +</span><span id="L-344"><a href="#L-344"><span class="linenos">344</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="L-345"><a href="#L-345"><span class="linenos">345</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-346"><a href="#L-346"><span class="linenos">346</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_coerce</span><span class="p">(</span><span class="n">left_type</span><span class="p">,</span> <span class="n">right_type</span><span class="p">)</span> +</span><span id="L-347"><a href="#L-347"><span class="linenos">347</span></a> +</span><span id="L-348"><a href="#L-348"><span class="linenos">348</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-349"><a href="#L-349"><span class="linenos">349</span></a> +</span><span id="L-350"><a href="#L-350"><span class="linenos">350</span></a> <span class="k">def</span> <span class="nf">_annotate_unary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-351"><a href="#L-351"><span class="linenos">351</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-352"><a href="#L-352"><span class="linenos">352</span></a> +</span><span id="L-353"><a href="#L-353"><span class="linenos">353</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="L-354"><a href="#L-354"><span class="linenos">354</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="L-355"><a href="#L-355"><span class="linenos">355</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-356"><a href="#L-356"><span class="linenos">356</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-357"><a href="#L-357"><span class="linenos">357</span></a> +</span><span id="L-358"><a href="#L-358"><span class="linenos">358</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-359"><a href="#L-359"><span class="linenos">359</span></a> +</span><span id="L-360"><a href="#L-360"><span class="linenos">360</span></a> <span class="k">def</span> <span class="nf">_annotate_literal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="L-361"><a href="#L-361"><span class="linenos">361</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> +</span><span id="L-362"><a href="#L-362"><span class="linenos">362</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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-363"><a href="#L-363"><span class="linenos">363</span></a> <span class="k">elif</span> <span class="n">expression</span><span class="o">.</span><span class="n">is_int</span><span class="p">:</span> +</span><span id="L-364"><a href="#L-364"><span class="linenos">364</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">INT</span> +</span><span id="L-365"><a href="#L-365"><span class="linenos">365</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-366"><a href="#L-366"><span class="linenos">366</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">DOUBLE</span> +</span><span id="L-367"><a href="#L-367"><span class="linenos">367</span></a> +</span><span id="L-368"><a href="#L-368"><span class="linenos">368</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-369"><a href="#L-369"><span class="linenos">369</span></a> +</span><span id="L-370"><a href="#L-370"><span class="linenos">370</span></a> <span class="k">def</span> <span class="nf">_annotate_with_type</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">target_type</span><span class="p">):</span> +</span><span id="L-371"><a href="#L-371"><span class="linenos">371</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">target_type</span> +</span><span id="L-372"><a href="#L-372"><span class="linenos">372</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-373"><a href="#L-373"><span class="linenos">373</span></a> +</span><span id="L-374"><a href="#L-374"><span class="linenos">374</span></a> <span class="k">def</span> <span class="nf">_annotate_by_args</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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">promote</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-375"><a href="#L-375"><span class="linenos">375</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-376"><a href="#L-376"><span class="linenos">376</span></a> <span class="n">expressions</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-377"><a href="#L-377"><span class="linenos">377</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span> +</span><span id="L-378"><a href="#L-378"><span class="linenos">378</span></a> <span class="n">arg_expr</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="n">arg</span><span class="p">)</span> +</span><span id="L-379"><a href="#L-379"><span class="linenos">379</span></a> <span class="n">expressions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">expr</span> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">ensure_list</span><span class="p">(</span><span class="n">arg_expr</span><span class="p">)</span> <span class="k">if</span> <span class="n">expr</span><span class="p">)</span> +</span><span id="L-380"><a href="#L-380"><span class="linenos">380</span></a> +</span><span id="L-381"><a href="#L-381"><span class="linenos">381</span></a> <span class="n">last_datatype</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-382"><a href="#L-382"><span class="linenos">382</span></a> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">expressions</span><span class="p">:</span> +</span><span id="L-383"><a href="#L-383"><span class="linenos">383</span></a> <span class="n">last_datatype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_coerce</span><span class="p">(</span><span class="n">last_datatype</span> <span class="ow">or</span> <span class="n">expr</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">type</span><span class="p">)</span> +</span><span id="L-384"><a href="#L-384"><span class="linenos">384</span></a> +</span><span id="L-385"><a href="#L-385"><span class="linenos">385</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">last_datatype</span> <span class="ow">or</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">UNKNOWN</span> +</span><span id="L-386"><a href="#L-386"><span class="linenos">386</span></a> +</span><span id="L-387"><a href="#L-387"><span class="linenos">387</span></a> <span class="k">if</span> <span class="n">promote</span><span class="p">:</span> +</span><span id="L-388"><a href="#L-388"><span class="linenos">388</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">INTEGER_TYPES</span><span class="p">:</span> +</span><span id="L-389"><a href="#L-389"><span class="linenos">389</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BIGINT</span> +</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a> <span class="k">elif</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">FLOAT_TYPES</span><span class="p">:</span> +</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">DOUBLE</span> +</span><span id="L-392"><a href="#L-392"><span class="linenos">392</span></a> +</span><span id="L-393"><a href="#L-393"><span class="linenos">393</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="annotate_types"> + <input id="annotate_types-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">annotate_types</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">schema</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">annotators</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="annotate_types-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#annotate_types"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="annotate_types-8"><a href="#annotate_types-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">annotate_types</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">annotators</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="annotate_types-9"><a href="#annotate_types-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="annotate_types-10"><a href="#annotate_types-10"><span class="linenos">10</span></a><span class="sd"> Recursively infer & annotate types in an expression syntax tree against a schema.</span> +</span><span id="annotate_types-11"><a href="#annotate_types-11"><span class="linenos">11</span></a><span class="sd"> Assumes that we've already executed the optimizer's qualify_columns step.</span> +</span><span id="annotate_types-12"><a href="#annotate_types-12"><span class="linenos">12</span></a> +</span><span id="annotate_types-13"><a href="#annotate_types-13"><span class="linenos">13</span></a><span class="sd"> Example:</span> +</span><span id="annotate_types-14"><a href="#annotate_types-14"><span class="linenos">14</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="annotate_types-15"><a href="#annotate_types-15"><span class="linenos">15</span></a><span class="sd"> >>> schema = {"y": {"cola": "SMALLINT"}}</span> +</span><span id="annotate_types-16"><a href="#annotate_types-16"><span class="linenos">16</span></a><span class="sd"> >>> sql = "SELECT x.cola + 2.5 AS cola FROM (SELECT y.cola AS cola FROM y AS y) AS x"</span> +</span><span id="annotate_types-17"><a href="#annotate_types-17"><span class="linenos">17</span></a><span class="sd"> >>> annotated_expr = annotate_types(sqlglot.parse_one(sql), schema=schema)</span> +</span><span id="annotate_types-18"><a href="#annotate_types-18"><span class="linenos">18</span></a><span class="sd"> >>> annotated_expr.expressions[0].type.this # Get the type of "x.cola + 2.5 AS cola"</span> +</span><span id="annotate_types-19"><a href="#annotate_types-19"><span class="linenos">19</span></a><span class="sd"> <Type.DOUBLE: 'DOUBLE'></span> +</span><span id="annotate_types-20"><a href="#annotate_types-20"><span class="linenos">20</span></a> +</span><span id="annotate_types-21"><a href="#annotate_types-21"><span class="linenos">21</span></a><span class="sd"> Args:</span> +</span><span id="annotate_types-22"><a href="#annotate_types-22"><span class="linenos">22</span></a><span class="sd"> expression (sqlglot.Expression): Expression to annotate.</span> +</span><span id="annotate_types-23"><a href="#annotate_types-23"><span class="linenos">23</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): Database schema.</span> +</span><span id="annotate_types-24"><a href="#annotate_types-24"><span class="linenos">24</span></a><span class="sd"> annotators (dict): Maps expression type to corresponding annotation function.</span> +</span><span id="annotate_types-25"><a href="#annotate_types-25"><span class="linenos">25</span></a><span class="sd"> coerces_to (dict): Maps expression type to set of types that it can be coerced into.</span> +</span><span id="annotate_types-26"><a href="#annotate_types-26"><span class="linenos">26</span></a><span class="sd"> Returns:</span> +</span><span id="annotate_types-27"><a href="#annotate_types-27"><span class="linenos">27</span></a><span class="sd"> sqlglot.Expression: expression annotated with types</span> +</span><span id="annotate_types-28"><a href="#annotate_types-28"><span class="linenos">28</span></a><span class="sd"> """</span> +</span><span id="annotate_types-29"><a href="#annotate_types-29"><span class="linenos">29</span></a> +</span><span id="annotate_types-30"><a href="#annotate_types-30"><span class="linenos">30</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="annotate_types-31"><a href="#annotate_types-31"><span class="linenos">31</span></a> +</span><span id="annotate_types-32"><a href="#annotate_types-32"><span class="linenos">32</span></a> <span class="k">return</span> <span class="n">TypeAnnotator</span><span class="p">(</span><span class="n">schema</span><span class="p">,</span> <span class="n">annotators</span><span class="p">,</span> <span class="n">coerces_to</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>Recursively infer & annotate types in an expression syntax tree against a schema. +Assumes that we've already executed the optimizer's qualify_columns step.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">schema</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"y"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"cola"</span><span class="p">:</span> <span class="s2">"SMALLINT"</span><span class="p">}}</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"SELECT x.cola + 2.5 AS cola FROM (SELECT y.cola AS cola FROM y AS y) AS x"</span> +<span class="gp">>>> </span><span class="n">annotated_expr</span> <span class="o">=</span> <span class="n">annotate_types</span><span class="p">(</span><span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">),</span> <span class="n">schema</span><span class="o">=</span><span class="n">schema</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">annotated_expr</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">type</span><span class="o">.</span><span class="n">this</span> <span class="c1"># Get the type of "x.cola + 2.5 AS cola"</span> +<span class="go"><Type.DOUBLE: 'DOUBLE'></span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> Expression to annotate.</li> +<li><strong>schema (dict|sqlglot.optimizer.Schema):</strong> Database schema.</li> +<li><strong>annotators (dict):</strong> Maps expression type to corresponding annotation function.</li> +<li><strong>coerces_to (dict):</strong> Maps expression type to set of types that it can be coerced into.</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: expression annotated with types</p> +</blockquote> +</div> + + + </section> + <section id="TypeAnnotator"> + <input id="TypeAnnotator-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr class"> + + <span class="def">class</span> + <span class="name">TypeAnnotator</span>: + + <label class="view-source-button" for="TypeAnnotator-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#TypeAnnotator"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="TypeAnnotator-35"><a href="#TypeAnnotator-35"><span class="linenos"> 35</span></a><span class="k">class</span> <span class="nc">TypeAnnotator</span><span class="p">:</span> +</span><span id="TypeAnnotator-36"><a href="#TypeAnnotator-36"><span class="linenos"> 36</span></a> <span class="n">ANNOTATORS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator-37"><a href="#TypeAnnotator-37"><span class="linenos"> 37</span></a> <span class="o">**</span><span class="p">{</span> +</span><span id="TypeAnnotator-38"><a href="#TypeAnnotator-38"><span class="linenos"> 38</span></a> <span class="n">expr_type</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_unary</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span> +</span><span id="TypeAnnotator-39"><a href="#TypeAnnotator-39"><span class="linenos"> 39</span></a> <span class="k">for</span> <span class="n">expr_type</span> <span class="ow">in</span> <span class="n">subclasses</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Unary</span><span class="p">)</span> +</span><span id="TypeAnnotator-40"><a href="#TypeAnnotator-40"><span class="linenos"> 40</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-41"><a href="#TypeAnnotator-41"><span class="linenos"> 41</span></a> <span class="o">**</span><span class="p">{</span> +</span><span id="TypeAnnotator-42"><a href="#TypeAnnotator-42"><span class="linenos"> 42</span></a> <span class="n">expr_type</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_binary</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span> +</span><span id="TypeAnnotator-43"><a href="#TypeAnnotator-43"><span class="linenos"> 43</span></a> <span class="k">for</span> <span class="n">expr_type</span> <span class="ow">in</span> <span class="n">subclasses</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="TypeAnnotator-44"><a href="#TypeAnnotator-44"><span class="linenos"> 44</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-45"><a href="#TypeAnnotator-45"><span class="linenos"> 45</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]),</span> +</span><span id="TypeAnnotator-46"><a href="#TypeAnnotator-46"><span class="linenos"> 46</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TryCast</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]),</span> +</span><span id="TypeAnnotator-47"><a href="#TypeAnnotator-47"><span class="linenos"> 47</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> +</span><span id="TypeAnnotator-48"><a href="#TypeAnnotator-48"><span class="linenos"> 48</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_unary</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> +</span><span id="TypeAnnotator-49"><a href="#TypeAnnotator-49"><span class="linenos"> 49</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="TypeAnnotator-50"><a href="#TypeAnnotator-50"><span class="linenos"> 50</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="TypeAnnotator-51"><a href="#TypeAnnotator-51"><span class="linenos"> 51</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_literal</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> +</span><span id="TypeAnnotator-52"><a href="#TypeAnnotator-52"><span class="linenos"> 52</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BOOLEAN</span><span class="p">),</span> +</span><span id="TypeAnnotator-53"><a href="#TypeAnnotator-53"><span class="linenos"> 53</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">NULL</span><span class="p">),</span> +</span><span id="TypeAnnotator-54"><a href="#TypeAnnotator-54"><span class="linenos"> 54</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Anonymous</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">UNKNOWN</span><span class="p">),</span> +</span><span id="TypeAnnotator-55"><a href="#TypeAnnotator-55"><span class="linenos"> 55</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ApproxDistinct</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-56"><a href="#TypeAnnotator-56"><span class="linenos"> 56</span></a> <span class="n">expr</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">BIGINT</span> +</span><span id="TypeAnnotator-57"><a href="#TypeAnnotator-57"><span class="linenos"> 57</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-58"><a href="#TypeAnnotator-58"><span class="linenos"> 58</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Avg</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-59"><a href="#TypeAnnotator-59"><span class="linenos"> 59</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Min</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">),</span> +</span><span id="TypeAnnotator-60"><a href="#TypeAnnotator-60"><span class="linenos"> 60</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">),</span> +</span><span id="TypeAnnotator-61"><a href="#TypeAnnotator-61"><span class="linenos"> 61</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Sum</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="n">promote</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> +</span><span id="TypeAnnotator-62"><a href="#TypeAnnotator-62"><span class="linenos"> 62</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Ceil</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-63"><a href="#TypeAnnotator-63"><span class="linenos"> 63</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BIGINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-64"><a href="#TypeAnnotator-64"><span class="linenos"> 64</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-65"><a href="#TypeAnnotator-65"><span class="linenos"> 65</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentDatetime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-66"><a href="#TypeAnnotator-66"><span class="linenos"> 66</span></a> <span class="n">expr</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">DATETIME</span> +</span><span id="TypeAnnotator-67"><a href="#TypeAnnotator-67"><span class="linenos"> 67</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-68"><a href="#TypeAnnotator-68"><span class="linenos"> 68</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-69"><a href="#TypeAnnotator-69"><span class="linenos"> 69</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-70"><a href="#TypeAnnotator-70"><span class="linenos"> 70</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-71"><a href="#TypeAnnotator-71"><span class="linenos"> 71</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">CurrentTimestamp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-72"><a href="#TypeAnnotator-72"><span class="linenos"> 72</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-73"><a href="#TypeAnnotator-73"><span class="linenos"> 73</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-74"><a href="#TypeAnnotator-74"><span class="linenos"> 74</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">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-75"><a href="#TypeAnnotator-75"><span class="linenos"> 75</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-76"><a href="#TypeAnnotator-76"><span class="linenos"> 76</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">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-77"><a href="#TypeAnnotator-77"><span class="linenos"> 77</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DatetimeAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-78"><a href="#TypeAnnotator-78"><span class="linenos"> 78</span></a> <span class="n">expr</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">DATETIME</span> +</span><span id="TypeAnnotator-79"><a href="#TypeAnnotator-79"><span class="linenos"> 79</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-80"><a href="#TypeAnnotator-80"><span class="linenos"> 80</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DatetimeSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-81"><a href="#TypeAnnotator-81"><span class="linenos"> 81</span></a> <span class="n">expr</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">DATETIME</span> +</span><span id="TypeAnnotator-82"><a href="#TypeAnnotator-82"><span class="linenos"> 82</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-83"><a href="#TypeAnnotator-83"><span class="linenos"> 83</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DatetimeDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-84"><a href="#TypeAnnotator-84"><span class="linenos"> 84</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Extract</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-85"><a href="#TypeAnnotator-85"><span class="linenos"> 85</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimestampAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-86"><a href="#TypeAnnotator-86"><span class="linenos"> 86</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-87"><a href="#TypeAnnotator-87"><span class="linenos"> 87</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-88"><a href="#TypeAnnotator-88"><span class="linenos"> 88</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimestampSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-89"><a href="#TypeAnnotator-89"><span class="linenos"> 89</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-90"><a href="#TypeAnnotator-90"><span class="linenos"> 90</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-91"><a href="#TypeAnnotator-91"><span class="linenos"> 91</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimestampDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-92"><a href="#TypeAnnotator-92"><span class="linenos"> 92</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeAdd</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TIMESTAMP</span><span class="p">),</span> +</span><span id="TypeAnnotator-93"><a href="#TypeAnnotator-93"><span class="linenos"> 93</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeSub</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TIMESTAMP</span><span class="p">),</span> +</span><span id="TypeAnnotator-94"><a href="#TypeAnnotator-94"><span class="linenos"> 94</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeDiff</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-95"><a href="#TypeAnnotator-95"><span class="linenos"> 95</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateStrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-96"><a href="#TypeAnnotator-96"><span class="linenos"> 96</span></a> <span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="TypeAnnotator-97"><a href="#TypeAnnotator-97"><span class="linenos"> 97</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-98"><a href="#TypeAnnotator-98"><span class="linenos"> 98</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateToDateStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-99"><a href="#TypeAnnotator-99"><span class="linenos"> 99</span></a> <span class="n">expr</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><span id="TypeAnnotator-100"><a href="#TypeAnnotator-100"><span class="linenos">100</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-101"><a href="#TypeAnnotator-101"><span class="linenos">101</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DateToDi</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-102"><a href="#TypeAnnotator-102"><span class="linenos">102</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Day</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-103"><a href="#TypeAnnotator-103"><span class="linenos">103</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DiToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-104"><a href="#TypeAnnotator-104"><span class="linenos">104</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Exp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-105"><a href="#TypeAnnotator-105"><span class="linenos">105</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Floor</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-106"><a href="#TypeAnnotator-106"><span class="linenos">106</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Case</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"default"</span><span class="p">,</span> <span class="s2">"ifs"</span><span class="p">),</span> +</span><span id="TypeAnnotator-107"><a href="#TypeAnnotator-107"><span class="linenos">107</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">If</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"true"</span><span class="p">,</span> <span class="s2">"false"</span><span class="p">),</span> +</span><span id="TypeAnnotator-108"><a href="#TypeAnnotator-108"><span class="linenos">108</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expressions"</span><span class="p">),</span> +</span><span id="TypeAnnotator-109"><a href="#TypeAnnotator-109"><span class="linenos">109</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">IfNull</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_by_args</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expression"</span><span class="p">),</span> +</span><span id="TypeAnnotator-110"><a href="#TypeAnnotator-110"><span class="linenos">110</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ConcatWs</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-111"><a href="#TypeAnnotator-111"><span class="linenos">111</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GroupConcat</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-112"><a href="#TypeAnnotator-112"><span class="linenos">112</span></a> <span class="n">expr</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><span id="TypeAnnotator-113"><a href="#TypeAnnotator-113"><span class="linenos">113</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-114"><a href="#TypeAnnotator-114"><span class="linenos">114</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ArrayConcat</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-115"><a href="#TypeAnnotator-115"><span class="linenos">115</span></a> <span class="n">expr</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><span id="TypeAnnotator-116"><a href="#TypeAnnotator-116"><span class="linenos">116</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-117"><a href="#TypeAnnotator-117"><span class="linenos">117</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Initcap</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-118"><a href="#TypeAnnotator-118"><span class="linenos">118</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Length</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">BIGINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-119"><a href="#TypeAnnotator-119"><span class="linenos">119</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Levenshtein</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-120"><a href="#TypeAnnotator-120"><span class="linenos">120</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Ln</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-121"><a href="#TypeAnnotator-121"><span class="linenos">121</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-122"><a href="#TypeAnnotator-122"><span class="linenos">122</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log2</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-123"><a href="#TypeAnnotator-123"><span class="linenos">123</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Log10</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-124"><a href="#TypeAnnotator-124"><span class="linenos">124</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Lower</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-125"><a href="#TypeAnnotator-125"><span class="linenos">125</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Month</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-126"><a href="#TypeAnnotator-126"><span class="linenos">126</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Pow</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-127"><a href="#TypeAnnotator-127"><span class="linenos">127</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Quantile</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-128"><a href="#TypeAnnotator-128"><span class="linenos">128</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">ApproxQuantile</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-129"><a href="#TypeAnnotator-129"><span class="linenos">129</span></a> <span class="n">expr</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">DOUBLE</span> +</span><span id="TypeAnnotator-130"><a href="#TypeAnnotator-130"><span class="linenos">130</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-131"><a href="#TypeAnnotator-131"><span class="linenos">131</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">RegexpLike</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-132"><a href="#TypeAnnotator-132"><span class="linenos">132</span></a> <span class="n">expr</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">BOOLEAN</span> +</span><span id="TypeAnnotator-133"><a href="#TypeAnnotator-133"><span class="linenos">133</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-134"><a href="#TypeAnnotator-134"><span class="linenos">134</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Round</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-135"><a href="#TypeAnnotator-135"><span class="linenos">135</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">SafeDivide</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-136"><a href="#TypeAnnotator-136"><span class="linenos">136</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Substring</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-137"><a href="#TypeAnnotator-137"><span class="linenos">137</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrPosition</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-138"><a href="#TypeAnnotator-138"><span class="linenos">138</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-139"><a href="#TypeAnnotator-139"><span class="linenos">139</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StrToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-140"><a href="#TypeAnnotator-140"><span class="linenos">140</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-141"><a href="#TypeAnnotator-141"><span class="linenos">141</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-142"><a href="#TypeAnnotator-142"><span class="linenos">142</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Sqrt</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-143"><a href="#TypeAnnotator-143"><span class="linenos">143</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Stddev</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-144"><a href="#TypeAnnotator-144"><span class="linenos">144</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StddevPop</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-145"><a href="#TypeAnnotator-145"><span class="linenos">145</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">StddevSamp</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-146"><a href="#TypeAnnotator-146"><span class="linenos">146</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeToStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-147"><a href="#TypeAnnotator-147"><span class="linenos">147</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeToTimeStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-148"><a href="#TypeAnnotator-148"><span class="linenos">148</span></a> <span class="n">expr</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><span id="TypeAnnotator-149"><a href="#TypeAnnotator-149"><span class="linenos">149</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-150"><a href="#TypeAnnotator-150"><span class="linenos">150</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeStrToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-151"><a href="#TypeAnnotator-151"><span class="linenos">151</span></a> <span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="TypeAnnotator-152"><a href="#TypeAnnotator-152"><span class="linenos">152</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-153"><a href="#TypeAnnotator-153"><span class="linenos">153</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TimeStrToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-154"><a href="#TypeAnnotator-154"><span class="linenos">154</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-155"><a href="#TypeAnnotator-155"><span class="linenos">155</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-156"><a href="#TypeAnnotator-156"><span class="linenos">156</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Trim</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-157"><a href="#TypeAnnotator-157"><span class="linenos">157</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDateStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-158"><a href="#TypeAnnotator-158"><span class="linenos">158</span></a> <span class="n">expr</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><span id="TypeAnnotator-159"><a href="#TypeAnnotator-159"><span class="linenos">159</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-160"><a href="#TypeAnnotator-160"><span class="linenos">160</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDsToDate</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">),</span> +</span><span id="TypeAnnotator-161"><a href="#TypeAnnotator-161"><span class="linenos">161</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">TsOrDiToDi</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">INT</span><span class="p">),</span> +</span><span id="TypeAnnotator-162"><a href="#TypeAnnotator-162"><span class="linenos">162</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-163"><a href="#TypeAnnotator-163"><span class="linenos">163</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToTime</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-164"><a href="#TypeAnnotator-164"><span class="linenos">164</span></a> <span class="n">expr</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">TIMESTAMP</span> +</span><span id="TypeAnnotator-165"><a href="#TypeAnnotator-165"><span class="linenos">165</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-166"><a href="#TypeAnnotator-166"><span class="linenos">166</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">UnixToTimeStr</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-167"><a href="#TypeAnnotator-167"><span class="linenos">167</span></a> <span class="n">expr</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><span id="TypeAnnotator-168"><a href="#TypeAnnotator-168"><span class="linenos">168</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-169"><a href="#TypeAnnotator-169"><span class="linenos">169</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Upper</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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="TypeAnnotator-170"><a href="#TypeAnnotator-170"><span class="linenos">170</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Variance</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">DOUBLE</span><span class="p">),</span> +</span><span id="TypeAnnotator-171"><a href="#TypeAnnotator-171"><span class="linenos">171</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">VariancePop</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span> +</span><span id="TypeAnnotator-172"><a href="#TypeAnnotator-172"><span class="linenos">172</span></a> <span class="n">expr</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">DOUBLE</span> +</span><span id="TypeAnnotator-173"><a href="#TypeAnnotator-173"><span class="linenos">173</span></a> <span class="p">),</span> +</span><span id="TypeAnnotator-174"><a href="#TypeAnnotator-174"><span class="linenos">174</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Week</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-175"><a href="#TypeAnnotator-175"><span class="linenos">175</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Year</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">expr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</span><span class="p">(</span><span class="n">expr</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">TINYINT</span><span class="p">),</span> +</span><span id="TypeAnnotator-176"><a href="#TypeAnnotator-176"><span class="linenos">176</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator-177"><a href="#TypeAnnotator-177"><span class="linenos">177</span></a> +</span><span id="TypeAnnotator-178"><a href="#TypeAnnotator-178"><span class="linenos">178</span></a> <span class="c1"># Reference: https://spark.apache.org/docs/3.2.0/sql-ref-ansi-compliance.html</span> +</span><span id="TypeAnnotator-179"><a href="#TypeAnnotator-179"><span class="linenos">179</span></a> <span class="n">COERCES_TO</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator-180"><a href="#TypeAnnotator-180"><span class="linenos">180</span></a> <span class="c1"># CHAR < NCHAR < VARCHAR < NVARCHAR < TEXT</span> +</span><span id="TypeAnnotator-181"><a href="#TypeAnnotator-181"><span class="linenos">181</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">TEXT</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="TypeAnnotator-182"><a href="#TypeAnnotator-182"><span class="linenos">182</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">NVARCHAR</span><span class="p">:</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">TEXT</span><span class="p">},</span> +</span><span id="TypeAnnotator-183"><a href="#TypeAnnotator-183"><span class="linenos">183</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">VARCHAR</span><span class="p">:</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">NVARCHAR</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">TEXT</span><span class="p">},</span> +</span><span id="TypeAnnotator-184"><a href="#TypeAnnotator-184"><span class="linenos">184</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">NCHAR</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-185"><a href="#TypeAnnotator-185"><span class="linenos">185</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">VARCHAR</span><span class="p">,</span> +</span><span id="TypeAnnotator-186"><a href="#TypeAnnotator-186"><span class="linenos">186</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">NVARCHAR</span><span class="p">,</span> +</span><span id="TypeAnnotator-187"><a href="#TypeAnnotator-187"><span class="linenos">187</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">TEXT</span><span class="p">,</span> +</span><span id="TypeAnnotator-188"><a href="#TypeAnnotator-188"><span class="linenos">188</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-189"><a href="#TypeAnnotator-189"><span class="linenos">189</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">CHAR</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-190"><a href="#TypeAnnotator-190"><span class="linenos">190</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">NCHAR</span><span class="p">,</span> +</span><span id="TypeAnnotator-191"><a href="#TypeAnnotator-191"><span class="linenos">191</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">VARCHAR</span><span class="p">,</span> +</span><span id="TypeAnnotator-192"><a href="#TypeAnnotator-192"><span class="linenos">192</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">NVARCHAR</span><span class="p">,</span> +</span><span id="TypeAnnotator-193"><a href="#TypeAnnotator-193"><span class="linenos">193</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">TEXT</span><span class="p">,</span> +</span><span id="TypeAnnotator-194"><a href="#TypeAnnotator-194"><span class="linenos">194</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-195"><a href="#TypeAnnotator-195"><span class="linenos">195</span></a> <span class="c1"># TINYINT < SMALLINT < INT < BIGINT < DECIMAL < FLOAT < DOUBLE</span> +</span><span id="TypeAnnotator-196"><a href="#TypeAnnotator-196"><span class="linenos">196</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">DOUBLE</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="TypeAnnotator-197"><a href="#TypeAnnotator-197"><span class="linenos">197</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">FLOAT</span><span class="p">:</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">DOUBLE</span><span class="p">},</span> +</span><span id="TypeAnnotator-198"><a href="#TypeAnnotator-198"><span class="linenos">198</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">DECIMAL</span><span class="p">:</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">FLOAT</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">DOUBLE</span><span class="p">},</span> +</span><span id="TypeAnnotator-199"><a href="#TypeAnnotator-199"><span class="linenos">199</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">BIGINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-200"><a href="#TypeAnnotator-200"><span class="linenos">200</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">DECIMAL</span><span class="p">,</span> +</span><span id="TypeAnnotator-201"><a href="#TypeAnnotator-201"><span class="linenos">201</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">FLOAT</span><span class="p">,</span> +</span><span id="TypeAnnotator-202"><a href="#TypeAnnotator-202"><span class="linenos">202</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">DOUBLE</span><span class="p">,</span> +</span><span id="TypeAnnotator-203"><a href="#TypeAnnotator-203"><span class="linenos">203</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-204"><a href="#TypeAnnotator-204"><span class="linenos">204</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="p">{</span> +</span><span id="TypeAnnotator-205"><a href="#TypeAnnotator-205"><span class="linenos">205</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">BIGINT</span><span class="p">,</span> +</span><span id="TypeAnnotator-206"><a href="#TypeAnnotator-206"><span class="linenos">206</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">DECIMAL</span><span class="p">,</span> +</span><span id="TypeAnnotator-207"><a href="#TypeAnnotator-207"><span class="linenos">207</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">FLOAT</span><span class="p">,</span> +</span><span id="TypeAnnotator-208"><a href="#TypeAnnotator-208"><span class="linenos">208</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">DOUBLE</span><span class="p">,</span> +</span><span id="TypeAnnotator-209"><a href="#TypeAnnotator-209"><span class="linenos">209</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-210"><a href="#TypeAnnotator-210"><span class="linenos">210</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">SMALLINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-211"><a href="#TypeAnnotator-211"><span class="linenos">211</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><span id="TypeAnnotator-212"><a href="#TypeAnnotator-212"><span class="linenos">212</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">BIGINT</span><span class="p">,</span> +</span><span id="TypeAnnotator-213"><a href="#TypeAnnotator-213"><span class="linenos">213</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">DECIMAL</span><span class="p">,</span> +</span><span id="TypeAnnotator-214"><a href="#TypeAnnotator-214"><span class="linenos">214</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">FLOAT</span><span class="p">,</span> +</span><span id="TypeAnnotator-215"><a href="#TypeAnnotator-215"><span class="linenos">215</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">DOUBLE</span><span class="p">,</span> +</span><span id="TypeAnnotator-216"><a href="#TypeAnnotator-216"><span class="linenos">216</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-217"><a href="#TypeAnnotator-217"><span class="linenos">217</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">TINYINT</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-218"><a href="#TypeAnnotator-218"><span class="linenos">218</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">SMALLINT</span><span class="p">,</span> +</span><span id="TypeAnnotator-219"><a href="#TypeAnnotator-219"><span class="linenos">219</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><span id="TypeAnnotator-220"><a href="#TypeAnnotator-220"><span class="linenos">220</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">BIGINT</span><span class="p">,</span> +</span><span id="TypeAnnotator-221"><a href="#TypeAnnotator-221"><span class="linenos">221</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">DECIMAL</span><span class="p">,</span> +</span><span id="TypeAnnotator-222"><a href="#TypeAnnotator-222"><span class="linenos">222</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">FLOAT</span><span class="p">,</span> +</span><span id="TypeAnnotator-223"><a href="#TypeAnnotator-223"><span class="linenos">223</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">DOUBLE</span><span class="p">,</span> +</span><span id="TypeAnnotator-224"><a href="#TypeAnnotator-224"><span class="linenos">224</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-225"><a href="#TypeAnnotator-225"><span class="linenos">225</span></a> <span class="c1"># DATE < DATETIME < TIMESTAMP < TIMESTAMPTZ < TIMESTAMPLTZ</span> +</span><span id="TypeAnnotator-226"><a href="#TypeAnnotator-226"><span class="linenos">226</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">TIMESTAMPLTZ</span><span class="p">:</span> <span class="nb">set</span><span class="p">(),</span> +</span><span id="TypeAnnotator-227"><a href="#TypeAnnotator-227"><span class="linenos">227</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">TIMESTAMPTZ</span><span class="p">:</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">TIMESTAMPLTZ</span><span class="p">},</span> +</span><span id="TypeAnnotator-228"><a href="#TypeAnnotator-228"><span class="linenos">228</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">TIMESTAMP</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-229"><a href="#TypeAnnotator-229"><span class="linenos">229</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-230"><a href="#TypeAnnotator-230"><span class="linenos">230</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-231"><a href="#TypeAnnotator-231"><span class="linenos">231</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-232"><a href="#TypeAnnotator-232"><span class="linenos">232</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-233"><a href="#TypeAnnotator-233"><span class="linenos">233</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">TIMESTAMP</span><span class="p">,</span> +</span><span id="TypeAnnotator-234"><a href="#TypeAnnotator-234"><span class="linenos">234</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-235"><a href="#TypeAnnotator-235"><span class="linenos">235</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-236"><a href="#TypeAnnotator-236"><span class="linenos">236</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-237"><a href="#TypeAnnotator-237"><span class="linenos">237</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">DATE</span><span class="p">:</span> <span class="p">{</span> +</span><span id="TypeAnnotator-238"><a href="#TypeAnnotator-238"><span class="linenos">238</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">,</span> +</span><span id="TypeAnnotator-239"><a href="#TypeAnnotator-239"><span class="linenos">239</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">TIMESTAMP</span><span class="p">,</span> +</span><span id="TypeAnnotator-240"><a href="#TypeAnnotator-240"><span class="linenos">240</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">TIMESTAMPTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-241"><a href="#TypeAnnotator-241"><span class="linenos">241</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">TIMESTAMPLTZ</span><span class="p">,</span> +</span><span id="TypeAnnotator-242"><a href="#TypeAnnotator-242"><span class="linenos">242</span></a> <span class="p">},</span> +</span><span id="TypeAnnotator-243"><a href="#TypeAnnotator-243"><span class="linenos">243</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator-244"><a href="#TypeAnnotator-244"><span class="linenos">244</span></a> +</span><span id="TypeAnnotator-245"><a href="#TypeAnnotator-245"><span class="linenos">245</span></a> <span class="n">TRAVERSABLES</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> +</span><span id="TypeAnnotator-246"><a href="#TypeAnnotator-246"><span class="linenos">246</span></a> +</span><span id="TypeAnnotator-247"><a href="#TypeAnnotator-247"><span class="linenos">247</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">annotators</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="TypeAnnotator-248"><a href="#TypeAnnotator-248"><span class="linenos">248</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span> +</span><span id="TypeAnnotator-249"><a href="#TypeAnnotator-249"><span class="linenos">249</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">annotators</span> <span class="o">=</span> <span class="n">annotators</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">ANNOTATORS</span> +</span><span id="TypeAnnotator-250"><a href="#TypeAnnotator-250"><span class="linenos">250</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">coerces_to</span> <span class="o">=</span> <span class="n">coerces_to</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">COERCES_TO</span> +</span><span id="TypeAnnotator-251"><a href="#TypeAnnotator-251"><span class="linenos">251</span></a> +</span><span id="TypeAnnotator-252"><a href="#TypeAnnotator-252"><span class="linenos">252</span></a> <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-253"><a href="#TypeAnnotator-253"><span class="linenos">253</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TRAVERSABLES</span><span class="p">):</span> +</span><span id="TypeAnnotator-254"><a href="#TypeAnnotator-254"><span class="linenos">254</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-255"><a href="#TypeAnnotator-255"><span class="linenos">255</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="TypeAnnotator-256"><a href="#TypeAnnotator-256"><span class="linenos">256</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="TypeAnnotator-257"><a href="#TypeAnnotator-257"><span class="linenos">257</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="TypeAnnotator-258"><a href="#TypeAnnotator-258"><span class="linenos">258</span></a> <span class="k">continue</span> +</span><span id="TypeAnnotator-259"><a href="#TypeAnnotator-259"><span class="linenos">259</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="TypeAnnotator-260"><a href="#TypeAnnotator-260"><span class="linenos">260</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="TypeAnnotator-261"><a href="#TypeAnnotator-261"><span class="linenos">261</span></a> +</span><span id="TypeAnnotator-262"><a href="#TypeAnnotator-262"><span class="linenos">262</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">):</span> +</span><span id="TypeAnnotator-263"><a href="#TypeAnnotator-263"><span class="linenos">263</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">):</span> +</span><span id="TypeAnnotator-264"><a href="#TypeAnnotator-264"><span class="linenos">264</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">]</span> +</span><span id="TypeAnnotator-265"><a href="#TypeAnnotator-265"><span class="linenos">265</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-266"><a href="#TypeAnnotator-266"><span class="linenos">266</span></a> <span class="n">values</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</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">expressions</span> +</span><span id="TypeAnnotator-267"><a href="#TypeAnnotator-267"><span class="linenos">267</span></a> +</span><span id="TypeAnnotator-268"><a href="#TypeAnnotator-268"><span class="linenos">268</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">values</span><span class="p">:</span> +</span><span id="TypeAnnotator-269"><a href="#TypeAnnotator-269"><span class="linenos">269</span></a> <span class="k">continue</span> +</span><span id="TypeAnnotator-270"><a href="#TypeAnnotator-270"><span class="linenos">270</span></a> +</span><span id="TypeAnnotator-271"><a href="#TypeAnnotator-271"><span class="linenos">271</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator-272"><a href="#TypeAnnotator-272"><span class="linenos">272</span></a> <span class="n">alias</span><span class="p">:</span> <span class="n">column</span> +</span><span id="TypeAnnotator-273"><a href="#TypeAnnotator-273"><span class="linenos">273</span></a> <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span> +</span><span id="TypeAnnotator-274"><a href="#TypeAnnotator-274"><span class="linenos">274</span></a> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">alias_column_names</span><span class="p">,</span> +</span><span id="TypeAnnotator-275"><a href="#TypeAnnotator-275"><span class="linenos">275</span></a> <span class="n">values</span><span class="p">,</span> +</span><span id="TypeAnnotator-276"><a href="#TypeAnnotator-276"><span class="linenos">276</span></a> <span class="p">)</span> +</span><span id="TypeAnnotator-277"><a href="#TypeAnnotator-277"><span class="linenos">277</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator-278"><a href="#TypeAnnotator-278"><span class="linenos">278</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-279"><a href="#TypeAnnotator-279"><span class="linenos">279</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator-280"><a href="#TypeAnnotator-280"><span class="linenos">280</span></a> <span class="n">select</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">select</span> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">selects</span> +</span><span id="TypeAnnotator-281"><a href="#TypeAnnotator-281"><span class="linenos">281</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator-282"><a href="#TypeAnnotator-282"><span class="linenos">282</span></a> <span class="c1"># First annotate the current scope's column references</span> +</span><span id="TypeAnnotator-283"><a href="#TypeAnnotator-283"><span class="linenos">283</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="TypeAnnotator-284"><a href="#TypeAnnotator-284"><span class="linenos">284</span></a> <span class="n">source</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">)</span> +</span><span id="TypeAnnotator-285"><a href="#TypeAnnotator-285"><span class="linenos">285</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="TypeAnnotator-286"><a href="#TypeAnnotator-286"><span class="linenos">286</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="o">.</span><span class="n">get_column_type</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> +</span><span id="TypeAnnotator-287"><a href="#TypeAnnotator-287"><span class="linenos">287</span></a> <span class="k">elif</span> <span class="n">source</span> <span class="ow">and</span> <span class="n">col</span><span class="o">.</span><span class="n">table</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">:</span> +</span><span id="TypeAnnotator-288"><a href="#TypeAnnotator-288"><span class="linenos">288</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">][</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">type</span> +</span><span id="TypeAnnotator-289"><a href="#TypeAnnotator-289"><span class="linenos">289</span></a> <span class="c1"># Then (possibly) annotate the remaining expressions in the scope</span> +</span><span id="TypeAnnotator-290"><a href="#TypeAnnotator-290"><span class="linenos">290</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-291"><a href="#TypeAnnotator-291"><span class="linenos">291</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="c1"># This takes care of non-traversable expressions</span> +</span><span id="TypeAnnotator-292"><a href="#TypeAnnotator-292"><span class="linenos">292</span></a> +</span><span id="TypeAnnotator-293"><a href="#TypeAnnotator-293"><span class="linenos">293</span></a> <span class="k">def</span> <span class="nf">_maybe_annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-294"><a href="#TypeAnnotator-294"><span class="linenos">294</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-295"><a href="#TypeAnnotator-295"><span class="linenos">295</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="TypeAnnotator-296"><a href="#TypeAnnotator-296"><span class="linenos">296</span></a> +</span><span id="TypeAnnotator-297"><a href="#TypeAnnotator-297"><span class="linenos">297</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="p">:</span> +</span><span id="TypeAnnotator-298"><a href="#TypeAnnotator-298"><span class="linenos">298</span></a> <span class="k">return</span> <span class="n">expression</span> <span class="c1"># We've already inferred the expression's type</span> +</span><span id="TypeAnnotator-299"><a href="#TypeAnnotator-299"><span class="linenos">299</span></a> +</span><span id="TypeAnnotator-300"><a href="#TypeAnnotator-300"><span class="linenos">300</span></a> <span class="n">annotator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">annotators</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> +</span><span id="TypeAnnotator-301"><a href="#TypeAnnotator-301"><span class="linenos">301</span></a> +</span><span id="TypeAnnotator-302"><a href="#TypeAnnotator-302"><span class="linenos">302</span></a> <span class="k">return</span> <span class="p">(</span> +</span><span id="TypeAnnotator-303"><a href="#TypeAnnotator-303"><span class="linenos">303</span></a> <span class="n">annotator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-304"><a href="#TypeAnnotator-304"><span class="linenos">304</span></a> <span class="k">if</span> <span class="n">annotator</span> +</span><span id="TypeAnnotator-305"><a href="#TypeAnnotator-305"><span class="linenos">305</span></a> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_with_type</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="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">UNKNOWN</span><span class="p">)</span> +</span><span id="TypeAnnotator-306"><a href="#TypeAnnotator-306"><span class="linenos">306</span></a> <span class="p">)</span> +</span><span id="TypeAnnotator-307"><a href="#TypeAnnotator-307"><span class="linenos">307</span></a> +</span><span id="TypeAnnotator-308"><a href="#TypeAnnotator-308"><span class="linenos">308</span></a> <span class="k">def</span> <span class="nf">_annotate_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-309"><a href="#TypeAnnotator-309"><span class="linenos">309</span></a> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="TypeAnnotator-310"><a href="#TypeAnnotator-310"><span class="linenos">310</span></a> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">ensure_collection</span><span class="p">(</span><span class="n">value</span><span class="p">):</span> +</span><span id="TypeAnnotator-311"><a href="#TypeAnnotator-311"><span class="linenos">311</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> +</span><span id="TypeAnnotator-312"><a href="#TypeAnnotator-312"><span class="linenos">312</span></a> +</span><span id="TypeAnnotator-313"><a href="#TypeAnnotator-313"><span class="linenos">313</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="TypeAnnotator-314"><a href="#TypeAnnotator-314"><span class="linenos">314</span></a> +</span><span id="TypeAnnotator-315"><a href="#TypeAnnotator-315"><span class="linenos">315</span></a> <span class="k">def</span> <span class="nf">_maybe_coerce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="TypeAnnotator-316"><a href="#TypeAnnotator-316"><span class="linenos">316</span></a> <span class="c1"># We propagate the NULL / UNKNOWN types upwards if found</span> +</span><span id="TypeAnnotator-317"><a href="#TypeAnnotator-317"><span class="linenos">317</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">type1</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="TypeAnnotator-318"><a href="#TypeAnnotator-318"><span class="linenos">318</span></a> <span class="n">type1</span> <span class="o">=</span> <span class="n">type1</span><span class="o">.</span><span class="n">this</span> +</span><span id="TypeAnnotator-319"><a href="#TypeAnnotator-319"><span class="linenos">319</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">type2</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="TypeAnnotator-320"><a href="#TypeAnnotator-320"><span class="linenos">320</span></a> <span class="n">type2</span> <span class="o">=</span> <span class="n">type2</span><span class="o">.</span><span class="n">this</span> +</span><span id="TypeAnnotator-321"><a href="#TypeAnnotator-321"><span class="linenos">321</span></a> +</span><span id="TypeAnnotator-322"><a href="#TypeAnnotator-322"><span class="linenos">322</span></a> <span class="k">if</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">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="TypeAnnotator-323"><a href="#TypeAnnotator-323"><span class="linenos">323</span></a> <span class="k">return</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">NULL</span> +</span><span id="TypeAnnotator-324"><a href="#TypeAnnotator-324"><span class="linenos">324</span></a> <span class="k">if</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">UNKNOWN</span> <span class="ow">in</span> <span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="n">type2</span><span class="p">):</span> +</span><span id="TypeAnnotator-325"><a href="#TypeAnnotator-325"><span class="linenos">325</span></a> <span class="k">return</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">UNKNOWN</span> +</span><span id="TypeAnnotator-326"><a href="#TypeAnnotator-326"><span class="linenos">326</span></a> +</span><span id="TypeAnnotator-327"><a href="#TypeAnnotator-327"><span class="linenos">327</span></a> <span class="k">return</span> <span class="n">type2</span> <span class="k">if</span> <span class="n">type2</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">coerces_to</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">type1</span><span class="p">,</span> <span class="p">{})</span> <span class="k">else</span> <span class="n">type1</span> +</span><span id="TypeAnnotator-328"><a href="#TypeAnnotator-328"><span class="linenos">328</span></a> +</span><span id="TypeAnnotator-329"><a href="#TypeAnnotator-329"><span class="linenos">329</span></a> <span class="k">def</span> <span class="nf">_annotate_binary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-330"><a href="#TypeAnnotator-330"><span class="linenos">330</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-331"><a href="#TypeAnnotator-331"><span class="linenos">331</span></a> +</span><span id="TypeAnnotator-332"><a href="#TypeAnnotator-332"><span class="linenos">332</span></a> <span class="n">left_type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="TypeAnnotator-333"><a href="#TypeAnnotator-333"><span class="linenos">333</span></a> <span class="n">right_type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="TypeAnnotator-334"><a href="#TypeAnnotator-334"><span class="linenos">334</span></a> +</span><span id="TypeAnnotator-335"><a href="#TypeAnnotator-335"><span class="linenos">335</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)):</span> +</span><span id="TypeAnnotator-336"><a href="#TypeAnnotator-336"><span class="linenos">336</span></a> <span class="k">if</span> <span class="n">left_type</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">NULL</span> <span class="ow">and</span> <span class="n">right_type</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">NULL</span><span class="p">:</span> +</span><span id="TypeAnnotator-337"><a href="#TypeAnnotator-337"><span class="linenos">337</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">NULL</span> +</span><span id="TypeAnnotator-338"><a href="#TypeAnnotator-338"><span class="linenos">338</span></a> <span class="k">elif</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">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left_type</span><span class="p">,</span> <span class="n">right_type</span><span class="p">):</span> +</span><span id="TypeAnnotator-339"><a href="#TypeAnnotator-339"><span class="linenos">339</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span> +</span><span id="TypeAnnotator-340"><a href="#TypeAnnotator-340"><span class="linenos">340</span></a> <span class="s2">"NULLABLE"</span><span class="p">,</span> <span class="n">expressions</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">"BOOLEAN"</span><span class="p">)</span> +</span><span id="TypeAnnotator-341"><a href="#TypeAnnotator-341"><span class="linenos">341</span></a> <span class="p">)</span> +</span><span id="TypeAnnotator-342"><a href="#TypeAnnotator-342"><span class="linenos">342</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-343"><a href="#TypeAnnotator-343"><span class="linenos">343</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="TypeAnnotator-344"><a href="#TypeAnnotator-344"><span class="linenos">344</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)):</span> +</span><span id="TypeAnnotator-345"><a href="#TypeAnnotator-345"><span class="linenos">345</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="TypeAnnotator-346"><a href="#TypeAnnotator-346"><span class="linenos">346</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-347"><a href="#TypeAnnotator-347"><span class="linenos">347</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_coerce</span><span class="p">(</span><span class="n">left_type</span><span class="p">,</span> <span class="n">right_type</span><span class="p">)</span> +</span><span id="TypeAnnotator-348"><a href="#TypeAnnotator-348"><span class="linenos">348</span></a> +</span><span id="TypeAnnotator-349"><a href="#TypeAnnotator-349"><span class="linenos">349</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="TypeAnnotator-350"><a href="#TypeAnnotator-350"><span class="linenos">350</span></a> +</span><span id="TypeAnnotator-351"><a href="#TypeAnnotator-351"><span class="linenos">351</span></a> <span class="k">def</span> <span class="nf">_annotate_unary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-352"><a href="#TypeAnnotator-352"><span class="linenos">352</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-353"><a href="#TypeAnnotator-353"><span class="linenos">353</span></a> +</span><span id="TypeAnnotator-354"><a href="#TypeAnnotator-354"><span class="linenos">354</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="TypeAnnotator-355"><a href="#TypeAnnotator-355"><span class="linenos">355</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BOOLEAN</span> +</span><span id="TypeAnnotator-356"><a href="#TypeAnnotator-356"><span class="linenos">356</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-357"><a href="#TypeAnnotator-357"><span class="linenos">357</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span> +</span><span id="TypeAnnotator-358"><a href="#TypeAnnotator-358"><span class="linenos">358</span></a> +</span><span id="TypeAnnotator-359"><a href="#TypeAnnotator-359"><span class="linenos">359</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="TypeAnnotator-360"><a href="#TypeAnnotator-360"><span class="linenos">360</span></a> +</span><span id="TypeAnnotator-361"><a href="#TypeAnnotator-361"><span class="linenos">361</span></a> <span class="k">def</span> <span class="nf">_annotate_literal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator-362"><a href="#TypeAnnotator-362"><span class="linenos">362</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> +</span><span id="TypeAnnotator-363"><a href="#TypeAnnotator-363"><span class="linenos">363</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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="TypeAnnotator-364"><a href="#TypeAnnotator-364"><span class="linenos">364</span></a> <span class="k">elif</span> <span class="n">expression</span><span class="o">.</span><span class="n">is_int</span><span class="p">:</span> +</span><span id="TypeAnnotator-365"><a href="#TypeAnnotator-365"><span class="linenos">365</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">INT</span> +</span><span id="TypeAnnotator-366"><a href="#TypeAnnotator-366"><span class="linenos">366</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator-367"><a href="#TypeAnnotator-367"><span class="linenos">367</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">DOUBLE</span> +</span><span id="TypeAnnotator-368"><a href="#TypeAnnotator-368"><span class="linenos">368</span></a> +</span><span id="TypeAnnotator-369"><a href="#TypeAnnotator-369"><span class="linenos">369</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="TypeAnnotator-370"><a href="#TypeAnnotator-370"><span class="linenos">370</span></a> +</span><span id="TypeAnnotator-371"><a href="#TypeAnnotator-371"><span class="linenos">371</span></a> <span class="k">def</span> <span class="nf">_annotate_with_type</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">target_type</span><span class="p">):</span> +</span><span id="TypeAnnotator-372"><a href="#TypeAnnotator-372"><span class="linenos">372</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">target_type</span> +</span><span id="TypeAnnotator-373"><a href="#TypeAnnotator-373"><span class="linenos">373</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-374"><a href="#TypeAnnotator-374"><span class="linenos">374</span></a> +</span><span id="TypeAnnotator-375"><a href="#TypeAnnotator-375"><span class="linenos">375</span></a> <span class="k">def</span> <span class="nf">_annotate_by_args</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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">promote</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="TypeAnnotator-376"><a href="#TypeAnnotator-376"><span class="linenos">376</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_annotate_args</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator-377"><a href="#TypeAnnotator-377"><span class="linenos">377</span></a> <span class="n">expressions</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="TypeAnnotator-378"><a href="#TypeAnnotator-378"><span class="linenos">378</span></a> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span> +</span><span id="TypeAnnotator-379"><a href="#TypeAnnotator-379"><span class="linenos">379</span></a> <span class="n">arg_expr</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="n">arg</span><span class="p">)</span> +</span><span id="TypeAnnotator-380"><a href="#TypeAnnotator-380"><span class="linenos">380</span></a> <span class="n">expressions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">expr</span> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">ensure_list</span><span class="p">(</span><span class="n">arg_expr</span><span class="p">)</span> <span class="k">if</span> <span class="n">expr</span><span class="p">)</span> +</span><span id="TypeAnnotator-381"><a href="#TypeAnnotator-381"><span class="linenos">381</span></a> +</span><span id="TypeAnnotator-382"><a href="#TypeAnnotator-382"><span class="linenos">382</span></a> <span class="n">last_datatype</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="TypeAnnotator-383"><a href="#TypeAnnotator-383"><span class="linenos">383</span></a> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">expressions</span><span class="p">:</span> +</span><span id="TypeAnnotator-384"><a href="#TypeAnnotator-384"><span class="linenos">384</span></a> <span class="n">last_datatype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_coerce</span><span class="p">(</span><span class="n">last_datatype</span> <span class="ow">or</span> <span class="n">expr</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">expr</span><span class="o">.</span><span class="n">type</span><span class="p">)</span> +</span><span id="TypeAnnotator-385"><a href="#TypeAnnotator-385"><span class="linenos">385</span></a> +</span><span id="TypeAnnotator-386"><a href="#TypeAnnotator-386"><span class="linenos">386</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">last_datatype</span> <span class="ow">or</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">UNKNOWN</span> +</span><span id="TypeAnnotator-387"><a href="#TypeAnnotator-387"><span class="linenos">387</span></a> +</span><span id="TypeAnnotator-388"><a href="#TypeAnnotator-388"><span class="linenos">388</span></a> <span class="k">if</span> <span class="n">promote</span><span class="p">:</span> +</span><span id="TypeAnnotator-389"><a href="#TypeAnnotator-389"><span class="linenos">389</span></a> <span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">INTEGER_TYPES</span><span class="p">:</span> +</span><span id="TypeAnnotator-390"><a href="#TypeAnnotator-390"><span class="linenos">390</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">BIGINT</span> +</span><span id="TypeAnnotator-391"><a href="#TypeAnnotator-391"><span class="linenos">391</span></a> <span class="k">elif</span> <span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">FLOAT_TYPES</span><span class="p">:</span> +</span><span id="TypeAnnotator-392"><a href="#TypeAnnotator-392"><span class="linenos">392</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">type</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">DOUBLE</span> +</span><span id="TypeAnnotator-393"><a href="#TypeAnnotator-393"><span class="linenos">393</span></a> +</span><span id="TypeAnnotator-394"><a href="#TypeAnnotator-394"><span class="linenos">394</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + <div id="TypeAnnotator.__init__" class="classattr"> + <input id="TypeAnnotator.__init__-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="name">TypeAnnotator</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">schema</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">annotators</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span></span>)</span> + + <label class="view-source-button" for="TypeAnnotator.__init__-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#TypeAnnotator.__init__"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="TypeAnnotator.__init__-247"><a href="#TypeAnnotator.__init__-247"><span class="linenos">247</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">annotators</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">coerces_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="TypeAnnotator.__init__-248"><a href="#TypeAnnotator.__init__-248"><span class="linenos">248</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span> +</span><span id="TypeAnnotator.__init__-249"><a href="#TypeAnnotator.__init__-249"><span class="linenos">249</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">annotators</span> <span class="o">=</span> <span class="n">annotators</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">ANNOTATORS</span> +</span><span id="TypeAnnotator.__init__-250"><a href="#TypeAnnotator.__init__-250"><span class="linenos">250</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">coerces_to</span> <span class="o">=</span> <span class="n">coerces_to</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">COERCES_TO</span> +</span></pre></div> + + + + + </div> + <div id="TypeAnnotator.annotate" class="classattr"> + <input id="TypeAnnotator.annotate-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">annotate</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="TypeAnnotator.annotate-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#TypeAnnotator.annotate"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="TypeAnnotator.annotate-252"><a href="#TypeAnnotator.annotate-252"><span class="linenos">252</span></a> <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-253"><a href="#TypeAnnotator.annotate-253"><span class="linenos">253</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TRAVERSABLES</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-254"><a href="#TypeAnnotator.annotate-254"><span class="linenos">254</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-255"><a href="#TypeAnnotator.annotate-255"><span class="linenos">255</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="TypeAnnotator.annotate-256"><a href="#TypeAnnotator.annotate-256"><span class="linenos">256</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="TypeAnnotator.annotate-257"><a href="#TypeAnnotator.annotate-257"><span class="linenos">257</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-258"><a href="#TypeAnnotator.annotate-258"><span class="linenos">258</span></a> <span class="k">continue</span> +</span><span id="TypeAnnotator.annotate-259"><a href="#TypeAnnotator.annotate-259"><span class="linenos">259</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-260"><a href="#TypeAnnotator.annotate-260"><span class="linenos">260</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="TypeAnnotator.annotate-261"><a href="#TypeAnnotator.annotate-261"><span class="linenos">261</span></a> +</span><span id="TypeAnnotator.annotate-262"><a href="#TypeAnnotator.annotate-262"><span class="linenos">262</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Lateral</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-263"><a href="#TypeAnnotator.annotate-263"><span class="linenos">263</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Explode</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-264"><a href="#TypeAnnotator.annotate-264"><span class="linenos">264</span></a> <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span><span class="p">]</span> +</span><span id="TypeAnnotator.annotate-265"><a href="#TypeAnnotator.annotate-265"><span class="linenos">265</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator.annotate-266"><a href="#TypeAnnotator.annotate-266"><span class="linenos">266</span></a> <span class="n">values</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</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">expressions</span> +</span><span id="TypeAnnotator.annotate-267"><a href="#TypeAnnotator.annotate-267"><span class="linenos">267</span></a> +</span><span id="TypeAnnotator.annotate-268"><a href="#TypeAnnotator.annotate-268"><span class="linenos">268</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">values</span><span class="p">:</span> +</span><span id="TypeAnnotator.annotate-269"><a href="#TypeAnnotator.annotate-269"><span class="linenos">269</span></a> <span class="k">continue</span> +</span><span id="TypeAnnotator.annotate-270"><a href="#TypeAnnotator.annotate-270"><span class="linenos">270</span></a> +</span><span id="TypeAnnotator.annotate-271"><a href="#TypeAnnotator.annotate-271"><span class="linenos">271</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator.annotate-272"><a href="#TypeAnnotator.annotate-272"><span class="linenos">272</span></a> <span class="n">alias</span><span class="p">:</span> <span class="n">column</span> +</span><span id="TypeAnnotator.annotate-273"><a href="#TypeAnnotator.annotate-273"><span class="linenos">273</span></a> <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span> +</span><span id="TypeAnnotator.annotate-274"><a href="#TypeAnnotator.annotate-274"><span class="linenos">274</span></a> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">alias_column_names</span><span class="p">,</span> +</span><span id="TypeAnnotator.annotate-275"><a href="#TypeAnnotator.annotate-275"><span class="linenos">275</span></a> <span class="n">values</span><span class="p">,</span> +</span><span id="TypeAnnotator.annotate-276"><a href="#TypeAnnotator.annotate-276"><span class="linenos">276</span></a> <span class="p">)</span> +</span><span id="TypeAnnotator.annotate-277"><a href="#TypeAnnotator.annotate-277"><span class="linenos">277</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator.annotate-278"><a href="#TypeAnnotator.annotate-278"><span class="linenos">278</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="TypeAnnotator.annotate-279"><a href="#TypeAnnotator.annotate-279"><span class="linenos">279</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="TypeAnnotator.annotate-280"><a href="#TypeAnnotator.annotate-280"><span class="linenos">280</span></a> <span class="n">select</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">select</span> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">selects</span> +</span><span id="TypeAnnotator.annotate-281"><a href="#TypeAnnotator.annotate-281"><span class="linenos">281</span></a> <span class="p">}</span> +</span><span id="TypeAnnotator.annotate-282"><a href="#TypeAnnotator.annotate-282"><span class="linenos">282</span></a> <span class="c1"># First annotate the current scope's column references</span> +</span><span id="TypeAnnotator.annotate-283"><a href="#TypeAnnotator.annotate-283"><span class="linenos">283</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="TypeAnnotator.annotate-284"><a href="#TypeAnnotator.annotate-284"><span class="linenos">284</span></a> <span class="n">source</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">)</span> +</span><span id="TypeAnnotator.annotate-285"><a href="#TypeAnnotator.annotate-285"><span class="linenos">285</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="TypeAnnotator.annotate-286"><a href="#TypeAnnotator.annotate-286"><span class="linenos">286</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="o">.</span><span class="n">get_column_type</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> +</span><span id="TypeAnnotator.annotate-287"><a href="#TypeAnnotator.annotate-287"><span class="linenos">287</span></a> <span class="k">elif</span> <span class="n">source</span> <span class="ow">and</span> <span class="n">col</span><span class="o">.</span><span class="n">table</span> <span class="ow">in</span> <span class="n">selects</span><span class="p">:</span> +</span><span id="TypeAnnotator.annotate-288"><a href="#TypeAnnotator.annotate-288"><span class="linenos">288</span></a> <span class="n">col</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">selects</span><span class="p">[</span><span class="n">col</span><span class="o">.</span><span class="n">table</span><span class="p">][</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">type</span> +</span><span id="TypeAnnotator.annotate-289"><a href="#TypeAnnotator.annotate-289"><span class="linenos">289</span></a> <span class="c1"># Then (possibly) annotate the remaining expressions in the scope</span> +</span><span id="TypeAnnotator.annotate-290"><a href="#TypeAnnotator.annotate-290"><span class="linenos">290</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="TypeAnnotator.annotate-291"><a href="#TypeAnnotator.annotate-291"><span class="linenos">291</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maybe_annotate</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> <span class="c1"># This takes care of non-traversable expressions</span> +</span></pre></div> + + + + + </div> + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/canonicalize.html b/docs/sqlglot/optimizer/canonicalize.html new file mode 100644 index 0000000..15e3120 --- /dev/null +++ b/docs/sqlglot/optimizer/canonicalize.html @@ -0,0 +1,445 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.canonicalize API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#canonicalize">canonicalize</a> + </li> + <li> + <a class="function" href="#add_text_to_concat">add_text_to_concat</a> + </li> + <li> + <a class="function" href="#coerce_type">coerce_type</a> + </li> + <li> + <a class="function" href="#remove_redundant_casts">remove_redundant_casts</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/canonicalize.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.canonicalize </h1> + + + <input id="mod-canonicalize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-canonicalize-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">canonicalize</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""Converts a sql expression into a standard form.</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> This method relies on annotate_types because many of the</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> conversions rely on type inference.</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> Args:</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> expression: The expression to canonicalize.</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> """</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">canonicalize</span><span class="p">)</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">add_text_to_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">coerce_type</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">remove_redundant_casts</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">):</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"quoted"</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a><span class="k">def</span> <span class="nf">add_text_to_concat</span><span class="p">(</span><span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">type</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEXT_TYPES</span><span class="p">:</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="k">return</span> <span class="n">node</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><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a><span class="k">def</span> <span class="nf">coerce_type</span><span class="p">(</span><span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">_coerce_date</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">node</span><span class="o">.</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">):</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos">37</span></a> <span class="n">_coerce_date</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"low"</span><span class="p">])</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Extract</span><span class="p">):</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEMPORAL_TYPES</span><span class="p">:</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> <span class="n">_replace_cast</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"datetime"</span><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a> +</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a><span class="k">def</span> <span class="nf">remove_redundant_casts</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos">46</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos">47</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a> <span class="p">):</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a> +</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="k">def</span> <span class="nf">_coerce_date</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">([</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">]):</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a> <span class="n">a</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a> <span class="ow">and</span> <span class="n">a</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">type</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="o">!=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos">62</span></a> <span class="p">):</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a> <span class="n">_replace_cast</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s2">"date"</span><span class="p">)</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a><span class="k">def</span> <span class="nf">_replace_cast</span><span class="p">(</span><span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos">67</span></a> <span class="n">data_type</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">to</span><span class="p">)</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos">68</span></a> <span class="n">cast</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">to</span><span class="o">=</span><span class="n">data_type</span><span class="p">)</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="n">cast</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">data_type</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a> <span class="n">node</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">cast</span><span class="p">)</span> +</span></pre></div> + + + </section> + <section id="canonicalize"> + <input id="canonicalize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">canonicalize</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="canonicalize-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#canonicalize"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="canonicalize-7"><a href="#canonicalize-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">canonicalize</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="canonicalize-8"><a href="#canonicalize-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""Converts a sql expression into a standard form.</span> +</span><span id="canonicalize-9"><a href="#canonicalize-9"><span class="linenos"> 9</span></a> +</span><span id="canonicalize-10"><a href="#canonicalize-10"><span class="linenos">10</span></a><span class="sd"> This method relies on annotate_types because many of the</span> +</span><span id="canonicalize-11"><a href="#canonicalize-11"><span class="linenos">11</span></a><span class="sd"> conversions rely on type inference.</span> +</span><span id="canonicalize-12"><a href="#canonicalize-12"><span class="linenos">12</span></a> +</span><span id="canonicalize-13"><a href="#canonicalize-13"><span class="linenos">13</span></a><span class="sd"> Args:</span> +</span><span id="canonicalize-14"><a href="#canonicalize-14"><span class="linenos">14</span></a><span class="sd"> expression: The expression to canonicalize.</span> +</span><span id="canonicalize-15"><a href="#canonicalize-15"><span class="linenos">15</span></a><span class="sd"> """</span> +</span><span id="canonicalize-16"><a href="#canonicalize-16"><span class="linenos">16</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">canonicalize</span><span class="p">)</span> +</span><span id="canonicalize-17"><a href="#canonicalize-17"><span class="linenos">17</span></a> +</span><span id="canonicalize-18"><a href="#canonicalize-18"><span class="linenos">18</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">add_text_to_concat</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="canonicalize-19"><a href="#canonicalize-19"><span class="linenos">19</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">coerce_type</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="canonicalize-20"><a href="#canonicalize-20"><span class="linenos">20</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">remove_redundant_casts</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="canonicalize-21"><a href="#canonicalize-21"><span class="linenos">21</span></a> +</span><span id="canonicalize-22"><a href="#canonicalize-22"><span class="linenos">22</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">):</span> +</span><span id="canonicalize-23"><a href="#canonicalize-23"><span class="linenos">23</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"quoted"</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span> +</span><span id="canonicalize-24"><a href="#canonicalize-24"><span class="linenos">24</span></a> +</span><span id="canonicalize-25"><a href="#canonicalize-25"><span class="linenos">25</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Converts a sql expression into a standard form.</p> + +<p>This method relies on annotate_types because many of the +conversions rely on type inference.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression:</strong> The expression to canonicalize.</li> +</ul> +</div> + + + </section> + <section id="add_text_to_concat"> + <input id="add_text_to_concat-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">add_text_to_concat</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">node</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="add_text_to_concat-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#add_text_to_concat"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="add_text_to_concat-28"><a href="#add_text_to_concat-28"><span class="linenos">28</span></a><span class="k">def</span> <span class="nf">add_text_to_concat</span><span class="p">(</span><span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="add_text_to_concat-29"><a href="#add_text_to_concat-29"><span class="linenos">29</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">)</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">type</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEXT_TYPES</span><span class="p">:</span> +</span><span id="add_text_to_concat-30"><a href="#add_text_to_concat-30"><span class="linenos">30</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="add_text_to_concat-31"><a href="#add_text_to_concat-31"><span class="linenos">31</span></a> <span class="k">return</span> <span class="n">node</span> +</span></pre></div> + + + + + </section> + <section id="coerce_type"> + <input id="coerce_type-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">coerce_type</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">node</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="coerce_type-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#coerce_type"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="coerce_type-34"><a href="#coerce_type-34"><span class="linenos">34</span></a><span class="k">def</span> <span class="nf">coerce_type</span><span class="p">(</span><span class="n">node</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="coerce_type-35"><a href="#coerce_type-35"><span class="linenos">35</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="coerce_type-36"><a href="#coerce_type-36"><span class="linenos">36</span></a> <span class="n">_coerce_date</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">node</span><span class="o">.</span><span class="n">right</span><span class="p">)</span> +</span><span id="coerce_type-37"><a href="#coerce_type-37"><span class="linenos">37</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">):</span> +</span><span id="coerce_type-38"><a href="#coerce_type-38"><span class="linenos">38</span></a> <span class="n">_coerce_date</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"low"</span><span class="p">])</span> +</span><span id="coerce_type-39"><a href="#coerce_type-39"><span class="linenos">39</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Extract</span><span class="p">):</span> +</span><span id="coerce_type-40"><a href="#coerce_type-40"><span class="linenos">40</span></a> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">TEMPORAL_TYPES</span><span class="p">:</span> +</span><span id="coerce_type-41"><a href="#coerce_type-41"><span class="linenos">41</span></a> <span class="n">_replace_cast</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="s2">"datetime"</span><span class="p">)</span> +</span><span id="coerce_type-42"><a href="#coerce_type-42"><span class="linenos">42</span></a> <span class="k">return</span> <span class="n">node</span> +</span></pre></div> + + + + + </section> + <section id="remove_redundant_casts"> + <input id="remove_redundant_casts-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">remove_redundant_casts</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="remove_redundant_casts-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#remove_redundant_casts"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="remove_redundant_casts-45"><a href="#remove_redundant_casts-45"><span class="linenos">45</span></a><span class="k">def</span> <span class="nf">remove_redundant_casts</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="remove_redundant_casts-46"><a href="#remove_redundant_casts-46"><span class="linenos">46</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="remove_redundant_casts-47"><a href="#remove_redundant_casts-47"><span class="linenos">47</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> +</span><span id="remove_redundant_casts-48"><a href="#remove_redundant_casts-48"><span class="linenos">48</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">type</span> +</span><span id="remove_redundant_casts-49"><a href="#remove_redundant_casts-49"><span class="linenos">49</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span> +</span><span id="remove_redundant_casts-50"><a href="#remove_redundant_casts-50"><span class="linenos">50</span></a> <span class="ow">and</span> <span class="n">expression</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">this</span> +</span><span id="remove_redundant_casts-51"><a href="#remove_redundant_casts-51"><span class="linenos">51</span></a> <span class="p">):</span> +</span><span id="remove_redundant_casts-52"><a href="#remove_redundant_casts-52"><span class="linenos">52</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="remove_redundant_casts-53"><a href="#remove_redundant_casts-53"><span class="linenos">53</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/eliminate_ctes.html b/docs/sqlglot/optimizer/eliminate_ctes.html new file mode 100644 index 0000000..2cad407 --- /dev/null +++ b/docs/sqlglot/optimizer/eliminate_ctes.html @@ -0,0 +1,371 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.eliminate_ctes API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#eliminate_ctes">eliminate_ctes</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/eliminate_ctes.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.eliminate_ctes </h1> + + + <input id="mod-eliminate_ctes-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-eliminate_ctes-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">build_scope</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="k">def</span> <span class="nf">eliminate_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="sd"> Remove unused CTEs from an expression.</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> Example:</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> >>> sql = "WITH y AS (SELECT a FROM x) SELECT a FROM z"</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> >>> eliminate_ctes(expression).sql()</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> 'SELECT a FROM z'</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> Args:</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="sd"> Returns:</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="sd"> """</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> <span class="n">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">ref_count</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">ref_count</span><span class="p">()</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="c1"># Traverse the scope tree in reverse so we can remove chains of unused CTEs</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">())):</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_cte</span><span class="p">:</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">count</span> <span class="o">=</span> <span class="n">ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">scope</span><span class="p">)]</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="k">if</span> <span class="n">count</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">cte_node</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="n">with_node</span> <span class="o">=</span> <span class="n">cte_node</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">cte_node</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="c1"># Pop the entire WITH clause if this is the last CTE</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">with_node</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">with_node</span><span class="o">.</span><span class="n">pop</span><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="c1"># Decrement the ref count for all sources this CTE selects from</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> <span class="n">ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o">-=</span> <span class="mi">1</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">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="eliminate_ctes"> + <input id="eliminate_ctes-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">eliminate_ctes</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="eliminate_ctes-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#eliminate_ctes"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="eliminate_ctes-5"><a href="#eliminate_ctes-5"><span class="linenos"> 5</span></a><span class="k">def</span> <span class="nf">eliminate_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="eliminate_ctes-6"><a href="#eliminate_ctes-6"><span class="linenos"> 6</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="eliminate_ctes-7"><a href="#eliminate_ctes-7"><span class="linenos"> 7</span></a><span class="sd"> Remove unused CTEs from an expression.</span> +</span><span id="eliminate_ctes-8"><a href="#eliminate_ctes-8"><span class="linenos"> 8</span></a> +</span><span id="eliminate_ctes-9"><a href="#eliminate_ctes-9"><span class="linenos"> 9</span></a><span class="sd"> Example:</span> +</span><span id="eliminate_ctes-10"><a href="#eliminate_ctes-10"><span class="linenos">10</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="eliminate_ctes-11"><a href="#eliminate_ctes-11"><span class="linenos">11</span></a><span class="sd"> >>> sql = "WITH y AS (SELECT a FROM x) SELECT a FROM z"</span> +</span><span id="eliminate_ctes-12"><a href="#eliminate_ctes-12"><span class="linenos">12</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="eliminate_ctes-13"><a href="#eliminate_ctes-13"><span class="linenos">13</span></a><span class="sd"> >>> eliminate_ctes(expression).sql()</span> +</span><span id="eliminate_ctes-14"><a href="#eliminate_ctes-14"><span class="linenos">14</span></a><span class="sd"> 'SELECT a FROM z'</span> +</span><span id="eliminate_ctes-15"><a href="#eliminate_ctes-15"><span class="linenos">15</span></a> +</span><span id="eliminate_ctes-16"><a href="#eliminate_ctes-16"><span class="linenos">16</span></a><span class="sd"> Args:</span> +</span><span id="eliminate_ctes-17"><a href="#eliminate_ctes-17"><span class="linenos">17</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="eliminate_ctes-18"><a href="#eliminate_ctes-18"><span class="linenos">18</span></a><span class="sd"> Returns:</span> +</span><span id="eliminate_ctes-19"><a href="#eliminate_ctes-19"><span class="linenos">19</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="eliminate_ctes-20"><a href="#eliminate_ctes-20"><span class="linenos">20</span></a><span class="sd"> """</span> +</span><span id="eliminate_ctes-21"><a href="#eliminate_ctes-21"><span class="linenos">21</span></a> <span class="n">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="eliminate_ctes-22"><a href="#eliminate_ctes-22"><span class="linenos">22</span></a> +</span><span id="eliminate_ctes-23"><a href="#eliminate_ctes-23"><span class="linenos">23</span></a> <span class="n">ref_count</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">ref_count</span><span class="p">()</span> +</span><span id="eliminate_ctes-24"><a href="#eliminate_ctes-24"><span class="linenos">24</span></a> +</span><span id="eliminate_ctes-25"><a href="#eliminate_ctes-25"><span class="linenos">25</span></a> <span class="c1"># Traverse the scope tree in reverse so we can remove chains of unused CTEs</span> +</span><span id="eliminate_ctes-26"><a href="#eliminate_ctes-26"><span class="linenos">26</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">())):</span> +</span><span id="eliminate_ctes-27"><a href="#eliminate_ctes-27"><span class="linenos">27</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_cte</span><span class="p">:</span> +</span><span id="eliminate_ctes-28"><a href="#eliminate_ctes-28"><span class="linenos">28</span></a> <span class="n">count</span> <span class="o">=</span> <span class="n">ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">scope</span><span class="p">)]</span> +</span><span id="eliminate_ctes-29"><a href="#eliminate_ctes-29"><span class="linenos">29</span></a> <span class="k">if</span> <span class="n">count</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span> +</span><span id="eliminate_ctes-30"><a href="#eliminate_ctes-30"><span class="linenos">30</span></a> <span class="n">cte_node</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="eliminate_ctes-31"><a href="#eliminate_ctes-31"><span class="linenos">31</span></a> <span class="n">with_node</span> <span class="o">=</span> <span class="n">cte_node</span><span class="o">.</span><span class="n">parent</span> +</span><span id="eliminate_ctes-32"><a href="#eliminate_ctes-32"><span class="linenos">32</span></a> <span class="n">cte_node</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="eliminate_ctes-33"><a href="#eliminate_ctes-33"><span class="linenos">33</span></a> +</span><span id="eliminate_ctes-34"><a href="#eliminate_ctes-34"><span class="linenos">34</span></a> <span class="c1"># Pop the entire WITH clause if this is the last CTE</span> +</span><span id="eliminate_ctes-35"><a href="#eliminate_ctes-35"><span class="linenos">35</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">with_node</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span> +</span><span id="eliminate_ctes-36"><a href="#eliminate_ctes-36"><span class="linenos">36</span></a> <span class="n">with_node</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="eliminate_ctes-37"><a href="#eliminate_ctes-37"><span class="linenos">37</span></a> +</span><span id="eliminate_ctes-38"><a href="#eliminate_ctes-38"><span class="linenos">38</span></a> <span class="c1"># Decrement the ref count for all sources this CTE selects from</span> +</span><span id="eliminate_ctes-39"><a href="#eliminate_ctes-39"><span class="linenos">39</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="eliminate_ctes-40"><a href="#eliminate_ctes-40"><span class="linenos">40</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="eliminate_ctes-41"><a href="#eliminate_ctes-41"><span class="linenos">41</span></a> <span class="n">ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o">-=</span> <span class="mi">1</span> +</span><span id="eliminate_ctes-42"><a href="#eliminate_ctes-42"><span class="linenos">42</span></a> +</span><span id="eliminate_ctes-43"><a href="#eliminate_ctes-43"><span class="linenos">43</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Remove unused CTEs from an expression.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"WITH y AS (SELECT a FROM x) SELECT a FROM z"</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">eliminate_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT a FROM z'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/eliminate_joins.html b/docs/sqlglot/optimizer/eliminate_joins.html new file mode 100644 index 0000000..1696908 --- /dev/null +++ b/docs/sqlglot/optimizer/eliminate_joins.html @@ -0,0 +1,610 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.eliminate_joins API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#eliminate_joins">eliminate_joins</a> + </li> + <li> + <a class="function" href="#join_condition">join_condition</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/eliminate_joins.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.eliminate_joins </h1> + + + <input id="mod-eliminate_joins-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-eliminate_joins-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">expressions</span> <span class="k">as</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.normalize</span> <span class="kn">import</span> <span class="n">normalized</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">simplify</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">eliminate_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Remove unused joins from an expression.</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> This only removes joins when we know that the join condition doesn't produce duplicate rows.</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> Example:</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> sql = "SELECT x.a FROM x LEFT JOIN (SELECT DISTINCT y.b FROM y) AS y ON x.b = y.b"</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> >>> eliminate_joins(expression).sql()</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> 'SELECT x.a FROM x'</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Args:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> Returns:</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> """</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> <span class="c1"># If any columns in this scope aren't qualified, it's hard to determine if a join isn't used.</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> <span class="c1"># It's probably possible to infer this from the outputs of derived tables.</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> <span class="c1"># But for now, let's just skip this rule.</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">unqualified_columns</span><span class="p">:</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="k">continue</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">joins</span> <span class="o">=</span> <span class="n">scope</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">"joins"</span><span class="p">,</span> <span class="p">[])</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="c1"># Reverse the joins so we can remove chains of unused joins</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">joins</span><span class="p">):</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="k">if</span> <span class="n">_should_eliminate_join</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="n">join</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">remove_source</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a><span class="k">def</span> <span class="nf">_should_eliminate_join</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="n">inner_source</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="k">return</span> <span class="p">(</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inner_source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">_join_is_used</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="ow">and</span> <span class="p">(</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="p">(</span><span class="n">join</span><span class="o">.</span><span class="n">side</span> <span class="o">==</span> <span class="s2">"LEFT"</span> <span class="ow">and</span> <span class="n">_is_joined_on_all_unique_outputs</span><span class="p">(</span><span class="n">inner_source</span><span class="p">,</span> <span class="n">join</span><span class="p">))</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="ow">or</span> <span class="p">(</span><span class="ow">not</span> <span class="n">join</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">"on"</span><span class="p">)</span> <span class="ow">and</span> <span class="n">_has_single_output_row</span><span class="p">(</span><span class="n">inner_source</span><span class="p">))</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="p">)</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="p">)</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> +</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="k">def</span> <span class="nf">_join_is_used</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="c1"># We need to find all columns that reference this join.</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="c1"># But columns in the ON clause shouldn't count.</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">join</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">"on"</span><span class="p">)</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="k">if</span> <span class="n">on</span><span class="p">:</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">on_clause_columns</span> <span class="o">=</span> <span class="p">{</span><span class="nb">id</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)}</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="n">on_clause_columns</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="k">return</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">column</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">source_columns</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> <span class="k">if</span> <span class="nb">id</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">on_clause_columns</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="p">)</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> +</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">def</span> <span class="nf">_is_joined_on_all_unique_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">):</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="n">unique_outputs</span> <span class="o">=</span> <span class="n">_unique_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">unique_outputs</span><span class="p">:</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="k">return</span> <span class="kc">False</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">_</span><span class="p">,</span> <span class="n">join_keys</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">join_condition</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="n">remaining_unique_outputs</span> <span class="o">=</span> <span class="n">unique_outputs</span> <span class="o">-</span> <span class="p">{</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">join_keys</span><span class="p">}</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="k">return</span> <span class="ow">not</span> <span class="n">remaining_unique_outputs</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a><span class="k">def</span> <span class="nf">_unique_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a><span class="w"> </span><span class="sd">"""Determine output columns of `scope` that must have a unique combination per row"""</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="k">if</span> <span class="n">scope</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">"distinct"</span><span class="p">):</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="n">group</span> <span class="o">=</span> <span class="n">scope</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">"group"</span><span class="p">)</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">if</span> <span class="n">group</span><span class="p">:</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">grouped_expressions</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">group</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="n">grouped_outputs</span> <span class="o">=</span> <span class="nb">set</span><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="n">unique_outputs</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="n">output</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">unalias</span><span class="p">()</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="k">if</span> <span class="n">output</span> <span class="ow">in</span> <span class="n">grouped_expressions</span><span class="p">:</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="n">grouped_outputs</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">output</span><span class="p">)</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="n">unique_outputs</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">)</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="c1"># All the grouped expressions must be in the output</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="n">grouped_expressions</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">grouped_outputs</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="n">unique_outputs</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">return</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="k">if</span> <span class="n">_has_single_output_row</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="k">return</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a><span class="k">def</span> <span class="nf">_has_single_output_row</span><span class="p">(</span><span class="n">scope</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">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="nb">all</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">unalias</span><span class="p">(),</span> <span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">)</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="ow">or</span> <span class="n">_is_limit_1</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="n">scope</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">"from"</span><span class="p">)</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="p">)</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a><span class="k">def</span> <span class="nf">_is_limit_1</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="n">limit</span> <span class="o">=</span> <span class="n">scope</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">"limit"</span><span class="p">)</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">return</span> <span class="n">limit</span> <span class="ow">and</span> <span class="n">limit</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="s2">"1"</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a><span class="k">def</span> <span class="nf">join_condition</span><span class="p">(</span><span class="n">join</span><span class="p">):</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a><span class="sd"> Extract the join condition from a join expression.</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="sd"> Args:</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a><span class="sd"> join (exp.Join)</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a><span class="sd"> Returns:</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a><span class="sd"> tuple[list[str], list[str], exp.Expression]:</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a><span class="sd"> Tuple of (source key, join key, remaining predicate)</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a><span class="sd"> """</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="n">on</span> <span class="o">=</span> <span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="n">source_key</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="n">join_key</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="k">def</span> <span class="nf">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o">=</span> <span class="n">condition</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="n">left_tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="n">right_tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">left_tables</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">right_tables</span><span class="p">:</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="n">join_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="n">source_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="k">elif</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">right_tables</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">left_tables</span><span class="p">:</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="n">join_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="n">source_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="c1"># find the join keys</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="c1"># SELECT</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="c1"># FROM x</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="c1"># JOIN y</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="c1"># ON x.a = y.b AND y.b > 1</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="c1">#</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> <span class="c1"># should pull y.b as the join key and x.a as the source key</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="k">if</span> <span class="n">normalized</span><span class="p">(</span><span class="n">on</span><span class="p">):</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">on</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">):</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="n">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> <span class="k">elif</span> <span class="n">normalized</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="n">parts</span> <span class="o">=</span> <span class="p">[</span><span class="n">part</span> <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">condition</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">part</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)]</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="k">if</span> <span class="n">conditions</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="n">parts</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="n">temp</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parts</span><span class="p">:</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="n">cs</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">conditions</span> <span class="k">if</span> <span class="n">p</span> <span class="o">==</span> <span class="n">c</span><span class="p">]</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="k">if</span> <span class="n">cs</span><span class="p">:</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="n">temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="n">temp</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">cs</span><span class="p">)</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="n">temp</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">conditions</span><span class="p">:</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="n">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">on</span><span class="p">)</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">remaining_condition</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">if</span> <span class="n">on</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">else</span> <span class="n">on</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="k">return</span> <span class="n">source_key</span><span class="p">,</span> <span class="n">join_key</span><span class="p">,</span> <span class="n">remaining_condition</span> +</span></pre></div> + + + </section> + <section id="eliminate_joins"> + <input id="eliminate_joins-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">eliminate_joins</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="eliminate_joins-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#eliminate_joins"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="eliminate_joins-8"><a href="#eliminate_joins-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">eliminate_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="eliminate_joins-9"><a href="#eliminate_joins-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="eliminate_joins-10"><a href="#eliminate_joins-10"><span class="linenos">10</span></a><span class="sd"> Remove unused joins from an expression.</span> +</span><span id="eliminate_joins-11"><a href="#eliminate_joins-11"><span class="linenos">11</span></a> +</span><span id="eliminate_joins-12"><a href="#eliminate_joins-12"><span class="linenos">12</span></a><span class="sd"> This only removes joins when we know that the join condition doesn't produce duplicate rows.</span> +</span><span id="eliminate_joins-13"><a href="#eliminate_joins-13"><span class="linenos">13</span></a> +</span><span id="eliminate_joins-14"><a href="#eliminate_joins-14"><span class="linenos">14</span></a><span class="sd"> Example:</span> +</span><span id="eliminate_joins-15"><a href="#eliminate_joins-15"><span class="linenos">15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="eliminate_joins-16"><a href="#eliminate_joins-16"><span class="linenos">16</span></a><span class="sd"> >>> sql = "SELECT x.a FROM x LEFT JOIN (SELECT DISTINCT y.b FROM y) AS y ON x.b = y.b"</span> +</span><span id="eliminate_joins-17"><a href="#eliminate_joins-17"><span class="linenos">17</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="eliminate_joins-18"><a href="#eliminate_joins-18"><span class="linenos">18</span></a><span class="sd"> >>> eliminate_joins(expression).sql()</span> +</span><span id="eliminate_joins-19"><a href="#eliminate_joins-19"><span class="linenos">19</span></a><span class="sd"> 'SELECT x.a FROM x'</span> +</span><span id="eliminate_joins-20"><a href="#eliminate_joins-20"><span class="linenos">20</span></a> +</span><span id="eliminate_joins-21"><a href="#eliminate_joins-21"><span class="linenos">21</span></a><span class="sd"> Args:</span> +</span><span id="eliminate_joins-22"><a href="#eliminate_joins-22"><span class="linenos">22</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="eliminate_joins-23"><a href="#eliminate_joins-23"><span class="linenos">23</span></a><span class="sd"> Returns:</span> +</span><span id="eliminate_joins-24"><a href="#eliminate_joins-24"><span class="linenos">24</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="eliminate_joins-25"><a href="#eliminate_joins-25"><span class="linenos">25</span></a><span class="sd"> """</span> +</span><span id="eliminate_joins-26"><a href="#eliminate_joins-26"><span class="linenos">26</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="eliminate_joins-27"><a href="#eliminate_joins-27"><span class="linenos">27</span></a> <span class="c1"># If any columns in this scope aren't qualified, it's hard to determine if a join isn't used.</span> +</span><span id="eliminate_joins-28"><a href="#eliminate_joins-28"><span class="linenos">28</span></a> <span class="c1"># It's probably possible to infer this from the outputs of derived tables.</span> +</span><span id="eliminate_joins-29"><a href="#eliminate_joins-29"><span class="linenos">29</span></a> <span class="c1"># But for now, let's just skip this rule.</span> +</span><span id="eliminate_joins-30"><a href="#eliminate_joins-30"><span class="linenos">30</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">unqualified_columns</span><span class="p">:</span> +</span><span id="eliminate_joins-31"><a href="#eliminate_joins-31"><span class="linenos">31</span></a> <span class="k">continue</span> +</span><span id="eliminate_joins-32"><a href="#eliminate_joins-32"><span class="linenos">32</span></a> +</span><span id="eliminate_joins-33"><a href="#eliminate_joins-33"><span class="linenos">33</span></a> <span class="n">joins</span> <span class="o">=</span> <span class="n">scope</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">"joins"</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="eliminate_joins-34"><a href="#eliminate_joins-34"><span class="linenos">34</span></a> +</span><span id="eliminate_joins-35"><a href="#eliminate_joins-35"><span class="linenos">35</span></a> <span class="c1"># Reverse the joins so we can remove chains of unused joins</span> +</span><span id="eliminate_joins-36"><a href="#eliminate_joins-36"><span class="linenos">36</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">joins</span><span class="p">):</span> +</span><span id="eliminate_joins-37"><a href="#eliminate_joins-37"><span class="linenos">37</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="eliminate_joins-38"><a href="#eliminate_joins-38"><span class="linenos">38</span></a> <span class="k">if</span> <span class="n">_should_eliminate_join</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="eliminate_joins-39"><a href="#eliminate_joins-39"><span class="linenos">39</span></a> <span class="n">join</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="eliminate_joins-40"><a href="#eliminate_joins-40"><span class="linenos">40</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">remove_source</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="eliminate_joins-41"><a href="#eliminate_joins-41"><span class="linenos">41</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Remove unused joins from an expression.</p> + +<p>This only removes joins when we know that the join condition doesn't produce duplicate rows.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"SELECT x.a FROM x LEFT JOIN (SELECT DISTINCT y.b FROM y) AS y ON x.b = y.b"</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">eliminate_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT x.a FROM x'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + <section id="join_condition"> + <input id="join_condition-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">join_condition</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">join</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="join_condition-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#join_condition"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="join_condition-121"><a href="#join_condition-121"><span class="linenos">121</span></a><span class="k">def</span> <span class="nf">join_condition</span><span class="p">(</span><span class="n">join</span><span class="p">):</span> +</span><span id="join_condition-122"><a href="#join_condition-122"><span class="linenos">122</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="join_condition-123"><a href="#join_condition-123"><span class="linenos">123</span></a><span class="sd"> Extract the join condition from a join expression.</span> +</span><span id="join_condition-124"><a href="#join_condition-124"><span class="linenos">124</span></a> +</span><span id="join_condition-125"><a href="#join_condition-125"><span class="linenos">125</span></a><span class="sd"> Args:</span> +</span><span id="join_condition-126"><a href="#join_condition-126"><span class="linenos">126</span></a><span class="sd"> join (exp.Join)</span> +</span><span id="join_condition-127"><a href="#join_condition-127"><span class="linenos">127</span></a><span class="sd"> Returns:</span> +</span><span id="join_condition-128"><a href="#join_condition-128"><span class="linenos">128</span></a><span class="sd"> tuple[list[str], list[str], exp.Expression]:</span> +</span><span id="join_condition-129"><a href="#join_condition-129"><span class="linenos">129</span></a><span class="sd"> Tuple of (source key, join key, remaining predicate)</span> +</span><span id="join_condition-130"><a href="#join_condition-130"><span class="linenos">130</span></a><span class="sd"> """</span> +</span><span id="join_condition-131"><a href="#join_condition-131"><span class="linenos">131</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="join_condition-132"><a href="#join_condition-132"><span class="linenos">132</span></a> <span class="n">on</span> <span class="o">=</span> <span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="join_condition-133"><a href="#join_condition-133"><span class="linenos">133</span></a> <span class="n">source_key</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="join_condition-134"><a href="#join_condition-134"><span class="linenos">134</span></a> <span class="n">join_key</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="join_condition-135"><a href="#join_condition-135"><span class="linenos">135</span></a> +</span><span id="join_condition-136"><a href="#join_condition-136"><span class="linenos">136</span></a> <span class="k">def</span> <span class="nf">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="join_condition-137"><a href="#join_condition-137"><span class="linenos">137</span></a> <span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o">=</span> <span class="n">condition</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="join_condition-138"><a href="#join_condition-138"><span class="linenos">138</span></a> <span class="n">left_tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="join_condition-139"><a href="#join_condition-139"><span class="linenos">139</span></a> <span class="n">right_tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="join_condition-140"><a href="#join_condition-140"><span class="linenos">140</span></a> +</span><span id="join_condition-141"><a href="#join_condition-141"><span class="linenos">141</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">left_tables</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">right_tables</span><span class="p">:</span> +</span><span id="join_condition-142"><a href="#join_condition-142"><span class="linenos">142</span></a> <span class="n">join_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="join_condition-143"><a href="#join_condition-143"><span class="linenos">143</span></a> <span class="n">source_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="join_condition-144"><a href="#join_condition-144"><span class="linenos">144</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="join_condition-145"><a href="#join_condition-145"><span class="linenos">145</span></a> <span class="k">elif</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">right_tables</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">left_tables</span><span class="p">:</span> +</span><span id="join_condition-146"><a href="#join_condition-146"><span class="linenos">146</span></a> <span class="n">join_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> +</span><span id="join_condition-147"><a href="#join_condition-147"><span class="linenos">147</span></a> <span class="n">source_key</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> +</span><span id="join_condition-148"><a href="#join_condition-148"><span class="linenos">148</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="join_condition-149"><a href="#join_condition-149"><span class="linenos">149</span></a> +</span><span id="join_condition-150"><a href="#join_condition-150"><span class="linenos">150</span></a> <span class="c1"># find the join keys</span> +</span><span id="join_condition-151"><a href="#join_condition-151"><span class="linenos">151</span></a> <span class="c1"># SELECT</span> +</span><span id="join_condition-152"><a href="#join_condition-152"><span class="linenos">152</span></a> <span class="c1"># FROM x</span> +</span><span id="join_condition-153"><a href="#join_condition-153"><span class="linenos">153</span></a> <span class="c1"># JOIN y</span> +</span><span id="join_condition-154"><a href="#join_condition-154"><span class="linenos">154</span></a> <span class="c1"># ON x.a = y.b AND y.b > 1</span> +</span><span id="join_condition-155"><a href="#join_condition-155"><span class="linenos">155</span></a> <span class="c1">#</span> +</span><span id="join_condition-156"><a href="#join_condition-156"><span class="linenos">156</span></a> <span class="c1"># should pull y.b as the join key and x.a as the source key</span> +</span><span id="join_condition-157"><a href="#join_condition-157"><span class="linenos">157</span></a> <span class="k">if</span> <span class="n">normalized</span><span class="p">(</span><span class="n">on</span><span class="p">):</span> +</span><span id="join_condition-158"><a href="#join_condition-158"><span class="linenos">158</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">on</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="join_condition-159"><a href="#join_condition-159"><span class="linenos">159</span></a> +</span><span id="join_condition-160"><a href="#join_condition-160"><span class="linenos">160</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="join_condition-161"><a href="#join_condition-161"><span class="linenos">161</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">):</span> +</span><span id="join_condition-162"><a href="#join_condition-162"><span class="linenos">162</span></a> <span class="n">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> +</span><span id="join_condition-163"><a href="#join_condition-163"><span class="linenos">163</span></a> <span class="k">elif</span> <span class="n">normalized</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="join_condition-164"><a href="#join_condition-164"><span class="linenos">164</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="join_condition-165"><a href="#join_condition-165"><span class="linenos">165</span></a> +</span><span id="join_condition-166"><a href="#join_condition-166"><span class="linenos">166</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="join_condition-167"><a href="#join_condition-167"><span class="linenos">167</span></a> <span class="n">parts</span> <span class="o">=</span> <span class="p">[</span><span class="n">part</span> <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">condition</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">part</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)]</span> +</span><span id="join_condition-168"><a href="#join_condition-168"><span class="linenos">168</span></a> <span class="k">if</span> <span class="n">conditions</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="join_condition-169"><a href="#join_condition-169"><span class="linenos">169</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="n">parts</span> +</span><span id="join_condition-170"><a href="#join_condition-170"><span class="linenos">170</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="join_condition-171"><a href="#join_condition-171"><span class="linenos">171</span></a> <span class="n">temp</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="join_condition-172"><a href="#join_condition-172"><span class="linenos">172</span></a> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parts</span><span class="p">:</span> +</span><span id="join_condition-173"><a href="#join_condition-173"><span class="linenos">173</span></a> <span class="n">cs</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">conditions</span> <span class="k">if</span> <span class="n">p</span> <span class="o">==</span> <span class="n">c</span><span class="p">]</span> +</span><span id="join_condition-174"><a href="#join_condition-174"><span class="linenos">174</span></a> +</span><span id="join_condition-175"><a href="#join_condition-175"><span class="linenos">175</span></a> <span class="k">if</span> <span class="n">cs</span><span class="p">:</span> +</span><span id="join_condition-176"><a href="#join_condition-176"><span class="linenos">176</span></a> <span class="n">temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> +</span><span id="join_condition-177"><a href="#join_condition-177"><span class="linenos">177</span></a> <span class="n">temp</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">cs</span><span class="p">)</span> +</span><span id="join_condition-178"><a href="#join_condition-178"><span class="linenos">178</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="n">temp</span> +</span><span id="join_condition-179"><a href="#join_condition-179"><span class="linenos">179</span></a> +</span><span id="join_condition-180"><a href="#join_condition-180"><span class="linenos">180</span></a> <span class="k">for</span> <span class="n">condition</span> <span class="ow">in</span> <span class="n">conditions</span><span class="p">:</span> +</span><span id="join_condition-181"><a href="#join_condition-181"><span class="linenos">181</span></a> <span class="n">extract_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> +</span><span id="join_condition-182"><a href="#join_condition-182"><span class="linenos">182</span></a> +</span><span id="join_condition-183"><a href="#join_condition-183"><span class="linenos">183</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">on</span><span class="p">)</span> +</span><span id="join_condition-184"><a href="#join_condition-184"><span class="linenos">184</span></a> <span class="n">remaining_condition</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">if</span> <span class="n">on</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">else</span> <span class="n">on</span> +</span><span id="join_condition-185"><a href="#join_condition-185"><span class="linenos">185</span></a> <span class="k">return</span> <span class="n">source_key</span><span class="p">,</span> <span class="n">join_key</span><span class="p">,</span> <span class="n">remaining_condition</span> +</span></pre></div> + + + <div class="docstring"><p>Extract the join condition from a join expression.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li>join (exp.Join)</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>tuple[list[str], list[str], exp.Expression]: + Tuple of (source key, join key, remaining predicate)</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/eliminate_subqueries.html b/docs/sqlglot/optimizer/eliminate_subqueries.html new file mode 100644 index 0000000..34e38b8 --- /dev/null +++ b/docs/sqlglot/optimizer/eliminate_subqueries.html @@ -0,0 +1,582 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.eliminate_subqueries API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#eliminate_subqueries">eliminate_subqueries</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/eliminate_subqueries.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.eliminate_subqueries </h1> + + + <input id="mod-eliminate_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-eliminate_subqueries-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">expressions</span> <span class="k">as</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">find_new_name</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">simplify</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="k">def</span> <span class="nf">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> Rewrite derived tables as CTES, deduplicating if possible.</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> Example:</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT * FROM x) AS y")</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> eliminate_subqueries(expression).sql()</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y'</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> This also deduplicates common subqueries:</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT * FROM x) AS y JOIN (SELECT * FROM x) AS z")</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> >>> eliminate_subqueries(expression).sql()</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y JOIN y AS z'</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> Args:</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> expression (sqlglot.Expression): expression</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> Returns:</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> sqlglot.Expression: expression</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a><span class="sd"> """</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="c1"># It's possible to have subqueries at the root, e.g. (SELECT * FROM x) LIMIT 1</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="n">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="k">return</span> <span class="n">expression</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="n">expression</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</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">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><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="c1"># Map of alias->Scope|Table</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="c1"># These are all aliases that are already used in the expression.</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="c1"># We don't want to create new CTEs that conflict with these names.</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="n">taken</span> <span class="o">=</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="c1"># All CTE aliases in the root scope are taken</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">:</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="n">taken</span><span class="p">[</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="c1"># All table names are taken</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">taken</span><span class="o">.</span><span class="n">update</span><span class="p">(</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="p">{</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="n">source</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">source</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="p">}</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="p">)</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="c1"># Map of Expression->alias</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="c1"># Existing CTES in the root expression. We'll use this for deduplication.</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="n">existing_ctes</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">root</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">"with"</span><span class="p">)</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="n">recursive</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="k">if</span> <span class="n">with_</span><span class="p">:</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="n">recursive</span> <span class="o">=</span> <span class="n">with_</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">"recursive"</span><span class="p">)</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="k">for</span> <span class="n">cte</span> <span class="ow">in</span> <span class="n">with_</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="n">existing_ctes</span><span class="p">[</span><span class="n">cte</span><span class="o">.</span><span class="n">this</span><span class="p">]</span> <span class="o">=</span> <span class="n">cte</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="n">new_ctes</span> <span class="o">=</span> <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="c1"># We're adding more CTEs, but we want to maintain the DAG order.</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="c1"># Derived tables within an existing CTE need to come before the existing CTE.</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="k">for</span> <span class="n">cte_scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">:</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="c1"># Append all the new CTEs from this existing CTE</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">cte_scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="n">cte_scope</span><span class="p">:</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="c1"># Don't try to eliminate this CTE itself</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="k">continue</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">new_cte</span> <span class="o">=</span> <span class="n">_eliminate</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="k">if</span> <span class="n">new_cte</span><span class="p">:</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_cte</span><span class="p">)</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="c1"># Append the existing CTE itself</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cte_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="c1"># Now append the rest</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">root</span><span class="o">.</span><span class="n">union_scopes</span><span class="p">,</span> <span class="n">root</span><span class="o">.</span><span class="n">subquery_scopes</span><span class="p">,</span> <span class="n">root</span><span class="o">.</span><span class="n">derived_table_scopes</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="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="n">new_cte</span> <span class="o">=</span> <span class="n">_eliminate</span><span class="p">(</span><span class="n">child_scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="k">if</span> <span class="n">new_cte</span><span class="p">:</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_cte</span><span class="p">)</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="k">if</span> <span class="n">new_ctes</span><span class="p">:</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"with"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">With</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_ctes</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="n">recursive</span><span class="p">))</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a><span class="k">def</span> <span class="nf">_eliminate</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">):</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_union</span><span class="p">:</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="k">return</span> <span class="n">_eliminate_union</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_derived_table</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="k">return</span> <span class="n">_eliminate_derived_table</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_cte</span><span class="p">:</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="k">return</span> <span class="n">_eliminate_cte</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a><span class="k">def</span> <span class="nf">_eliminate_union</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">):</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">duplicate_cte_alias</span> <span class="o">=</span> <span class="n">existing_ctes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">duplicate_cte_alias</span> <span class="ow">or</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken</span><span class="o">=</span><span class="n">taken</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="s2">"cte"</span><span class="p">)</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="n">taken</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="c1"># Try to maintain the selections</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="n">expressions</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">alias</span><span class="p">),</span> <span class="n">alias</span><span class="o">=</span><span class="n">e</span><span class="o">.</span><span class="n">alias_or_name</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">e</span> <span class="ow">in</span> <span class="n">expressions</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="p">]</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="c1"># If not all selections have an alias, just select *</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</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="nb">len</span><span class="p">(</span><span class="n">expressions</span><span class="p">):</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"*"</span><span class="p">]</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="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">*</span><span class="n">selects</span><span class="p">)</span><span class="o">.</span><span class="n">from_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">table_</span><span class="p">(</span><span class="n">alias</span><span class="p">),</span> <span class="n">alias</span><span class="o">=</span><span class="n">alias</span><span class="p">)))</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">duplicate_cte_alias</span><span class="p">:</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">existing_ctes</span><span class="p">[</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">]</span> <span class="o">=</span> <span class="n">alias</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">(</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="n">this</span><span class="o">=</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">alias</span><span class="p">)),</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="p">)</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a><span class="k">def</span> <span class="nf">_eliminate_derived_table</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">):</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="n">name</span><span class="p">,</span> <span class="n">cte</span> <span class="o">=</span> <span class="n">_new_cte</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="n">table</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">table_</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">alias</span><span class="o">=</span><span class="n">parent</span><span class="o">.</span><span class="n">alias</span> <span class="ow">or</span> <span class="n">name</span><span class="p">)</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="k">return</span> <span class="n">cte</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a><span class="k">def</span> <span class="nf">_eliminate_cte</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">):</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="n">name</span><span class="p">,</span> <span class="n">cte</span> <span class="o">=</span> <span class="n">_new_cte</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">with_</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="n">with_</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="c1"># Rename references to this CTE</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="k">for</span> <span class="n">table</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">child_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="k">if</span> <span class="n">source</span> <span class="ow">is</span> <span class="n">scope</span><span class="p">:</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="n">new_table</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">table_</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">alias</span><span class="o">=</span><span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">)</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="n">table</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">new_table</span><span class="p">)</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="k">return</span> <span class="n">cte</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a><span class="k">def</span> <span class="nf">_new_cte</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">):</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a><span class="sd"> Returns:</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a><span class="sd"> tuple of (name, cte)</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a><span class="sd"> where `name` is a new name for this CTE in the root scope and `cte` is a new CTE instance.</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a><span class="sd"> If this CTE duplicates an existing CTE, `cte` will be None.</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a><span class="sd"> """</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="n">duplicate_cte_alias</span> <span class="o">=</span> <span class="n">existing_ctes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="p">:</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken</span><span class="o">=</span><span class="n">taken</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="s2">"cte"</span><span class="p">)</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="k">if</span> <span class="n">duplicate_cte_alias</span><span class="p">:</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">duplicate_cte_alias</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="k">elif</span> <span class="n">taken</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">):</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken</span><span class="o">=</span><span class="n">taken</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="n">taken</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">duplicate_cte_alias</span><span class="p">:</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="n">existing_ctes</span><span class="p">[</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">]</span> <span class="o">=</span> <span class="n">name</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="n">cte</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">(</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="n">this</span><span class="o">=</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a> <span class="n">alias</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">name</span><span class="p">)),</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="p">)</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="n">cte</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">cte</span> +</span></pre></div> + + + </section> + <section id="eliminate_subqueries"> + <input id="eliminate_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">eliminate_subqueries</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="eliminate_subqueries-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#eliminate_subqueries"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="eliminate_subqueries-10"><a href="#eliminate_subqueries-10"><span class="linenos">10</span></a><span class="k">def</span> <span class="nf">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="eliminate_subqueries-11"><a href="#eliminate_subqueries-11"><span class="linenos">11</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="eliminate_subqueries-12"><a href="#eliminate_subqueries-12"><span class="linenos">12</span></a><span class="sd"> Rewrite derived tables as CTES, deduplicating if possible.</span> +</span><span id="eliminate_subqueries-13"><a href="#eliminate_subqueries-13"><span class="linenos">13</span></a> +</span><span id="eliminate_subqueries-14"><a href="#eliminate_subqueries-14"><span class="linenos">14</span></a><span class="sd"> Example:</span> +</span><span id="eliminate_subqueries-15"><a href="#eliminate_subqueries-15"><span class="linenos">15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="eliminate_subqueries-16"><a href="#eliminate_subqueries-16"><span class="linenos">16</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT * FROM x) AS y")</span> +</span><span id="eliminate_subqueries-17"><a href="#eliminate_subqueries-17"><span class="linenos">17</span></a><span class="sd"> >>> eliminate_subqueries(expression).sql()</span> +</span><span id="eliminate_subqueries-18"><a href="#eliminate_subqueries-18"><span class="linenos">18</span></a><span class="sd"> 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y'</span> +</span><span id="eliminate_subqueries-19"><a href="#eliminate_subqueries-19"><span class="linenos">19</span></a> +</span><span id="eliminate_subqueries-20"><a href="#eliminate_subqueries-20"><span class="linenos">20</span></a><span class="sd"> This also deduplicates common subqueries:</span> +</span><span id="eliminate_subqueries-21"><a href="#eliminate_subqueries-21"><span class="linenos">21</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT * FROM x) AS y JOIN (SELECT * FROM x) AS z")</span> +</span><span id="eliminate_subqueries-22"><a href="#eliminate_subqueries-22"><span class="linenos">22</span></a><span class="sd"> >>> eliminate_subqueries(expression).sql()</span> +</span><span id="eliminate_subqueries-23"><a href="#eliminate_subqueries-23"><span class="linenos">23</span></a><span class="sd"> 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y JOIN y AS z'</span> +</span><span id="eliminate_subqueries-24"><a href="#eliminate_subqueries-24"><span class="linenos">24</span></a> +</span><span id="eliminate_subqueries-25"><a href="#eliminate_subqueries-25"><span class="linenos">25</span></a><span class="sd"> Args:</span> +</span><span id="eliminate_subqueries-26"><a href="#eliminate_subqueries-26"><span class="linenos">26</span></a><span class="sd"> expression (sqlglot.Expression): expression</span> +</span><span id="eliminate_subqueries-27"><a href="#eliminate_subqueries-27"><span class="linenos">27</span></a><span class="sd"> Returns:</span> +</span><span id="eliminate_subqueries-28"><a href="#eliminate_subqueries-28"><span class="linenos">28</span></a><span class="sd"> sqlglot.Expression: expression</span> +</span><span id="eliminate_subqueries-29"><a href="#eliminate_subqueries-29"><span class="linenos">29</span></a><span class="sd"> """</span> +</span><span id="eliminate_subqueries-30"><a href="#eliminate_subqueries-30"><span class="linenos">30</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="eliminate_subqueries-31"><a href="#eliminate_subqueries-31"><span class="linenos">31</span></a> <span class="c1"># It's possible to have subqueries at the root, e.g. (SELECT * FROM x) LIMIT 1</span> +</span><span id="eliminate_subqueries-32"><a href="#eliminate_subqueries-32"><span class="linenos">32</span></a> <span class="n">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="eliminate_subqueries-33"><a href="#eliminate_subqueries-33"><span class="linenos">33</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="eliminate_subqueries-34"><a href="#eliminate_subqueries-34"><span class="linenos">34</span></a> +</span><span id="eliminate_subqueries-35"><a href="#eliminate_subqueries-35"><span class="linenos">35</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="eliminate_subqueries-36"><a href="#eliminate_subqueries-36"><span class="linenos">36</span></a> <span class="n">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="eliminate_subqueries-37"><a href="#eliminate_subqueries-37"><span class="linenos">37</span></a> +</span><span id="eliminate_subqueries-38"><a href="#eliminate_subqueries-38"><span class="linenos">38</span></a> <span class="c1"># Map of alias->Scope|Table</span> +</span><span id="eliminate_subqueries-39"><a href="#eliminate_subqueries-39"><span class="linenos">39</span></a> <span class="c1"># These are all aliases that are already used in the expression.</span> +</span><span id="eliminate_subqueries-40"><a href="#eliminate_subqueries-40"><span class="linenos">40</span></a> <span class="c1"># We don't want to create new CTEs that conflict with these names.</span> +</span><span id="eliminate_subqueries-41"><a href="#eliminate_subqueries-41"><span class="linenos">41</span></a> <span class="n">taken</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="eliminate_subqueries-42"><a href="#eliminate_subqueries-42"><span class="linenos">42</span></a> +</span><span id="eliminate_subqueries-43"><a href="#eliminate_subqueries-43"><span class="linenos">43</span></a> <span class="c1"># All CTE aliases in the root scope are taken</span> +</span><span id="eliminate_subqueries-44"><a href="#eliminate_subqueries-44"><span class="linenos">44</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">:</span> +</span><span id="eliminate_subqueries-45"><a href="#eliminate_subqueries-45"><span class="linenos">45</span></a> <span class="n">taken</span><span class="p">[</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span> +</span><span id="eliminate_subqueries-46"><a href="#eliminate_subqueries-46"><span class="linenos">46</span></a> +</span><span id="eliminate_subqueries-47"><a href="#eliminate_subqueries-47"><span class="linenos">47</span></a> <span class="c1"># All table names are taken</span> +</span><span id="eliminate_subqueries-48"><a href="#eliminate_subqueries-48"><span class="linenos">48</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="eliminate_subqueries-49"><a href="#eliminate_subqueries-49"><span class="linenos">49</span></a> <span class="n">taken</span><span class="o">.</span><span class="n">update</span><span class="p">(</span> +</span><span id="eliminate_subqueries-50"><a href="#eliminate_subqueries-50"><span class="linenos">50</span></a> <span class="p">{</span> +</span><span id="eliminate_subqueries-51"><a href="#eliminate_subqueries-51"><span class="linenos">51</span></a> <span class="n">source</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">source</span> +</span><span id="eliminate_subqueries-52"><a href="#eliminate_subqueries-52"><span class="linenos">52</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> +</span><span id="eliminate_subqueries-53"><a href="#eliminate_subqueries-53"><span class="linenos">53</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> +</span><span id="eliminate_subqueries-54"><a href="#eliminate_subqueries-54"><span class="linenos">54</span></a> <span class="p">}</span> +</span><span id="eliminate_subqueries-55"><a href="#eliminate_subqueries-55"><span class="linenos">55</span></a> <span class="p">)</span> +</span><span id="eliminate_subqueries-56"><a href="#eliminate_subqueries-56"><span class="linenos">56</span></a> +</span><span id="eliminate_subqueries-57"><a href="#eliminate_subqueries-57"><span class="linenos">57</span></a> <span class="c1"># Map of Expression->alias</span> +</span><span id="eliminate_subqueries-58"><a href="#eliminate_subqueries-58"><span class="linenos">58</span></a> <span class="c1"># Existing CTES in the root expression. We'll use this for deduplication.</span> +</span><span id="eliminate_subqueries-59"><a href="#eliminate_subqueries-59"><span class="linenos">59</span></a> <span class="n">existing_ctes</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="eliminate_subqueries-60"><a href="#eliminate_subqueries-60"><span class="linenos">60</span></a> +</span><span id="eliminate_subqueries-61"><a href="#eliminate_subqueries-61"><span class="linenos">61</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">root</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">"with"</span><span class="p">)</span> +</span><span id="eliminate_subqueries-62"><a href="#eliminate_subqueries-62"><span class="linenos">62</span></a> <span class="n">recursive</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="eliminate_subqueries-63"><a href="#eliminate_subqueries-63"><span class="linenos">63</span></a> <span class="k">if</span> <span class="n">with_</span><span class="p">:</span> +</span><span id="eliminate_subqueries-64"><a href="#eliminate_subqueries-64"><span class="linenos">64</span></a> <span class="n">recursive</span> <span class="o">=</span> <span class="n">with_</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">"recursive"</span><span class="p">)</span> +</span><span id="eliminate_subqueries-65"><a href="#eliminate_subqueries-65"><span class="linenos">65</span></a> <span class="k">for</span> <span class="n">cte</span> <span class="ow">in</span> <span class="n">with_</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="eliminate_subqueries-66"><a href="#eliminate_subqueries-66"><span class="linenos">66</span></a> <span class="n">existing_ctes</span><span class="p">[</span><span class="n">cte</span><span class="o">.</span><span class="n">this</span><span class="p">]</span> <span class="o">=</span> <span class="n">cte</span><span class="o">.</span><span class="n">alias</span> +</span><span id="eliminate_subqueries-67"><a href="#eliminate_subqueries-67"><span class="linenos">67</span></a> <span class="n">new_ctes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="eliminate_subqueries-68"><a href="#eliminate_subqueries-68"><span class="linenos">68</span></a> +</span><span id="eliminate_subqueries-69"><a href="#eliminate_subqueries-69"><span class="linenos">69</span></a> <span class="c1"># We're adding more CTEs, but we want to maintain the DAG order.</span> +</span><span id="eliminate_subqueries-70"><a href="#eliminate_subqueries-70"><span class="linenos">70</span></a> <span class="c1"># Derived tables within an existing CTE need to come before the existing CTE.</span> +</span><span id="eliminate_subqueries-71"><a href="#eliminate_subqueries-71"><span class="linenos">71</span></a> <span class="k">for</span> <span class="n">cte_scope</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">:</span> +</span><span id="eliminate_subqueries-72"><a href="#eliminate_subqueries-72"><span class="linenos">72</span></a> <span class="c1"># Append all the new CTEs from this existing CTE</span> +</span><span id="eliminate_subqueries-73"><a href="#eliminate_subqueries-73"><span class="linenos">73</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">cte_scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="eliminate_subqueries-74"><a href="#eliminate_subqueries-74"><span class="linenos">74</span></a> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="n">cte_scope</span><span class="p">:</span> +</span><span id="eliminate_subqueries-75"><a href="#eliminate_subqueries-75"><span class="linenos">75</span></a> <span class="c1"># Don't try to eliminate this CTE itself</span> +</span><span id="eliminate_subqueries-76"><a href="#eliminate_subqueries-76"><span class="linenos">76</span></a> <span class="k">continue</span> +</span><span id="eliminate_subqueries-77"><a href="#eliminate_subqueries-77"><span class="linenos">77</span></a> <span class="n">new_cte</span> <span class="o">=</span> <span class="n">_eliminate</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="eliminate_subqueries-78"><a href="#eliminate_subqueries-78"><span class="linenos">78</span></a> <span class="k">if</span> <span class="n">new_cte</span><span class="p">:</span> +</span><span id="eliminate_subqueries-79"><a href="#eliminate_subqueries-79"><span class="linenos">79</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_cte</span><span class="p">)</span> +</span><span id="eliminate_subqueries-80"><a href="#eliminate_subqueries-80"><span class="linenos">80</span></a> +</span><span id="eliminate_subqueries-81"><a href="#eliminate_subqueries-81"><span class="linenos">81</span></a> <span class="c1"># Append the existing CTE itself</span> +</span><span id="eliminate_subqueries-82"><a href="#eliminate_subqueries-82"><span class="linenos">82</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cte_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span> +</span><span id="eliminate_subqueries-83"><a href="#eliminate_subqueries-83"><span class="linenos">83</span></a> +</span><span id="eliminate_subqueries-84"><a href="#eliminate_subqueries-84"><span class="linenos">84</span></a> <span class="c1"># Now append the rest</span> +</span><span id="eliminate_subqueries-85"><a href="#eliminate_subqueries-85"><span class="linenos">85</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> +</span><span id="eliminate_subqueries-86"><a href="#eliminate_subqueries-86"><span class="linenos">86</span></a> <span class="n">root</span><span class="o">.</span><span class="n">union_scopes</span><span class="p">,</span> <span class="n">root</span><span class="o">.</span><span class="n">subquery_scopes</span><span class="p">,</span> <span class="n">root</span><span class="o">.</span><span class="n">derived_table_scopes</span> +</span><span id="eliminate_subqueries-87"><a href="#eliminate_subqueries-87"><span class="linenos">87</span></a> <span class="p">):</span> +</span><span id="eliminate_subqueries-88"><a href="#eliminate_subqueries-88"><span class="linenos">88</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="eliminate_subqueries-89"><a href="#eliminate_subqueries-89"><span class="linenos">89</span></a> <span class="n">new_cte</span> <span class="o">=</span> <span class="n">_eliminate</span><span class="p">(</span><span class="n">child_scope</span><span class="p">,</span> <span class="n">existing_ctes</span><span class="p">,</span> <span class="n">taken</span><span class="p">)</span> +</span><span id="eliminate_subqueries-90"><a href="#eliminate_subqueries-90"><span class="linenos">90</span></a> <span class="k">if</span> <span class="n">new_cte</span><span class="p">:</span> +</span><span id="eliminate_subqueries-91"><a href="#eliminate_subqueries-91"><span class="linenos">91</span></a> <span class="n">new_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_cte</span><span class="p">)</span> +</span><span id="eliminate_subqueries-92"><a href="#eliminate_subqueries-92"><span class="linenos">92</span></a> +</span><span id="eliminate_subqueries-93"><a href="#eliminate_subqueries-93"><span class="linenos">93</span></a> <span class="k">if</span> <span class="n">new_ctes</span><span class="p">:</span> +</span><span id="eliminate_subqueries-94"><a href="#eliminate_subqueries-94"><span class="linenos">94</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"with"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">With</span><span class="p">(</span><span class="n">expressions</span><span class="o">=</span><span class="n">new_ctes</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="n">recursive</span><span class="p">))</span> +</span><span id="eliminate_subqueries-95"><a href="#eliminate_subqueries-95"><span class="linenos">95</span></a> +</span><span id="eliminate_subqueries-96"><a href="#eliminate_subqueries-96"><span class="linenos">96</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite derived tables as CTES, deduplicating if possible.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT a FROM (SELECT * FROM x) AS y"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="this-also-deduplicates-common-subqueries">This also deduplicates common subqueries:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT a FROM (SELECT * FROM x) AS y JOIN (SELECT * FROM x) AS z"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">eliminate_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y JOIN y AS z'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/expand_laterals.html b/docs/sqlglot/optimizer/expand_laterals.html new file mode 100644 index 0000000..89944f9 --- /dev/null +++ b/docs/sqlglot/optimizer/expand_laterals.html @@ -0,0 +1,353 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.expand_laterals API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#expand_laterals">expand_laterals</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/expand_laterals.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.expand_laterals </h1> + + + <input id="mod-expand_laterals-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-expand_laterals-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">annotations</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">expand_laterals</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> Expand lateral column alias references.</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> This assumes `qualify_columns` as already run.</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> Example:</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="sd"> >>> sql = "SELECT x.a + 1 AS b, b + 1 AS c FROM x"</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> >>> expand_laterals(expression).sql()</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="sd"> 'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="sd"> Args:</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a><span class="sd"> expression: expression to optimize</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a><span class="sd"> Returns:</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a><span class="sd"> optimized expression</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a><span class="sd"> """</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">alias_to_expression</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="k">for</span> <span class="n">projection</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">projection</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">alias_to_expression</span><span class="p">:</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">column</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">alias_to_expression</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">projection</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="n">alias_to_expression</span><span class="p">[</span><span class="n">projection</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">projection</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="expand_laterals"> + <input id="expand_laterals-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">expand_laterals</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="expand_laterals-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#expand_laterals"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="expand_laterals-9"><a href="#expand_laterals-9"><span class="linenos"> 9</span></a><span class="k">def</span> <span class="nf">expand_laterals</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="expand_laterals-10"><a href="#expand_laterals-10"><span class="linenos">10</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="expand_laterals-11"><a href="#expand_laterals-11"><span class="linenos">11</span></a><span class="sd"> Expand lateral column alias references.</span> +</span><span id="expand_laterals-12"><a href="#expand_laterals-12"><span class="linenos">12</span></a> +</span><span id="expand_laterals-13"><a href="#expand_laterals-13"><span class="linenos">13</span></a><span class="sd"> This assumes `qualify_columns` as already run.</span> +</span><span id="expand_laterals-14"><a href="#expand_laterals-14"><span class="linenos">14</span></a> +</span><span id="expand_laterals-15"><a href="#expand_laterals-15"><span class="linenos">15</span></a><span class="sd"> Example:</span> +</span><span id="expand_laterals-16"><a href="#expand_laterals-16"><span class="linenos">16</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="expand_laterals-17"><a href="#expand_laterals-17"><span class="linenos">17</span></a><span class="sd"> >>> sql = "SELECT x.a + 1 AS b, b + 1 AS c FROM x"</span> +</span><span id="expand_laterals-18"><a href="#expand_laterals-18"><span class="linenos">18</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="expand_laterals-19"><a href="#expand_laterals-19"><span class="linenos">19</span></a><span class="sd"> >>> expand_laterals(expression).sql()</span> +</span><span id="expand_laterals-20"><a href="#expand_laterals-20"><span class="linenos">20</span></a><span class="sd"> 'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'</span> +</span><span id="expand_laterals-21"><a href="#expand_laterals-21"><span class="linenos">21</span></a> +</span><span id="expand_laterals-22"><a href="#expand_laterals-22"><span class="linenos">22</span></a><span class="sd"> Args:</span> +</span><span id="expand_laterals-23"><a href="#expand_laterals-23"><span class="linenos">23</span></a><span class="sd"> expression: expression to optimize</span> +</span><span id="expand_laterals-24"><a href="#expand_laterals-24"><span class="linenos">24</span></a><span class="sd"> Returns:</span> +</span><span id="expand_laterals-25"><a href="#expand_laterals-25"><span class="linenos">25</span></a><span class="sd"> optimized expression</span> +</span><span id="expand_laterals-26"><a href="#expand_laterals-26"><span class="linenos">26</span></a><span class="sd"> """</span> +</span><span id="expand_laterals-27"><a href="#expand_laterals-27"><span class="linenos">27</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="expand_laterals-28"><a href="#expand_laterals-28"><span class="linenos">28</span></a> <span class="n">alias_to_expression</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="expand_laterals-29"><a href="#expand_laterals-29"><span class="linenos">29</span></a> <span class="k">for</span> <span class="n">projection</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="expand_laterals-30"><a href="#expand_laterals-30"><span class="linenos">30</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">projection</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="expand_laterals-31"><a href="#expand_laterals-31"><span class="linenos">31</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">alias_to_expression</span><span class="p">:</span> +</span><span id="expand_laterals-32"><a href="#expand_laterals-32"><span class="linenos">32</span></a> <span class="n">column</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">alias_to_expression</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="expand_laterals-33"><a href="#expand_laterals-33"><span class="linenos">33</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">projection</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="expand_laterals-34"><a href="#expand_laterals-34"><span class="linenos">34</span></a> <span class="n">alias_to_expression</span><span class="p">[</span><span class="n">projection</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">projection</span><span class="o">.</span><span class="n">this</span> +</span><span id="expand_laterals-35"><a href="#expand_laterals-35"><span class="linenos">35</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Expand lateral column alias references.</p> + +<p>This assumes <code>qualify_columns</code> as already run.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"SELECT x.a + 1 AS b, b + 1 AS c FROM x"</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">expand_laterals</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression:</strong> expression to optimize</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>optimized expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/expand_multi_table_selects.html b/docs/sqlglot/optimizer/expand_multi_table_selects.html new file mode 100644 index 0000000..d0751a3 --- /dev/null +++ b/docs/sqlglot/optimizer/expand_multi_table_selects.html @@ -0,0 +1,321 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.expand_multi_table_selects API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#expand_multi_table_selects">expand_multi_table_selects</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/expand_multi_table_selects.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.expand_multi_table_selects </h1> + + + <input id="mod-expand_multi_table_selects-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-expand_multi_table_selects-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="k">def</span> <span class="nf">expand_multi_table_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="sd"> Replace multiple FROM expressions with JOINs.</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> Example:</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> >>> from sqlglot import parse_one</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> >>> expand_multi_table_selects(parse_one("SELECT * FROM x, y")).sql()</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> 'SELECT * FROM x CROSS JOIN y'</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> """</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a> <span class="k">for</span> <span class="n">from_</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> <span class="k">for</span> <span class="n">query</span> <span class="ow">in</span> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a> <span class="n">query</span><span class="p">,</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</span><span class="p">,</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> <span class="p">)</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="expand_multi_table_selects"> + <input id="expand_multi_table_selects-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">expand_multi_table_selects</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="expand_multi_table_selects-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#expand_multi_table_selects"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="expand_multi_table_selects-5"><a href="#expand_multi_table_selects-5"><span class="linenos"> 5</span></a><span class="k">def</span> <span class="nf">expand_multi_table_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="expand_multi_table_selects-6"><a href="#expand_multi_table_selects-6"><span class="linenos"> 6</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="expand_multi_table_selects-7"><a href="#expand_multi_table_selects-7"><span class="linenos"> 7</span></a><span class="sd"> Replace multiple FROM expressions with JOINs.</span> +</span><span id="expand_multi_table_selects-8"><a href="#expand_multi_table_selects-8"><span class="linenos"> 8</span></a> +</span><span id="expand_multi_table_selects-9"><a href="#expand_multi_table_selects-9"><span class="linenos"> 9</span></a><span class="sd"> Example:</span> +</span><span id="expand_multi_table_selects-10"><a href="#expand_multi_table_selects-10"><span class="linenos">10</span></a><span class="sd"> >>> from sqlglot import parse_one</span> +</span><span id="expand_multi_table_selects-11"><a href="#expand_multi_table_selects-11"><span class="linenos">11</span></a><span class="sd"> >>> expand_multi_table_selects(parse_one("SELECT * FROM x, y")).sql()</span> +</span><span id="expand_multi_table_selects-12"><a href="#expand_multi_table_selects-12"><span class="linenos">12</span></a><span class="sd"> 'SELECT * FROM x CROSS JOIN y'</span> +</span><span id="expand_multi_table_selects-13"><a href="#expand_multi_table_selects-13"><span class="linenos">13</span></a><span class="sd"> """</span> +</span><span id="expand_multi_table_selects-14"><a href="#expand_multi_table_selects-14"><span class="linenos">14</span></a> <span class="k">for</span> <span class="n">from_</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="expand_multi_table_selects-15"><a href="#expand_multi_table_selects-15"><span class="linenos">15</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">parent</span> +</span><span id="expand_multi_table_selects-16"><a href="#expand_multi_table_selects-16"><span class="linenos">16</span></a> +</span><span id="expand_multi_table_selects-17"><a href="#expand_multi_table_selects-17"><span class="linenos">17</span></a> <span class="k">for</span> <span class="n">query</span> <span class="ow">in</span> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="expand_multi_table_selects-18"><a href="#expand_multi_table_selects-18"><span class="linenos">18</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="expand_multi_table_selects-19"><a href="#expand_multi_table_selects-19"><span class="linenos">19</span></a> <span class="n">query</span><span class="p">,</span> +</span><span id="expand_multi_table_selects-20"><a href="#expand_multi_table_selects-20"><span class="linenos">20</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</span><span class="p">,</span> +</span><span id="expand_multi_table_selects-21"><a href="#expand_multi_table_selects-21"><span class="linenos">21</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="expand_multi_table_selects-22"><a href="#expand_multi_table_selects-22"><span class="linenos">22</span></a> <span class="p">)</span> +</span><span id="expand_multi_table_selects-23"><a href="#expand_multi_table_selects-23"><span class="linenos">23</span></a> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> +</span><span id="expand_multi_table_selects-24"><a href="#expand_multi_table_selects-24"><span class="linenos">24</span></a> +</span><span id="expand_multi_table_selects-25"><a href="#expand_multi_table_selects-25"><span class="linenos">25</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Replace multiple FROM expressions with JOINs.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">parse_one</span> +<span class="gp">>>> </span><span class="n">expand_multi_table_selects</span><span class="p">(</span><span class="n">parse_one</span><span class="p">(</span><span class="s2">"SELECT * FROM x, y"</span><span class="p">))</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT * FROM x CROSS JOIN y'</span> +</code></pre> + </div> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/isolate_table_selects.html b/docs/sqlglot/optimizer/isolate_table_selects.html new file mode 100644 index 0000000..10413e3 --- /dev/null +++ b/docs/sqlglot/optimizer/isolate_table_selects.html @@ -0,0 +1,317 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.isolate_table_selects API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#isolate_table_selects">isolate_table_selects</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/isolate_table_selects.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.isolate_table_selects </h1> + + + <input id="mod-isolate_table_selects-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-isolate_table_selects-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">alias</span><span class="p">,</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.errors</span> <span class="kn">import</span> <span class="n">OptimizeError</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">traverse_scope</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">isolate_table_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a> <span class="k">continue</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">schema</span><span class="o">.</span><span class="n">column_names</span><span class="p">(</span><span class="n">source</span><span class="p">):</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> <span class="k">continue</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="s2">"Tables require an alias. Run qualify_tables optimization."</span><span class="p">)</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> <span class="n">source</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s2">"*"</span><span class="p">)</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="o">.</span><span class="n">from_</span><span class="p">(</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="n">alias</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">source</span><span class="o">.</span><span class="n">name</span> <span class="ow">or</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="p">)</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="p">)</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="isolate_table_selects"> + <input id="isolate_table_selects-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">isolate_table_selects</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">schema</span><span class="o">=</span><span class="kc">None</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="isolate_table_selects-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#isolate_table_selects"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="isolate_table_selects-8"><a href="#isolate_table_selects-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">isolate_table_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="isolate_table_selects-9"><a href="#isolate_table_selects-9"><span class="linenos"> 9</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="isolate_table_selects-10"><a href="#isolate_table_selects-10"><span class="linenos">10</span></a> +</span><span id="isolate_table_selects-11"><a href="#isolate_table_selects-11"><span class="linenos">11</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="isolate_table_selects-12"><a href="#isolate_table_selects-12"><span class="linenos">12</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="isolate_table_selects-13"><a href="#isolate_table_selects-13"><span class="linenos">13</span></a> <span class="k">continue</span> +</span><span id="isolate_table_selects-14"><a href="#isolate_table_selects-14"><span class="linenos">14</span></a> +</span><span id="isolate_table_selects-15"><a href="#isolate_table_selects-15"><span class="linenos">15</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="isolate_table_selects-16"><a href="#isolate_table_selects-16"><span class="linenos">16</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">schema</span><span class="o">.</span><span class="n">column_names</span><span class="p">(</span><span class="n">source</span><span class="p">):</span> +</span><span id="isolate_table_selects-17"><a href="#isolate_table_selects-17"><span class="linenos">17</span></a> <span class="k">continue</span> +</span><span id="isolate_table_selects-18"><a href="#isolate_table_selects-18"><span class="linenos">18</span></a> +</span><span id="isolate_table_selects-19"><a href="#isolate_table_selects-19"><span class="linenos">19</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="isolate_table_selects-20"><a href="#isolate_table_selects-20"><span class="linenos">20</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="s2">"Tables require an alias. Run qualify_tables optimization."</span><span class="p">)</span> +</span><span id="isolate_table_selects-21"><a href="#isolate_table_selects-21"><span class="linenos">21</span></a> +</span><span id="isolate_table_selects-22"><a href="#isolate_table_selects-22"><span class="linenos">22</span></a> <span class="n">source</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span> +</span><span id="isolate_table_selects-23"><a href="#isolate_table_selects-23"><span class="linenos">23</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s2">"*"</span><span class="p">)</span> +</span><span id="isolate_table_selects-24"><a href="#isolate_table_selects-24"><span class="linenos">24</span></a> <span class="o">.</span><span class="n">from_</span><span class="p">(</span> +</span><span id="isolate_table_selects-25"><a href="#isolate_table_selects-25"><span class="linenos">25</span></a> <span class="n">alias</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">source</span><span class="o">.</span><span class="n">name</span> <span class="ow">or</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> +</span><span id="isolate_table_selects-26"><a href="#isolate_table_selects-26"><span class="linenos">26</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="isolate_table_selects-27"><a href="#isolate_table_selects-27"><span class="linenos">27</span></a> <span class="p">)</span> +</span><span id="isolate_table_selects-28"><a href="#isolate_table_selects-28"><span class="linenos">28</span></a> <span class="o">.</span><span class="n">subquery</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="isolate_table_selects-29"><a href="#isolate_table_selects-29"><span class="linenos">29</span></a> <span class="p">)</span> +</span><span id="isolate_table_selects-30"><a href="#isolate_table_selects-30"><span class="linenos">30</span></a> +</span><span id="isolate_table_selects-31"><a href="#isolate_table_selects-31"><span class="linenos">31</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/lower_identities.html b/docs/sqlglot/optimizer/lower_identities.html new file mode 100644 index 0000000..cf760a6 --- /dev/null +++ b/docs/sqlglot/optimizer/lower_identities.html @@ -0,0 +1,430 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.lower_identities API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#lower_identities">lower_identities</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/lower_identities.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.lower_identities </h1> + + + <input id="mod-lower_identities-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-lower_identities-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">ensure_collection</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="k">def</span> <span class="nf">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="sd"> Convert all unquoted identifiers to lower case.</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Assuming the schema is all lower case, this essentially makes identifiers case-insensitive.</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> Example:</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> >>> lower_identities(expression).sql()</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> 'SELECT bar.a AS A FROM "Foo".bar'</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="sd"> Args:</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> expression (sqlglot.Expression): expression to quote</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="sd"> Returns:</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a><span class="sd"> sqlglot.Expression: quoted expression</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="sd"> """</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="c1"># We need to leave the output aliases unchanged, so the selects need special handling</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="n">_lower_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="c1"># These clauses can reference output aliases and also need special handling</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="n">_lower_order</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">_lower_having</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="c1"># We've already handled these args, so don't traverse into them</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="n">traversed</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="s2">"order"</span><span class="p">,</span> <span class="s2">"having"</span><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="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="c1"># Root subquery, e.g. (SELECT A AS A FROM X) LIMIT 1</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">traversed</span> <span class="o">|=</span> <span class="p">{</span><span class="s2">"this"</span><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="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="c1"># Union, e.g. SELECT A AS A FROM X UNION SELECT A AS A FROM X</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> <span class="n">traversed</span> <span class="o">|=</span> <span class="p">{</span><span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expression"</span><span class="p">}</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a> <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">traversed</span><span class="p">:</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a> <span class="k">continue</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">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">ensure_collection</span><span class="p">(</span><span class="n">v</span><span class="p">):</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">child</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-49"><a href="#L-49"><span class="linenos">49</span></a> <span class="n">child</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_lower</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a> +</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a> +</span><span id="L-54"><a href="#L-54"><span class="linenos">54</span></a><span class="k">def</span> <span class="nf">_lower_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos">55</span></a> <span class="k">for</span> <span class="n">e</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-56"><a href="#L-56"><span class="linenos">56</span></a> <span class="c1"># Leave output aliases as-is</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a> <span class="n">e</span><span class="o">.</span><span class="n">unalias</span><span class="p">()</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_lower</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a><span class="k">def</span> <span class="nf">_lower_order</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a> <span class="n">order</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">"order"</span><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">if</span> <span class="ow">not</span> <span class="n">order</span><span class="p">:</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a> <span class="k">return</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a> <span class="n">output_aliases</span> <span class="o">=</span> <span class="p">{</span><span class="n">e</span><span class="o">.</span><span class="n">alias</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">expressions</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><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">for</span> <span class="n">ordered</span> <span class="ow">in</span> <span class="n">order</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="c1"># Don't lower references to output aliases</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos">71</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ordered</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos">72</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ordered</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos">73</span></a> <span class="ow">and</span> <span class="n">ordered</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">output_aliases</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos">74</span></a> <span class="p">):</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos">75</span></a> <span class="n">ordered</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_lower</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos">76</span></a> +</span><span id="L-77"><a href="#L-77"><span class="linenos">77</span></a> +</span><span id="L-78"><a href="#L-78"><span class="linenos">78</span></a><span class="k">def</span> <span class="nf">_lower_having</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos">79</span></a> <span class="n">having</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">"having"</span><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="k">if</span> <span class="ow">not</span> <span class="n">having</span><span class="p">:</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos">82</span></a> <span class="k">return</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos">83</span></a> +</span><span id="L-84"><a href="#L-84"><span class="linenos">84</span></a> <span class="c1"># Don't lower references to output aliases</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos">85</span></a> <span class="k">for</span> <span class="n">agg</span> <span class="ow">in</span> <span class="n">having</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">):</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos">86</span></a> <span class="n">agg</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_lower</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-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><span id="L-89"><a href="#L-89"><span class="linenos">89</span></a><span class="k">def</span> <span class="nf">_lower</span><span class="p">(</span><span class="n">node</span><span class="p">):</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos">90</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">node</span><span class="o">.</span><span class="n">quoted</span><span class="p">:</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos">91</span></a> <span class="n">node</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">node</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos">92</span></a> <span class="k">return</span> <span class="n">node</span> +</span></pre></div> + + + </section> + <section id="lower_identities"> + <input id="lower_identities-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">lower_identities</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="lower_identities-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#lower_identities"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="lower_identities-6"><a href="#lower_identities-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="lower_identities-7"><a href="#lower_identities-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="lower_identities-8"><a href="#lower_identities-8"><span class="linenos"> 8</span></a><span class="sd"> Convert all unquoted identifiers to lower case.</span> +</span><span id="lower_identities-9"><a href="#lower_identities-9"><span class="linenos"> 9</span></a> +</span><span id="lower_identities-10"><a href="#lower_identities-10"><span class="linenos">10</span></a><span class="sd"> Assuming the schema is all lower case, this essentially makes identifiers case-insensitive.</span> +</span><span id="lower_identities-11"><a href="#lower_identities-11"><span class="linenos">11</span></a> +</span><span id="lower_identities-12"><a href="#lower_identities-12"><span class="linenos">12</span></a><span class="sd"> Example:</span> +</span><span id="lower_identities-13"><a href="#lower_identities-13"><span class="linenos">13</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="lower_identities-14"><a href="#lower_identities-14"><span class="linenos">14</span></a><span class="sd"> >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')</span> +</span><span id="lower_identities-15"><a href="#lower_identities-15"><span class="linenos">15</span></a><span class="sd"> >>> lower_identities(expression).sql()</span> +</span><span id="lower_identities-16"><a href="#lower_identities-16"><span class="linenos">16</span></a><span class="sd"> 'SELECT bar.a AS A FROM "Foo".bar'</span> +</span><span id="lower_identities-17"><a href="#lower_identities-17"><span class="linenos">17</span></a> +</span><span id="lower_identities-18"><a href="#lower_identities-18"><span class="linenos">18</span></a><span class="sd"> Args:</span> +</span><span id="lower_identities-19"><a href="#lower_identities-19"><span class="linenos">19</span></a><span class="sd"> expression (sqlglot.Expression): expression to quote</span> +</span><span id="lower_identities-20"><a href="#lower_identities-20"><span class="linenos">20</span></a><span class="sd"> Returns:</span> +</span><span id="lower_identities-21"><a href="#lower_identities-21"><span class="linenos">21</span></a><span class="sd"> sqlglot.Expression: quoted expression</span> +</span><span id="lower_identities-22"><a href="#lower_identities-22"><span class="linenos">22</span></a><span class="sd"> """</span> +</span><span id="lower_identities-23"><a href="#lower_identities-23"><span class="linenos">23</span></a> <span class="c1"># We need to leave the output aliases unchanged, so the selects need special handling</span> +</span><span id="lower_identities-24"><a href="#lower_identities-24"><span class="linenos">24</span></a> <span class="n">_lower_selects</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="lower_identities-25"><a href="#lower_identities-25"><span class="linenos">25</span></a> +</span><span id="lower_identities-26"><a href="#lower_identities-26"><span class="linenos">26</span></a> <span class="c1"># These clauses can reference output aliases and also need special handling</span> +</span><span id="lower_identities-27"><a href="#lower_identities-27"><span class="linenos">27</span></a> <span class="n">_lower_order</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="lower_identities-28"><a href="#lower_identities-28"><span class="linenos">28</span></a> <span class="n">_lower_having</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="lower_identities-29"><a href="#lower_identities-29"><span class="linenos">29</span></a> +</span><span id="lower_identities-30"><a href="#lower_identities-30"><span class="linenos">30</span></a> <span class="c1"># We've already handled these args, so don't traverse into them</span> +</span><span id="lower_identities-31"><a href="#lower_identities-31"><span class="linenos">31</span></a> <span class="n">traversed</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="s2">"order"</span><span class="p">,</span> <span class="s2">"having"</span><span class="p">}</span> +</span><span id="lower_identities-32"><a href="#lower_identities-32"><span class="linenos">32</span></a> +</span><span id="lower_identities-33"><a href="#lower_identities-33"><span class="linenos">33</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="lower_identities-34"><a href="#lower_identities-34"><span class="linenos">34</span></a> <span class="c1"># Root subquery, e.g. (SELECT A AS A FROM X) LIMIT 1</span> +</span><span id="lower_identities-35"><a href="#lower_identities-35"><span class="linenos">35</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> +</span><span id="lower_identities-36"><a href="#lower_identities-36"><span class="linenos">36</span></a> <span class="n">traversed</span> <span class="o">|=</span> <span class="p">{</span><span class="s2">"this"</span><span class="p">}</span> +</span><span id="lower_identities-37"><a href="#lower_identities-37"><span class="linenos">37</span></a> +</span><span id="lower_identities-38"><a href="#lower_identities-38"><span class="linenos">38</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="lower_identities-39"><a href="#lower_identities-39"><span class="linenos">39</span></a> <span class="c1"># Union, e.g. SELECT A AS A FROM X UNION SELECT A AS A FROM X</span> +</span><span id="lower_identities-40"><a href="#lower_identities-40"><span class="linenos">40</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> +</span><span id="lower_identities-41"><a href="#lower_identities-41"><span class="linenos">41</span></a> <span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">)</span> +</span><span id="lower_identities-42"><a href="#lower_identities-42"><span class="linenos">42</span></a> <span class="n">traversed</span> <span class="o">|=</span> <span class="p">{</span><span class="s2">"this"</span><span class="p">,</span> <span class="s2">"expression"</span><span class="p">}</span> +</span><span id="lower_identities-43"><a href="#lower_identities-43"><span class="linenos">43</span></a> +</span><span id="lower_identities-44"><a href="#lower_identities-44"><span class="linenos">44</span></a> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="lower_identities-45"><a href="#lower_identities-45"><span class="linenos">45</span></a> <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">traversed</span><span class="p">:</span> +</span><span id="lower_identities-46"><a href="#lower_identities-46"><span class="linenos">46</span></a> <span class="k">continue</span> +</span><span id="lower_identities-47"><a href="#lower_identities-47"><span class="linenos">47</span></a> +</span><span id="lower_identities-48"><a href="#lower_identities-48"><span class="linenos">48</span></a> <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">ensure_collection</span><span class="p">(</span><span class="n">v</span><span class="p">):</span> +</span><span id="lower_identities-49"><a href="#lower_identities-49"><span class="linenos">49</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">child</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="lower_identities-50"><a href="#lower_identities-50"><span class="linenos">50</span></a> <span class="n">child</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_lower</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="lower_identities-51"><a href="#lower_identities-51"><span class="linenos">51</span></a> +</span><span id="lower_identities-52"><a href="#lower_identities-52"><span class="linenos">52</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Convert all unquoted identifiers to lower case.</p> + +<p>Assuming the schema is all lower case, this essentially makes identifiers case-insensitive.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s1">'SELECT Bar.A AS A FROM "Foo".Bar'</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">lower_identities</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT bar.a AS A FROM "Foo".bar'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to quote</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: quoted expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/merge_subqueries.html b/docs/sqlglot/optimizer/merge_subqueries.html new file mode 100644 index 0000000..9ffb070 --- /dev/null +++ b/docs/sqlglot/optimizer/merge_subqueries.html @@ -0,0 +1,794 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.merge_subqueries API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#merge_subqueries">merge_subqueries</a> + </li> + <li> + <a class="function" href="#merge_ctes">merge_ctes</a> + </li> + <li> + <a class="function" href="#merge_derived_tables">merge_derived_tables</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/merge_subqueries.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.merge_subqueries </h1> + + + <input id="mod-merge_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-merge_subqueries-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">expressions</span> <span class="k">as</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">find_new_name</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">simplify</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="k">def</span> <span class="nf">merge_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> Rewrite sqlglot AST to merge derived tables into the outer query.</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> This also merges CTEs if they are selected from only once.</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> Example:</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT x.a FROM x) JOIN y")</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> >>> merge_subqueries(expression).sql()</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> 'SELECT x.a FROM x JOIN y'</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> If `leave_tables_isolated` is True, this will not merge inner queries into outer</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> queries if it would result in multiple table selects in a single query:</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT x.a FROM x) JOIN y")</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> >>> merge_subqueries(expression, leave_tables_isolated=True).sql()</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> 'SELECT a FROM (SELECT x.a FROM x) JOIN y'</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> Inspired by https://dev.mysql.com/doc/refman/8.0/en/derived-table-optimization.html</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a><span class="sd"> Args:</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a><span class="sd"> leave_tables_isolated (bool):</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a><span class="sd"> Returns:</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a><span class="sd"> """</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">merge_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">)</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">merge_derived_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">)</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="k">return</span> <span class="n">expression</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><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a><span class="c1"># If a derived table has these Select args, it can't be merged</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a><span class="n">UNMERGABLE_ARGS</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="o">.</span><span class="n">arg_types</span><span class="p">)</span> <span class="o">-</span> <span class="p">{</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="s2">"expressions"</span><span class="p">,</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="s2">"from"</span><span class="p">,</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="s2">"joins"</span><span class="p">,</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="s2">"where"</span><span class="p">,</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="s2">"order"</span><span class="p">,</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="s2">"hint"</span><span class="p">,</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a><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><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a><span class="k">def</span> <span class="nf">merge_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="n">scopes</span> <span class="o">=</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="c1"># All places where we select from CTEs.</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="c1"># We key on the CTE scope so we can detect CTES that are selected from multiple times.</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="n">cte_selections</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="k">for</span> <span class="n">outer_scope</span> <span class="ow">in</span> <span class="n">scopes</span><span class="p">:</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="k">for</span> <span class="n">table</span><span class="p">,</span> <span class="n">inner_scope</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">is_cte</span><span class="p">:</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">cte_selections</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">)]</span><span class="o">.</span><span class="n">append</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 class="n">outer_scope</span><span class="p">,</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="n">inner_scope</span><span class="p">,</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">table</span><span class="p">,</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <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="n">singular_cte_selections</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">cte_selections</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="k">for</span> <span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">singular_cte_selections</span><span class="p">:</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="n">from_or_join</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="k">if</span> <span class="n">_mergeable</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">_rename_inner_sources</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="n">_merge_from</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="n">_merge_expressions</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">_merge_joins</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="n">_merge_where</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">_merge_order</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">_merge_hints</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="n">_pop_cte</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">)</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a><span class="k">def</span> <span class="nf">merge_derived_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="k">for</span> <span class="n">outer_scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="k">for</span> <span class="n">subquery</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="n">from_or_join</span> <span class="o">=</span> <span class="n">subquery</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">subquery</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="n">inner_scope</span> <span class="o">=</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="k">if</span> <span class="n">_mergeable</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="n">_rename_inner_sources</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="n">_merge_from</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">subquery</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="n">_merge_expressions</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="n">_merge_joins</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="n">_merge_where</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">_merge_order</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="n">_merge_hints</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> +</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="k">def</span> <span class="nf">_mergeable</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a><span class="sd"> Return True if `inner_select` can be merged into outer query.</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a><span class="sd"> Args:</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a><span class="sd"> outer_scope (Scope)</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a><span class="sd"> inner_scope (Scope)</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a><span class="sd"> leave_tables_isolated (bool)</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a><span class="sd"> from_or_join (exp.From|exp.Join)</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a><span class="sd"> Returns:</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a><span class="sd"> bool: True if can be merged</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a><span class="sd"> """</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="n">inner_select</span> <span class="o">=</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</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">_is_a_window_expression_in_unmergable_operation</span><span class="p">():</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">window_expressions</span> <span class="o">=</span> <span class="n">inner_select</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Window</span><span class="p">)</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">window_alias_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">window</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias_or_name</span> <span class="k">for</span> <span class="n">window</span> <span class="ow">in</span> <span class="n">window_expressions</span><span class="p">}</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="n">inner_select_name</span> <span class="o">=</span> <span class="n">inner_select</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="n">unmergable_window_columns</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="n">column</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">columns</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Group</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Order</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="p">)</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="p">]</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="n">window_expressions_in_unmergable</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="n">column</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">unmergable_window_columns</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">inner_select_name</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">window_alias_names</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="p">]</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">window_expressions_in_unmergable</span><span class="p">)</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="k">def</span> <span class="nf">_outer_select_joins_on_inner_select_join</span><span class="p">():</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a><span class="sd"> All columns from the inner select in the ON clause must be from the first FROM table.</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a><span class="sd"> That is, this can be merged:</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a><span class="sd"> SELECT * FROM x JOIN (SELECT y.a AS a FROM y JOIN z) AS q ON x.a = q.a</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a><span class="sd"> ^^^ ^</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a><span class="sd"> But this can't:</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a><span class="sd"> SELECT * FROM x JOIN (SELECT z.a AS a FROM y JOIN z) AS q ON x.a = q.a</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="sd"> """</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">from_or_join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="k">return</span> <span class="kc">False</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">from_or_join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">from_or_join</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">"on"</span><span class="p">)</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">on</span><span class="p">:</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="k">return</span> <span class="kc">False</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="n">selections</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">alias</span><span class="p">]</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> <span class="n">inner_from</span> <span class="o">=</span> <span class="n">inner_scope</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">"from"</span><span class="p">)</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">inner_from</span><span class="p">:</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="k">return</span> <span class="kc">False</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">inner_from_table</span> <span class="o">=</span> <span class="n">inner_from</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">alias_or_name</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="n">inner_projections</span> <span class="o">=</span> <span class="p">{</span><span class="n">s</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">selects</span><span class="p">}</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="k">return</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="n">col</span><span class="o">.</span><span class="n">table</span> <span class="o">!=</span> <span class="n">inner_from_table</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> <span class="k">for</span> <span class="n">selection</span> <span class="ow">in</span> <span class="n">selections</span> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">inner_projections</span><span class="p">[</span><span class="n">selection</span><span class="p">]</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="p">)</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="k">return</span> <span class="p">(</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inner_select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">inner_select</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="n">arg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">UNMERGABLE_ARGS</span><span class="p">)</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="ow">and</span> <span class="n">inner_select</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">)</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</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="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">inner_select</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="n">leave_tables_isolated</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">outer_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">)</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">from_or_join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="ow">and</span> <span class="n">inner_select</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">"where"</span><span class="p">)</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="ow">and</span> <span class="n">from_or_join</span><span class="o">.</span><span class="n">side</span> <span class="ow">in</span> <span class="p">{</span><span class="s2">"FULL"</span><span class="p">,</span> <span class="s2">"LEFT"</span><span class="p">,</span> <span class="s2">"RIGHT"</span><span class="p">}</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="p">)</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">from_or_join</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-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="ow">and</span> <span class="n">inner_select</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">"where"</span><span class="p">)</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="ow">and</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="n">j</span><span class="o">.</span><span class="n">side</span> <span class="ow">in</span> <span class="p">{</span><span class="s2">"FULL"</span><span class="p">,</span> <span class="s2">"RIGHT"</span><span class="p">}</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">outer_scope</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">"joins"</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="p">)</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="p">)</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">_outer_select_joins_on_inner_select_join</span><span class="p">()</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">_is_a_window_expression_in_unmergable_operation</span><span class="p">()</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="p">)</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a><span class="k">def</span> <span class="nf">_rename_inner_sources</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a><span class="sd"> Renames any sources in the inner query that conflict with names in the outer query.</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a><span class="sd"> Args:</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a><span class="sd"> alias (str)</span> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a><span class="sd"> """</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="n">taken</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">outer_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="n">conflicts</span> <span class="o">=</span> <span class="n">taken</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">inner_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">))</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> <span class="n">conflicts</span> <span class="o">-=</span> <span class="p">{</span><span class="n">alias</span><span class="p">}</span> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">for</span> <span class="n">conflict</span> <span class="ow">in</span> <span class="n">conflicts</span><span class="p">:</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="n">new_name</span> <span class="o">=</span> <span class="n">find_new_name</span><span class="p">(</span><span class="n">taken</span><span class="p">,</span> <span class="n">conflict</span><span class="p">)</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> <span class="n">source</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">[</span><span class="n">conflict</span><span class="p">]</span> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="n">new_alias</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">new_name</span><span class="p">)</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">new_alias</span><span class="p">))</span> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> <span class="ow">and</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">new_alias</span><span class="p">)</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> <span class="n">source</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">new_alias</span><span class="p">))</span> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">source_columns</span><span class="p">(</span><span class="n">conflict</span><span class="p">):</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="n">column</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"table"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">new_name</span><span class="p">))</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a> <span class="n">inner_scope</span><span class="o">.</span><span class="n">rename_source</span><span class="p">(</span><span class="n">conflict</span><span class="p">,</span> <span class="n">new_name</span><span class="p">)</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a><span class="k">def</span> <span class="nf">_merge_from</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">node_to_replace</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a><span class="sd"> Merge FROM clause of inner query into outer query.</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a><span class="sd"> Args:</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a><span class="sd"> node_to_replace (exp.Subquery|exp.Table)</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a><span class="sd"> alias (str)</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a><span class="sd"> """</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> <span class="n">new_subquery</span> <span class="o">=</span> <span class="n">inner_scope</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">"from"</span><span class="p">)</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="n">node_to_replace</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">new_subquery</span><span class="p">)</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> <span class="k">for</span> <span class="n">join_hint</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">join_hints</span><span class="p">:</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">join_hint</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> <span class="k">if</span> <span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span> <span class="o">==</span> <span class="n">node_to_replace</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> <span class="n">table</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">to_identifier</span><span class="p">(</span><span class="n">new_subquery</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">))</span> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">remove_source</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">add_source</span><span class="p">(</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> <span class="n">new_subquery</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">new_subquery</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">]</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> <span class="p">)</span> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a><span class="k">def</span> <span class="nf">_merge_joins</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a><span class="sd"> Merge JOIN clauses of inner query into outer query.</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a><span class="sd"> Args:</span> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a><span class="sd"> from_or_join (exp.From|exp.Join)</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a><span class="sd"> """</span> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a> <span class="n">new_joins</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> <span class="n">comma_joins</span> <span class="o">=</span> <span class="n">inner_scope</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">"from"</span><span class="p">)</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="k">for</span> <span class="n">subquery</span> <span class="ow">in</span> <span class="n">comma_joins</span><span class="p">:</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> <span class="n">new_joins</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">Join</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">subquery</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s2">"CROSS"</span><span class="p">))</span> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">add_source</span><span class="p">(</span><span class="n">subquery</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">subquery</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">])</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> <span class="n">joins</span> <span class="o">=</span> <span class="n">inner_scope</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">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]</span> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">joins</span><span class="p">:</span> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="n">new_joins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">join</span><span class="p">)</span> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">add_source</span><span class="p">(</span><span class="n">join</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">join</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">])</span> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a> <span class="k">if</span> <span class="n">new_joins</span><span class="p">:</span> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a> <span class="n">outer_joins</span> <span class="o">=</span> <span class="n">outer_scope</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">"joins"</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a> <span class="c1"># Maintain the join order</span> +</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">from_or_join</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-271"><a href="#L-271"><span class="linenos">271</span></a> <span class="n">position</span> <span class="o">=</span> <span class="mi">0</span> +</span><span id="L-272"><a href="#L-272"><span class="linenos">272</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-273"><a href="#L-273"><span class="linenos">273</span></a> <span class="n">position</span> <span class="o">=</span> <span class="n">outer_joins</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">from_or_join</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> +</span><span id="L-274"><a href="#L-274"><span class="linenos">274</span></a> <span class="n">outer_joins</span><span class="p">[</span><span class="n">position</span><span class="p">:</span><span class="n">position</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_joins</span> +</span><span id="L-275"><a href="#L-275"><span class="linenos">275</span></a> +</span><span id="L-276"><a href="#L-276"><span class="linenos">276</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">,</span> <span class="n">outer_joins</span><span class="p">)</span> +</span><span id="L-277"><a href="#L-277"><span class="linenos">277</span></a> +</span><span id="L-278"><a href="#L-278"><span class="linenos">278</span></a> +</span><span id="L-279"><a href="#L-279"><span class="linenos">279</span></a><span class="k">def</span> <span class="nf">_merge_expressions</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span> +</span><span id="L-280"><a href="#L-280"><span class="linenos">280</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-281"><a href="#L-281"><span class="linenos">281</span></a><span class="sd"> Merge projections of inner query into outer query.</span> +</span><span id="L-282"><a href="#L-282"><span class="linenos">282</span></a> +</span><span id="L-283"><a href="#L-283"><span class="linenos">283</span></a><span class="sd"> Args:</span> +</span><span id="L-284"><a href="#L-284"><span class="linenos">284</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-285"><a href="#L-285"><span class="linenos">285</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-286"><a href="#L-286"><span class="linenos">286</span></a><span class="sd"> alias (str)</span> +</span><span id="L-287"><a href="#L-287"><span class="linenos">287</span></a><span class="sd"> """</span> +</span><span id="L-288"><a href="#L-288"><span class="linenos">288</span></a> <span class="c1"># Collect all columns that reference the alias of the inner query</span> +</span><span id="L-289"><a href="#L-289"><span class="linenos">289</span></a> <span class="n">outer_columns</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span> +</span><span id="L-290"><a href="#L-290"><span class="linenos">290</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="L-291"><a href="#L-291"><span class="linenos">291</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">alias</span><span class="p">:</span> +</span><span id="L-292"><a href="#L-292"><span class="linenos">292</span></a> <span class="n">outer_columns</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> +</span><span id="L-293"><a href="#L-293"><span class="linenos">293</span></a> +</span><span id="L-294"><a href="#L-294"><span class="linenos">294</span></a> <span class="c1"># Replace columns with the projection expression in the inner query</span> +</span><span id="L-295"><a href="#L-295"><span class="linenos">295</span></a> <span class="k">for</span> <span class="n">expression</span> <span class="ow">in</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-296"><a href="#L-296"><span class="linenos">296</span></a> <span class="n">projection_name</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-297"><a href="#L-297"><span class="linenos">297</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">projection_name</span><span class="p">:</span> +</span><span id="L-298"><a href="#L-298"><span class="linenos">298</span></a> <span class="k">continue</span> +</span><span id="L-299"><a href="#L-299"><span class="linenos">299</span></a> <span class="n">columns_to_replace</span> <span class="o">=</span> <span class="n">outer_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">projection_name</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="L-300"><a href="#L-300"><span class="linenos">300</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns_to_replace</span><span class="p">:</span> +</span><span id="L-301"><a href="#L-301"><span class="linenos">301</span></a> <span class="n">column</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unalias</span><span class="p">()</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="L-302"><a href="#L-302"><span class="linenos">302</span></a> +</span><span id="L-303"><a href="#L-303"><span class="linenos">303</span></a> +</span><span id="L-304"><a href="#L-304"><span class="linenos">304</span></a><span class="k">def</span> <span class="nf">_merge_where</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="L-305"><a href="#L-305"><span class="linenos">305</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-306"><a href="#L-306"><span class="linenos">306</span></a><span class="sd"> Merge WHERE clause of inner query into outer query.</span> +</span><span id="L-307"><a href="#L-307"><span class="linenos">307</span></a> +</span><span id="L-308"><a href="#L-308"><span class="linenos">308</span></a><span class="sd"> Args:</span> +</span><span id="L-309"><a href="#L-309"><span class="linenos">309</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-310"><a href="#L-310"><span class="linenos">310</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-311"><a href="#L-311"><span class="linenos">311</span></a><span class="sd"> from_or_join (exp.From|exp.Join)</span> +</span><span id="L-312"><a href="#L-312"><span class="linenos">312</span></a><span class="sd"> """</span> +</span><span id="L-313"><a href="#L-313"><span class="linenos">313</span></a> <span class="n">where</span> <span class="o">=</span> <span class="n">inner_scope</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">"where"</span><span class="p">)</span> +</span><span id="L-314"><a href="#L-314"><span class="linenos">314</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">where</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">:</span> +</span><span id="L-315"><a href="#L-315"><span class="linenos">315</span></a> <span class="k">return</span> +</span><span id="L-316"><a href="#L-316"><span class="linenos">316</span></a> +</span><span id="L-317"><a href="#L-317"><span class="linenos">317</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">from_or_join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-318"><a href="#L-318"><span class="linenos">318</span></a> <span class="c1"># Merge predicates from an outer join to the ON clause</span> +</span><span id="L-319"><a href="#L-319"><span class="linenos">319</span></a> <span class="n">from_or_join</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-320"><a href="#L-320"><span class="linenos">320</span></a> <span class="n">from_or_join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"on"</span><span class="p">,</span> <span class="n">simplify</span><span class="p">(</span><span class="n">from_or_join</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">"on"</span><span class="p">)))</span> +</span><span id="L-321"><a href="#L-321"><span class="linenos">321</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-322"><a href="#L-322"><span class="linenos">322</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-323"><a href="#L-323"><span class="linenos">323</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"where"</span><span class="p">,</span> <span class="n">simplify</span><span class="p">(</span><span class="n">outer_scope</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">"where"</span><span class="p">)))</span> +</span><span id="L-324"><a href="#L-324"><span class="linenos">324</span></a> +</span><span id="L-325"><a href="#L-325"><span class="linenos">325</span></a> +</span><span id="L-326"><a href="#L-326"><span class="linenos">326</span></a><span class="k">def</span> <span class="nf">_merge_order</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">):</span> +</span><span id="L-327"><a href="#L-327"><span class="linenos">327</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-328"><a href="#L-328"><span class="linenos">328</span></a><span class="sd"> Merge ORDER clause of inner query into outer query.</span> +</span><span id="L-329"><a href="#L-329"><span class="linenos">329</span></a> +</span><span id="L-330"><a href="#L-330"><span class="linenos">330</span></a><span class="sd"> Args:</span> +</span><span id="L-331"><a href="#L-331"><span class="linenos">331</span></a><span class="sd"> outer_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-332"><a href="#L-332"><span class="linenos">332</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-333"><a href="#L-333"><span class="linenos">333</span></a><span class="sd"> """</span> +</span><span id="L-334"><a href="#L-334"><span class="linenos">334</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-335"><a href="#L-335"><span class="linenos">335</span></a> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-336"><a href="#L-336"><span class="linenos">336</span></a> <span class="n">outer_scope</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="n">arg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">"group"</span><span class="p">,</span> <span class="s2">"distinct"</span><span class="p">,</span> <span class="s2">"having"</span><span class="p">,</span> <span class="s2">"order"</span><span class="p">]</span> +</span><span id="L-337"><a href="#L-337"><span class="linenos">337</span></a> <span class="p">)</span> +</span><span id="L-338"><a href="#L-338"><span class="linenos">338</span></a> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">outer_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span> +</span><span id="L-339"><a href="#L-339"><span class="linenos">339</span></a> <span class="ow">or</span> <span class="nb">any</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">)</span> <span class="k">for</span> <span class="n">expression</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> +</span><span id="L-340"><a href="#L-340"><span class="linenos">340</span></a> <span class="p">):</span> +</span><span id="L-341"><a href="#L-341"><span class="linenos">341</span></a> <span class="k">return</span> +</span><span id="L-342"><a href="#L-342"><span class="linenos">342</span></a> +</span><span id="L-343"><a href="#L-343"><span class="linenos">343</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"order"</span><span class="p">,</span> <span class="n">inner_scope</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">"order"</span><span class="p">))</span> +</span><span id="L-344"><a href="#L-344"><span class="linenos">344</span></a> +</span><span id="L-345"><a href="#L-345"><span class="linenos">345</span></a> +</span><span id="L-346"><a href="#L-346"><span class="linenos">346</span></a><span class="k">def</span> <span class="nf">_merge_hints</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">):</span> +</span><span id="L-347"><a href="#L-347"><span class="linenos">347</span></a> <span class="n">inner_scope_hint</span> <span class="o">=</span> <span class="n">inner_scope</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">"hint"</span><span class="p">)</span> +</span><span id="L-348"><a href="#L-348"><span class="linenos">348</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">inner_scope_hint</span><span class="p">:</span> +</span><span id="L-349"><a href="#L-349"><span class="linenos">349</span></a> <span class="k">return</span> +</span><span id="L-350"><a href="#L-350"><span class="linenos">350</span></a> <span class="n">outer_scope_hint</span> <span class="o">=</span> <span class="n">outer_scope</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">"hint"</span><span class="p">)</span> +</span><span id="L-351"><a href="#L-351"><span class="linenos">351</span></a> <span class="k">if</span> <span class="n">outer_scope_hint</span><span class="p">:</span> +</span><span id="L-352"><a href="#L-352"><span class="linenos">352</span></a> <span class="k">for</span> <span class="n">hint_expression</span> <span class="ow">in</span> <span class="n">inner_scope_hint</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-353"><a href="#L-353"><span class="linenos">353</span></a> <span class="n">outer_scope_hint</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">hint_expression</span><span class="p">)</span> +</span><span id="L-354"><a href="#L-354"><span class="linenos">354</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-355"><a href="#L-355"><span class="linenos">355</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"hint"</span><span class="p">,</span> <span class="n">inner_scope_hint</span><span class="p">)</span> +</span><span id="L-356"><a href="#L-356"><span class="linenos">356</span></a> +</span><span id="L-357"><a href="#L-357"><span class="linenos">357</span></a> +</span><span id="L-358"><a href="#L-358"><span class="linenos">358</span></a><span class="k">def</span> <span class="nf">_pop_cte</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">):</span> +</span><span id="L-359"><a href="#L-359"><span class="linenos">359</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-360"><a href="#L-360"><span class="linenos">360</span></a><span class="sd"> Remove CTE from the AST.</span> +</span><span id="L-361"><a href="#L-361"><span class="linenos">361</span></a> +</span><span id="L-362"><a href="#L-362"><span class="linenos">362</span></a><span class="sd"> Args:</span> +</span><span id="L-363"><a href="#L-363"><span class="linenos">363</span></a><span class="sd"> inner_scope (sqlglot.optimizer.scope.Scope)</span> +</span><span id="L-364"><a href="#L-364"><span class="linenos">364</span></a><span class="sd"> """</span> +</span><span id="L-365"><a href="#L-365"><span class="linenos">365</span></a> <span class="n">cte</span> <span class="o">=</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-366"><a href="#L-366"><span class="linenos">366</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">cte</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-367"><a href="#L-367"><span class="linenos">367</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">with_</span><span class="o">.</span><span class="n">expressions</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="L-368"><a href="#L-368"><span class="linenos">368</span></a> <span class="n">with_</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span><span id="L-369"><a href="#L-369"><span class="linenos">369</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-370"><a href="#L-370"><span class="linenos">370</span></a> <span class="n">cte</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> +</span></pre></div> + + + </section> + <section id="merge_subqueries"> + <input id="merge_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">merge_subqueries</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="merge_subqueries-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#merge_subqueries"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="merge_subqueries-10"><a href="#merge_subqueries-10"><span class="linenos">10</span></a><span class="k">def</span> <span class="nf">merge_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="merge_subqueries-11"><a href="#merge_subqueries-11"><span class="linenos">11</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="merge_subqueries-12"><a href="#merge_subqueries-12"><span class="linenos">12</span></a><span class="sd"> Rewrite sqlglot AST to merge derived tables into the outer query.</span> +</span><span id="merge_subqueries-13"><a href="#merge_subqueries-13"><span class="linenos">13</span></a> +</span><span id="merge_subqueries-14"><a href="#merge_subqueries-14"><span class="linenos">14</span></a><span class="sd"> This also merges CTEs if they are selected from only once.</span> +</span><span id="merge_subqueries-15"><a href="#merge_subqueries-15"><span class="linenos">15</span></a> +</span><span id="merge_subqueries-16"><a href="#merge_subqueries-16"><span class="linenos">16</span></a><span class="sd"> Example:</span> +</span><span id="merge_subqueries-17"><a href="#merge_subqueries-17"><span class="linenos">17</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="merge_subqueries-18"><a href="#merge_subqueries-18"><span class="linenos">18</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT x.a FROM x) JOIN y")</span> +</span><span id="merge_subqueries-19"><a href="#merge_subqueries-19"><span class="linenos">19</span></a><span class="sd"> >>> merge_subqueries(expression).sql()</span> +</span><span id="merge_subqueries-20"><a href="#merge_subqueries-20"><span class="linenos">20</span></a><span class="sd"> 'SELECT x.a FROM x JOIN y'</span> +</span><span id="merge_subqueries-21"><a href="#merge_subqueries-21"><span class="linenos">21</span></a> +</span><span id="merge_subqueries-22"><a href="#merge_subqueries-22"><span class="linenos">22</span></a><span class="sd"> If `leave_tables_isolated` is True, this will not merge inner queries into outer</span> +</span><span id="merge_subqueries-23"><a href="#merge_subqueries-23"><span class="linenos">23</span></a><span class="sd"> queries if it would result in multiple table selects in a single query:</span> +</span><span id="merge_subqueries-24"><a href="#merge_subqueries-24"><span class="linenos">24</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT x.a FROM x) JOIN y")</span> +</span><span id="merge_subqueries-25"><a href="#merge_subqueries-25"><span class="linenos">25</span></a><span class="sd"> >>> merge_subqueries(expression, leave_tables_isolated=True).sql()</span> +</span><span id="merge_subqueries-26"><a href="#merge_subqueries-26"><span class="linenos">26</span></a><span class="sd"> 'SELECT a FROM (SELECT x.a FROM x) JOIN y'</span> +</span><span id="merge_subqueries-27"><a href="#merge_subqueries-27"><span class="linenos">27</span></a> +</span><span id="merge_subqueries-28"><a href="#merge_subqueries-28"><span class="linenos">28</span></a><span class="sd"> Inspired by https://dev.mysql.com/doc/refman/8.0/en/derived-table-optimization.html</span> +</span><span id="merge_subqueries-29"><a href="#merge_subqueries-29"><span class="linenos">29</span></a> +</span><span id="merge_subqueries-30"><a href="#merge_subqueries-30"><span class="linenos">30</span></a><span class="sd"> Args:</span> +</span><span id="merge_subqueries-31"><a href="#merge_subqueries-31"><span class="linenos">31</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="merge_subqueries-32"><a href="#merge_subqueries-32"><span class="linenos">32</span></a><span class="sd"> leave_tables_isolated (bool):</span> +</span><span id="merge_subqueries-33"><a href="#merge_subqueries-33"><span class="linenos">33</span></a><span class="sd"> Returns:</span> +</span><span id="merge_subqueries-34"><a href="#merge_subqueries-34"><span class="linenos">34</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="merge_subqueries-35"><a href="#merge_subqueries-35"><span class="linenos">35</span></a><span class="sd"> """</span> +</span><span id="merge_subqueries-36"><a href="#merge_subqueries-36"><span class="linenos">36</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">merge_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">)</span> +</span><span id="merge_subqueries-37"><a href="#merge_subqueries-37"><span class="linenos">37</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">merge_derived_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">)</span> +</span><span id="merge_subqueries-38"><a href="#merge_subqueries-38"><span class="linenos">38</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to merge derived tables into the outer query.</p> + +<p>This also merges CTEs if they are selected from only once.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT a FROM (SELECT x.a FROM x) JOIN y"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">merge_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT x.a FROM x JOIN y'</span> +</code></pre> + </div> +</blockquote> + +<p>If <code>leave_tables_isolated</code> is True, this will not merge inner queries into outer +queries if it would result in multiple table selects in a single query:</p> + +<blockquote> + <blockquote> + <blockquote> + <p>expression = <a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a>("SELECT a FROM (SELECT x.a FROM x) JOIN y") + merge_subqueries(expression, leave_tables_isolated=True).sql() + 'SELECT a FROM (SELECT x.a FROM x) JOIN y'</p> + </blockquote> + </blockquote> +</blockquote> + +<p>Inspired by <a href="https://dev.mysql.com/doc/refman/8.0/en/derived-table-optimization.html">https://dev.mysql.com/doc/refman/8.0/en/derived-table-optimization.html</a></p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +<li><strong>leave_tables_isolated (bool):</strong> </li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + <section id="merge_ctes"> + <input id="merge_ctes-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">merge_ctes</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="merge_ctes-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#merge_ctes"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="merge_ctes-52"><a href="#merge_ctes-52"><span class="linenos">52</span></a><span class="k">def</span> <span class="nf">merge_ctes</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="merge_ctes-53"><a href="#merge_ctes-53"><span class="linenos">53</span></a> <span class="n">scopes</span> <span class="o">=</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="merge_ctes-54"><a href="#merge_ctes-54"><span class="linenos">54</span></a> +</span><span id="merge_ctes-55"><a href="#merge_ctes-55"><span class="linenos">55</span></a> <span class="c1"># All places where we select from CTEs.</span> +</span><span id="merge_ctes-56"><a href="#merge_ctes-56"><span class="linenos">56</span></a> <span class="c1"># We key on the CTE scope so we can detect CTES that are selected from multiple times.</span> +</span><span id="merge_ctes-57"><a href="#merge_ctes-57"><span class="linenos">57</span></a> <span class="n">cte_selections</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span> +</span><span id="merge_ctes-58"><a href="#merge_ctes-58"><span class="linenos">58</span></a> <span class="k">for</span> <span class="n">outer_scope</span> <span class="ow">in</span> <span class="n">scopes</span><span class="p">:</span> +</span><span id="merge_ctes-59"><a href="#merge_ctes-59"><span class="linenos">59</span></a> <span class="k">for</span> <span class="n">table</span><span class="p">,</span> <span class="n">inner_scope</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="merge_ctes-60"><a href="#merge_ctes-60"><span class="linenos">60</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">inner_scope</span><span class="o">.</span><span class="n">is_cte</span><span class="p">:</span> +</span><span id="merge_ctes-61"><a href="#merge_ctes-61"><span class="linenos">61</span></a> <span class="n">cte_selections</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">)]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> +</span><span id="merge_ctes-62"><a href="#merge_ctes-62"><span class="linenos">62</span></a> <span class="p">(</span> +</span><span id="merge_ctes-63"><a href="#merge_ctes-63"><span class="linenos">63</span></a> <span class="n">outer_scope</span><span class="p">,</span> +</span><span id="merge_ctes-64"><a href="#merge_ctes-64"><span class="linenos">64</span></a> <span class="n">inner_scope</span><span class="p">,</span> +</span><span id="merge_ctes-65"><a href="#merge_ctes-65"><span class="linenos">65</span></a> <span class="n">table</span><span class="p">,</span> +</span><span id="merge_ctes-66"><a href="#merge_ctes-66"><span class="linenos">66</span></a> <span class="p">)</span> +</span><span id="merge_ctes-67"><a href="#merge_ctes-67"><span class="linenos">67</span></a> <span class="p">)</span> +</span><span id="merge_ctes-68"><a href="#merge_ctes-68"><span class="linenos">68</span></a> +</span><span id="merge_ctes-69"><a href="#merge_ctes-69"><span class="linenos">69</span></a> <span class="n">singular_cte_selections</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">cte_selections</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span> +</span><span id="merge_ctes-70"><a href="#merge_ctes-70"><span class="linenos">70</span></a> <span class="k">for</span> <span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">singular_cte_selections</span><span class="p">:</span> +</span><span id="merge_ctes-71"><a href="#merge_ctes-71"><span class="linenos">71</span></a> <span class="n">from_or_join</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> +</span><span id="merge_ctes-72"><a href="#merge_ctes-72"><span class="linenos">72</span></a> <span class="k">if</span> <span class="n">_mergeable</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="merge_ctes-73"><a href="#merge_ctes-73"><span class="linenos">73</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="merge_ctes-74"><a href="#merge_ctes-74"><span class="linenos">74</span></a> <span class="n">_rename_inner_sources</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_ctes-75"><a href="#merge_ctes-75"><span class="linenos">75</span></a> <span class="n">_merge_from</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_ctes-76"><a href="#merge_ctes-76"><span class="linenos">76</span></a> <span class="n">_merge_expressions</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_ctes-77"><a href="#merge_ctes-77"><span class="linenos">77</span></a> <span class="n">_merge_joins</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="merge_ctes-78"><a href="#merge_ctes-78"><span class="linenos">78</span></a> <span class="n">_merge_where</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="merge_ctes-79"><a href="#merge_ctes-79"><span class="linenos">79</span></a> <span class="n">_merge_order</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="merge_ctes-80"><a href="#merge_ctes-80"><span class="linenos">80</span></a> <span class="n">_merge_hints</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="merge_ctes-81"><a href="#merge_ctes-81"><span class="linenos">81</span></a> <span class="n">_pop_cte</span><span class="p">(</span><span class="n">inner_scope</span><span class="p">)</span> +</span><span id="merge_ctes-82"><a href="#merge_ctes-82"><span class="linenos">82</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="merge_ctes-83"><a href="#merge_ctes-83"><span class="linenos">83</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + <section id="merge_derived_tables"> + <input id="merge_derived_tables-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">merge_derived_tables</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="merge_derived_tables-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#merge_derived_tables"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="merge_derived_tables-86"><a href="#merge_derived_tables-86"><span class="linenos"> 86</span></a><span class="k">def</span> <span class="nf">merge_derived_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="merge_derived_tables-87"><a href="#merge_derived_tables-87"><span class="linenos"> 87</span></a> <span class="k">for</span> <span class="n">outer_scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="merge_derived_tables-88"><a href="#merge_derived_tables-88"><span class="linenos"> 88</span></a> <span class="k">for</span> <span class="n">subquery</span> <span class="ow">in</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="merge_derived_tables-89"><a href="#merge_derived_tables-89"><span class="linenos"> 89</span></a> <span class="n">from_or_join</span> <span class="o">=</span> <span class="n">subquery</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> +</span><span id="merge_derived_tables-90"><a href="#merge_derived_tables-90"><span class="linenos"> 90</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">subquery</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="merge_derived_tables-91"><a href="#merge_derived_tables-91"><span class="linenos"> 91</span></a> <span class="n">inner_scope</span> <span class="o">=</span> <span class="n">outer_scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> +</span><span id="merge_derived_tables-92"><a href="#merge_derived_tables-92"><span class="linenos"> 92</span></a> <span class="k">if</span> <span class="n">_mergeable</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">leave_tables_isolated</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">):</span> +</span><span id="merge_derived_tables-93"><a href="#merge_derived_tables-93"><span class="linenos"> 93</span></a> <span class="n">_rename_inner_sources</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_derived_tables-94"><a href="#merge_derived_tables-94"><span class="linenos"> 94</span></a> <span class="n">_merge_from</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">subquery</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_derived_tables-95"><a href="#merge_derived_tables-95"><span class="linenos"> 95</span></a> <span class="n">_merge_expressions</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="merge_derived_tables-96"><a href="#merge_derived_tables-96"><span class="linenos"> 96</span></a> <span class="n">_merge_joins</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="merge_derived_tables-97"><a href="#merge_derived_tables-97"><span class="linenos"> 97</span></a> <span class="n">_merge_where</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">,</span> <span class="n">from_or_join</span><span class="p">)</span> +</span><span id="merge_derived_tables-98"><a href="#merge_derived_tables-98"><span class="linenos"> 98</span></a> <span class="n">_merge_order</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="merge_derived_tables-99"><a href="#merge_derived_tables-99"><span class="linenos"> 99</span></a> <span class="n">_merge_hints</span><span class="p">(</span><span class="n">outer_scope</span><span class="p">,</span> <span class="n">inner_scope</span><span class="p">)</span> +</span><span id="merge_derived_tables-100"><a href="#merge_derived_tables-100"><span class="linenos">100</span></a> <span class="n">outer_scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="merge_derived_tables-101"><a href="#merge_derived_tables-101"><span class="linenos">101</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/normalize.html b/docs/sqlglot/optimizer/normalize.html new file mode 100644 index 0000000..a93d820 --- /dev/null +++ b/docs/sqlglot/optimizer/normalize.html @@ -0,0 +1,585 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.normalize API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#normalize">normalize</a> + </li> + <li> + <a class="function" href="#normalized">normalized</a> + </li> + <li> + <a class="function" href="#normalization_distance">normalization_distance</a> + </li> + <li> + <a class="function" href="#distributive_law">distributive_law</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/normalize.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.normalize </h1> + + + <input id="mod-normalize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-normalize-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">while_changing</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">flatten</span><span class="p">,</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">uniq_sort</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">max_distance</span><span class="o">=</span><span class="mi">128</span><span class="p">):</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> Rewrite sqlglot AST into conjunctive normal form.</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="sd"> Example:</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="sd"> >>> expression = sqlglot.parse_one("(x AND y) OR z")</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> >>> normalize(expression).sql()</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> '(x OR z) AND (y OR z)'</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> Args:</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> expression (sqlglot.Expression): expression to normalize</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> dnf (bool): rewrite in disjunctive normal form instead</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> max_distance (int): the maximal estimated distance from cnf to attempt conversion</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Returns:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> sqlglot.Expression: normalized expression</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> """</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a> +</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">while_changing</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">distributive_law</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">))</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> <span class="k">return</span> <span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a><span class="k">def</span> <span class="nf">normalized</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="n">ancestor</span><span class="p">,</span> <span class="n">root</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> <span class="k">if</span> <span class="n">dnf</span> <span class="k">else</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><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="k">return</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">connector</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">ancestor</span><span class="p">)</span> <span class="k">for</span> <span class="n">connector</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">root</span><span class="p">))</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><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a><span class="k">def</span> <span class="nf">normalization_distance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a><span class="sd"> The difference in the number of predicates between the current expression and the normalized form.</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="sd"> This is used as an estimate of the cost of the conversion which is exponential in complexity.</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a><span class="sd"> Example:</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a><span class="sd"> >>> expression = sqlglot.parse_one("(a AND b) OR (c AND d)")</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a><span class="sd"> >>> normalization_distance(expression)</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a><span class="sd"> 4</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a><span class="sd"> Args:</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a><span class="sd"> expression (sqlglot.Expression): expression to compute distance</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a><span class="sd"> dnf (bool): compute to dnf distance instead</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a><span class="sd"> Returns:</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a><span class="sd"> int: difference</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a><span class="sd"> """</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">))</span> <span class="o">-</span> <span class="p">(</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">)))</span> <span class="o">+</span> <span class="mi">1</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="p">)</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a><span class="k">def</span> <span class="nf">_predicate_lengths</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">):</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a><span class="sd"> Returns a list of predicate lengths when expanded to normalized form.</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a><span class="sd"> (A AND B) OR C -> [2, 2] because len(A OR C), len(B OR C).</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a><span class="sd"> """</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> <span class="k">return</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="n">left</span><span class="p">,</span> <span class="n">right</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">values</span><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="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">if</span> <span class="n">dnf</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">):</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="k">return</span> <span class="p">[</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">dnf</span><span class="p">)</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">right</span><span class="p">,</span> <span class="n">dnf</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 class="k">return</span> <span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">dnf</span><span class="p">)</span> <span class="o">+</span> <span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">right</span><span class="p">,</span> <span class="n">dnf</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a><span class="k">def</span> <span class="nf">distributive_law</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">):</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a><span class="sd"> x OR (y AND z) -> (x OR y) AND (x OR z)</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a><span class="sd"> (x AND y) OR (y AND z) -> (x OR y) AND (x OR z) AND (y OR y) AND (y OR z)</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a><span class="sd"> """</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">if</span> <span class="n">normalization_distance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">)</span> <span class="o">></span> <span class="n">max_distance</span><span class="p">:</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="n">to_exp</span><span class="p">,</span> <span class="n">from_exp</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">if</span> <span class="n">dnf</span> <span class="k">else</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">distributive_law</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">))</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">from_exp</span><span class="p">):</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="n">from_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="n">from_exp</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="n">to_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="n">to_exp</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">)))</span> <span class="o">></span> <span class="nb">len</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">))):</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</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="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a><span class="k">def</span> <span class="nf">_distribute</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">):</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="n">a</span><span class="p">,</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">to_func</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">paren</span><span class="p">(</span><span class="n">from_func</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">left</span><span class="p">)),</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">paren</span><span class="p">(</span><span class="n">from_func</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">right</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="p">)</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">to_func</span><span class="p">(</span><span class="n">from_func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">left</span><span class="p">),</span> <span class="n">from_func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">return</span> <span class="n">_simplify</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> +</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">_simplify</span><span class="p">(</span><span class="n">node</span><span class="p">):</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">uniq_sort</span><span class="p">(</span><span class="n">flatten</span><span class="p">(</span><span class="n">node</span><span class="p">))</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">_simplify</span><span class="p">)</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="n">node</span> +</span></pre></div> + + + </section> + <section id="normalize"> + <input id="normalize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">normalize</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">dnf</span><span class="o">=</span><span class="kc">False</span>, </span><span class="param"><span class="n">max_distance</span><span class="o">=</span><span class="mi">128</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="normalize-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#normalize"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="normalize-7"><a href="#normalize-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">max_distance</span><span class="o">=</span><span class="mi">128</span><span class="p">):</span> +</span><span id="normalize-8"><a href="#normalize-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="normalize-9"><a href="#normalize-9"><span class="linenos"> 9</span></a><span class="sd"> Rewrite sqlglot AST into conjunctive normal form.</span> +</span><span id="normalize-10"><a href="#normalize-10"><span class="linenos">10</span></a> +</span><span id="normalize-11"><a href="#normalize-11"><span class="linenos">11</span></a><span class="sd"> Example:</span> +</span><span id="normalize-12"><a href="#normalize-12"><span class="linenos">12</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="normalize-13"><a href="#normalize-13"><span class="linenos">13</span></a><span class="sd"> >>> expression = sqlglot.parse_one("(x AND y) OR z")</span> +</span><span id="normalize-14"><a href="#normalize-14"><span class="linenos">14</span></a><span class="sd"> >>> normalize(expression).sql()</span> +</span><span id="normalize-15"><a href="#normalize-15"><span class="linenos">15</span></a><span class="sd"> '(x OR z) AND (y OR z)'</span> +</span><span id="normalize-16"><a href="#normalize-16"><span class="linenos">16</span></a> +</span><span id="normalize-17"><a href="#normalize-17"><span class="linenos">17</span></a><span class="sd"> Args:</span> +</span><span id="normalize-18"><a href="#normalize-18"><span class="linenos">18</span></a><span class="sd"> expression (sqlglot.Expression): expression to normalize</span> +</span><span id="normalize-19"><a href="#normalize-19"><span class="linenos">19</span></a><span class="sd"> dnf (bool): rewrite in disjunctive normal form instead</span> +</span><span id="normalize-20"><a href="#normalize-20"><span class="linenos">20</span></a><span class="sd"> max_distance (int): the maximal estimated distance from cnf to attempt conversion</span> +</span><span id="normalize-21"><a href="#normalize-21"><span class="linenos">21</span></a><span class="sd"> Returns:</span> +</span><span id="normalize-22"><a href="#normalize-22"><span class="linenos">22</span></a><span class="sd"> sqlglot.Expression: normalized expression</span> +</span><span id="normalize-23"><a href="#normalize-23"><span class="linenos">23</span></a><span class="sd"> """</span> +</span><span id="normalize-24"><a href="#normalize-24"><span class="linenos">24</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="normalize-25"><a href="#normalize-25"><span class="linenos">25</span></a> +</span><span id="normalize-26"><a href="#normalize-26"><span class="linenos">26</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">while_changing</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">distributive_law</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">))</span> +</span><span id="normalize-27"><a href="#normalize-27"><span class="linenos">27</span></a> <span class="k">return</span> <span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST into conjunctive normal form.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"(x AND y) OR z"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'(x OR z) AND (y OR z)'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to normalize</li> +<li><strong>dnf (bool):</strong> rewrite in disjunctive normal form instead</li> +<li><strong>max_distance (int):</strong> the maximal estimated distance from cnf to attempt conversion</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: normalized expression</p> +</blockquote> +</div> + + + </section> + <section id="normalized"> + <input id="normalized-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">normalized</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">dnf</span><span class="o">=</span><span class="kc">False</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="normalized-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#normalized"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="normalized-30"><a href="#normalized-30"><span class="linenos">30</span></a><span class="k">def</span> <span class="nf">normalized</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="normalized-31"><a href="#normalized-31"><span class="linenos">31</span></a> <span class="n">ancestor</span><span class="p">,</span> <span class="n">root</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> <span class="k">if</span> <span class="n">dnf</span> <span class="k">else</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> +</span><span id="normalized-32"><a href="#normalized-32"><span class="linenos">32</span></a> +</span><span id="normalized-33"><a href="#normalized-33"><span class="linenos">33</span></a> <span class="k">return</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">connector</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">ancestor</span><span class="p">)</span> <span class="k">for</span> <span class="n">connector</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">root</span><span class="p">))</span> +</span></pre></div> + + + + + </section> + <section id="normalization_distance"> + <input id="normalization_distance-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">normalization_distance</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">dnf</span><span class="o">=</span><span class="kc">False</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="normalization_distance-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#normalization_distance"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="normalization_distance-36"><a href="#normalization_distance-36"><span class="linenos">36</span></a><span class="k">def</span> <span class="nf">normalization_distance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="normalization_distance-37"><a href="#normalization_distance-37"><span class="linenos">37</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="normalization_distance-38"><a href="#normalization_distance-38"><span class="linenos">38</span></a><span class="sd"> The difference in the number of predicates between the current expression and the normalized form.</span> +</span><span id="normalization_distance-39"><a href="#normalization_distance-39"><span class="linenos">39</span></a> +</span><span id="normalization_distance-40"><a href="#normalization_distance-40"><span class="linenos">40</span></a><span class="sd"> This is used as an estimate of the cost of the conversion which is exponential in complexity.</span> +</span><span id="normalization_distance-41"><a href="#normalization_distance-41"><span class="linenos">41</span></a> +</span><span id="normalization_distance-42"><a href="#normalization_distance-42"><span class="linenos">42</span></a><span class="sd"> Example:</span> +</span><span id="normalization_distance-43"><a href="#normalization_distance-43"><span class="linenos">43</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="normalization_distance-44"><a href="#normalization_distance-44"><span class="linenos">44</span></a><span class="sd"> >>> expression = sqlglot.parse_one("(a AND b) OR (c AND d)")</span> +</span><span id="normalization_distance-45"><a href="#normalization_distance-45"><span class="linenos">45</span></a><span class="sd"> >>> normalization_distance(expression)</span> +</span><span id="normalization_distance-46"><a href="#normalization_distance-46"><span class="linenos">46</span></a><span class="sd"> 4</span> +</span><span id="normalization_distance-47"><a href="#normalization_distance-47"><span class="linenos">47</span></a> +</span><span id="normalization_distance-48"><a href="#normalization_distance-48"><span class="linenos">48</span></a><span class="sd"> Args:</span> +</span><span id="normalization_distance-49"><a href="#normalization_distance-49"><span class="linenos">49</span></a><span class="sd"> expression (sqlglot.Expression): expression to compute distance</span> +</span><span id="normalization_distance-50"><a href="#normalization_distance-50"><span class="linenos">50</span></a><span class="sd"> dnf (bool): compute to dnf distance instead</span> +</span><span id="normalization_distance-51"><a href="#normalization_distance-51"><span class="linenos">51</span></a><span class="sd"> Returns:</span> +</span><span id="normalization_distance-52"><a href="#normalization_distance-52"><span class="linenos">52</span></a><span class="sd"> int: difference</span> +</span><span id="normalization_distance-53"><a href="#normalization_distance-53"><span class="linenos">53</span></a><span class="sd"> """</span> +</span><span id="normalization_distance-54"><a href="#normalization_distance-54"><span class="linenos">54</span></a> <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">_predicate_lengths</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">))</span> <span class="o">-</span> <span class="p">(</span> +</span><span id="normalization_distance-55"><a href="#normalization_distance-55"><span class="linenos">55</span></a> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">)))</span> <span class="o">+</span> <span class="mi">1</span> +</span><span id="normalization_distance-56"><a href="#normalization_distance-56"><span class="linenos">56</span></a> <span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>The difference in the number of predicates between the current expression and the normalized form.</p> + +<p>This is used as an estimate of the cost of the conversion which is exponential in complexity.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"(a AND b) OR (c AND d)"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">normalization_distance</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +<span class="go">4</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to compute distance</li> +<li><strong>dnf (bool):</strong> compute to dnf distance instead</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>int: difference</p> +</blockquote> +</div> + + + </section> + <section id="distributive_law"> + <input id="distributive_law-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">distributive_law</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">dnf</span>, </span><span class="param"><span class="n">max_distance</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="distributive_law-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#distributive_law"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="distributive_law-79"><a href="#distributive_law-79"><span class="linenos"> 79</span></a><span class="k">def</span> <span class="nf">distributive_law</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">):</span> +</span><span id="distributive_law-80"><a href="#distributive_law-80"><span class="linenos"> 80</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="distributive_law-81"><a href="#distributive_law-81"><span class="linenos"> 81</span></a><span class="sd"> x OR (y AND z) -> (x OR y) AND (x OR z)</span> +</span><span id="distributive_law-82"><a href="#distributive_law-82"><span class="linenos"> 82</span></a><span class="sd"> (x AND y) OR (y AND z) -> (x OR y) AND (x OR z) AND (y OR y) AND (y OR z)</span> +</span><span id="distributive_law-83"><a href="#distributive_law-83"><span class="linenos"> 83</span></a><span class="sd"> """</span> +</span><span id="distributive_law-84"><a href="#distributive_law-84"><span class="linenos"> 84</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="distributive_law-85"><a href="#distributive_law-85"><span class="linenos"> 85</span></a> <span class="k">if</span> <span class="n">normalization_distance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">dnf</span><span class="p">)</span> <span class="o">></span> <span class="n">max_distance</span><span class="p">:</span> +</span><span id="distributive_law-86"><a href="#distributive_law-86"><span class="linenos"> 86</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="distributive_law-87"><a href="#distributive_law-87"><span class="linenos"> 87</span></a> +</span><span id="distributive_law-88"><a href="#distributive_law-88"><span class="linenos"> 88</span></a> <span class="n">to_exp</span><span class="p">,</span> <span class="n">from_exp</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">if</span> <span class="n">dnf</span> <span class="k">else</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> +</span><span id="distributive_law-89"><a href="#distributive_law-89"><span class="linenos"> 89</span></a> +</span><span id="distributive_law-90"><a href="#distributive_law-90"><span class="linenos"> 90</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">distributive_law</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">dnf</span><span class="p">,</span> <span class="n">max_distance</span><span class="p">))</span> +</span><span id="distributive_law-91"><a href="#distributive_law-91"><span class="linenos"> 91</span></a> +</span><span id="distributive_law-92"><a href="#distributive_law-92"><span class="linenos"> 92</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">from_exp</span><span class="p">):</span> +</span><span id="distributive_law-93"><a href="#distributive_law-93"><span class="linenos"> 93</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="distributive_law-94"><a href="#distributive_law-94"><span class="linenos"> 94</span></a> +</span><span id="distributive_law-95"><a href="#distributive_law-95"><span class="linenos"> 95</span></a> <span class="n">from_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="n">from_exp</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="distributive_law-96"><a href="#distributive_law-96"><span class="linenos"> 96</span></a> <span class="n">to_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="n">to_exp</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="distributive_law-97"><a href="#distributive_law-97"><span class="linenos"> 97</span></a> +</span><span id="distributive_law-98"><a href="#distributive_law-98"><span class="linenos"> 98</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="distributive_law-99"><a href="#distributive_law-99"><span class="linenos"> 99</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">)))</span> <span class="o">></span> <span class="nb">len</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">))):</span> +</span><span id="distributive_law-100"><a href="#distributive_law-100"><span class="linenos">100</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="distributive_law-101"><a href="#distributive_law-101"><span class="linenos">101</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="distributive_law-102"><a href="#distributive_law-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="distributive_law-103"><a href="#distributive_law-103"><span class="linenos">103</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="distributive_law-104"><a href="#distributive_law-104"><span class="linenos">104</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">to_exp</span><span class="p">):</span> +</span><span id="distributive_law-105"><a href="#distributive_law-105"><span class="linenos">105</span></a> <span class="k">return</span> <span class="n">_distribute</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">from_func</span><span class="p">,</span> <span class="n">to_func</span><span class="p">)</span> +</span><span id="distributive_law-106"><a href="#distributive_law-106"><span class="linenos">106</span></a> +</span><span id="distributive_law-107"><a href="#distributive_law-107"><span class="linenos">107</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>x OR (y AND z) -> (x OR y) AND (x OR z) +(x AND y) OR (y AND z) -> (x OR y) AND (x OR z) AND (y OR y) AND (y OR z)</p> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/optimize_joins.html b/docs/sqlglot/optimizer/optimize_joins.html new file mode 100644 index 0000000..66891c7 --- /dev/null +++ b/docs/sqlglot/optimizer/optimize_joins.html @@ -0,0 +1,489 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.optimize_joins API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#optimize_joins">optimize_joins</a> + </li> + <li> + <a class="function" href="#reorder_joins">reorder_joins</a> + </li> + <li> + <a class="function" href="#normalize">normalize</a> + </li> + <li> + <a class="function" href="#other_table_names">other_table_names</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/optimize_joins.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.optimize_joins </h1> + + + <input id="mod-optimize_joins-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-optimize_joins-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">tsort</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">simplify</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">optimize_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="sd"> Removes cross joins if possible and reorder joins based on predicate dependencies.</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> Example:</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd"> >>> from sqlglot import parse_one</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> >>> optimize_joins(parse_one("SELECT * FROM x CROSS JOIN y JOIN z ON x.a = z.a AND y.a = z.a")).sql()</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> 'SELECT * FROM x JOIN z ON x.a = z.a AND TRUE JOIN y ON y.a = z.a'</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> """</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> <span class="n">references</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> <span class="n">cross_joins</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">,</span> <span class="p">[]):</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="k">if</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="n">references</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">references</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="p">[])</span> <span class="o">+</span> <span class="p">[</span><span class="n">join</span><span class="p">]</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">cross_joins</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">name</span><span class="p">,</span> <span class="n">join</span><span class="p">))</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">cross_joins</span><span class="p">:</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="k">for</span> <span class="n">dep</span> <span class="ow">in</span> <span class="n">references</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">[]):</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">dep</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"on"</span><span class="p">]</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">on</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">on</span><span class="p">))</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">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">predicate</span><span class="p">):</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos">37</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="n">join</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">reorder_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a> +</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">def</span> <span class="nf">reorder_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos">46</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos">47</span></a><span class="sd"> Reorder joins by topological sort order based on predicate references.</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a><span class="sd"> """</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a> <span class="k">for</span> <span class="n">from_</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a> <span class="n">head</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a> <span class="n">joins</span> <span class="o">=</span> <span class="p">{</span><span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">join</span> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">,</span> <span class="p">[])}</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a> <span class="n">dag</span> <span class="o">=</span> <span class="p">{</span><span class="n">head</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</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="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">joins</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a> <span class="n">dag</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">set</span><span class="p">(</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a> <span class="s2">"joins"</span><span class="p">,</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a> <span class="p">[</span><span class="n">joins</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">tsort</span><span class="p">(</span><span class="n">dag</span><span class="p">)</span> <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="n">head</span><span class="o">.</span><span class="n">alias_or_name</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 class="k">return</span> <span class="n">expression</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a><span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos">67</span></a><span class="sd"> Remove INNER and OUTER from joins as they are optional.</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos">68</span></a><span class="sd"> """</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a> <span class="k">if</span> <span class="n">join</span><span class="o">.</span><span class="n">kind</span> <span class="o">!=</span> <span class="s2">"CROSS"</span><span class="p">:</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos">71</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"kind"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos">72</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos">73</span></a> +</span><span id="L-74"><a href="#L-74"><span class="linenos">74</span></a> +</span><span id="L-75"><a href="#L-75"><span class="linenos">75</span></a><span class="k">def</span> <span class="nf">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">exclude</span><span class="p">):</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos">76</span></a> <span class="k">return</span> <span class="p">[</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos">77</span></a> <span class="n">name</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos">78</span></a> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()))</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos">79</span></a> <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="n">exclude</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos">80</span></a> <span class="p">]</span> +</span></pre></div> + + + </section> + <section id="optimize_joins"> + <input id="optimize_joins-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">optimize_joins</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="optimize_joins-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#optimize_joins"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="optimize_joins-7"><a href="#optimize_joins-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">optimize_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="optimize_joins-8"><a href="#optimize_joins-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="optimize_joins-9"><a href="#optimize_joins-9"><span class="linenos"> 9</span></a><span class="sd"> Removes cross joins if possible and reorder joins based on predicate dependencies.</span> +</span><span id="optimize_joins-10"><a href="#optimize_joins-10"><span class="linenos">10</span></a> +</span><span id="optimize_joins-11"><a href="#optimize_joins-11"><span class="linenos">11</span></a><span class="sd"> Example:</span> +</span><span id="optimize_joins-12"><a href="#optimize_joins-12"><span class="linenos">12</span></a><span class="sd"> >>> from sqlglot import parse_one</span> +</span><span id="optimize_joins-13"><a href="#optimize_joins-13"><span class="linenos">13</span></a><span class="sd"> >>> optimize_joins(parse_one("SELECT * FROM x CROSS JOIN y JOIN z ON x.a = z.a AND y.a = z.a")).sql()</span> +</span><span id="optimize_joins-14"><a href="#optimize_joins-14"><span class="linenos">14</span></a><span class="sd"> 'SELECT * FROM x JOIN z ON x.a = z.a AND TRUE JOIN y ON y.a = z.a'</span> +</span><span id="optimize_joins-15"><a href="#optimize_joins-15"><span class="linenos">15</span></a><span class="sd"> """</span> +</span><span id="optimize_joins-16"><a href="#optimize_joins-16"><span class="linenos">16</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="optimize_joins-17"><a href="#optimize_joins-17"><span class="linenos">17</span></a> <span class="n">references</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="optimize_joins-18"><a href="#optimize_joins-18"><span class="linenos">18</span></a> <span class="n">cross_joins</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="optimize_joins-19"><a href="#optimize_joins-19"><span class="linenos">19</span></a> +</span><span id="optimize_joins-20"><a href="#optimize_joins-20"><span class="linenos">20</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">,</span> <span class="p">[]):</span> +</span><span id="optimize_joins-21"><a href="#optimize_joins-21"><span class="linenos">21</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="optimize_joins-22"><a href="#optimize_joins-22"><span class="linenos">22</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> +</span><span id="optimize_joins-23"><a href="#optimize_joins-23"><span class="linenos">23</span></a> +</span><span id="optimize_joins-24"><a href="#optimize_joins-24"><span class="linenos">24</span></a> <span class="k">if</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="optimize_joins-25"><a href="#optimize_joins-25"><span class="linenos">25</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="optimize_joins-26"><a href="#optimize_joins-26"><span class="linenos">26</span></a> <span class="n">references</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">references</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="p">[])</span> <span class="o">+</span> <span class="p">[</span><span class="n">join</span><span class="p">]</span> +</span><span id="optimize_joins-27"><a href="#optimize_joins-27"><span class="linenos">27</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="optimize_joins-28"><a href="#optimize_joins-28"><span class="linenos">28</span></a> <span class="n">cross_joins</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">name</span><span class="p">,</span> <span class="n">join</span><span class="p">))</span> +</span><span id="optimize_joins-29"><a href="#optimize_joins-29"><span class="linenos">29</span></a> +</span><span id="optimize_joins-30"><a href="#optimize_joins-30"><span class="linenos">30</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">cross_joins</span><span class="p">:</span> +</span><span id="optimize_joins-31"><a href="#optimize_joins-31"><span class="linenos">31</span></a> <span class="k">for</span> <span class="n">dep</span> <span class="ow">in</span> <span class="n">references</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">[]):</span> +</span><span id="optimize_joins-32"><a href="#optimize_joins-32"><span class="linenos">32</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">dep</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"on"</span><span class="p">]</span> +</span><span id="optimize_joins-33"><a href="#optimize_joins-33"><span class="linenos">33</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">on</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">on</span><span class="p">))</span> +</span><span id="optimize_joins-34"><a href="#optimize_joins-34"><span class="linenos">34</span></a> +</span><span id="optimize_joins-35"><a href="#optimize_joins-35"><span class="linenos">35</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">on</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="optimize_joins-36"><a href="#optimize_joins-36"><span class="linenos">36</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">on</span><span class="o">.</span><span class="n">flatten</span><span class="p">():</span> +</span><span id="optimize_joins-37"><a href="#optimize_joins-37"><span class="linenos">37</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">predicate</span><span class="p">):</span> +</span><span id="optimize_joins-38"><a href="#optimize_joins-38"><span class="linenos">38</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="optimize_joins-39"><a href="#optimize_joins-39"><span class="linenos">39</span></a> <span class="n">join</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="optimize_joins-40"><a href="#optimize_joins-40"><span class="linenos">40</span></a> +</span><span id="optimize_joins-41"><a href="#optimize_joins-41"><span class="linenos">41</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">reorder_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="optimize_joins-42"><a href="#optimize_joins-42"><span class="linenos">42</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="optimize_joins-43"><a href="#optimize_joins-43"><span class="linenos">43</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Removes cross joins if possible and reorder joins based on predicate dependencies.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">parse_one</span> +<span class="gp">>>> </span><span class="n">optimize_joins</span><span class="p">(</span><span class="n">parse_one</span><span class="p">(</span><span class="s2">"SELECT * FROM x CROSS JOIN y JOIN z ON x.a = z.a AND y.a = z.a"</span><span class="p">))</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT * FROM x JOIN z ON x.a = z.a AND TRUE JOIN y ON y.a = z.a'</span> +</code></pre> + </div> +</blockquote> +</div> + + + </section> + <section id="reorder_joins"> + <input id="reorder_joins-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">reorder_joins</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="reorder_joins-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#reorder_joins"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="reorder_joins-46"><a href="#reorder_joins-46"><span class="linenos">46</span></a><span class="k">def</span> <span class="nf">reorder_joins</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="reorder_joins-47"><a href="#reorder_joins-47"><span class="linenos">47</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="reorder_joins-48"><a href="#reorder_joins-48"><span class="linenos">48</span></a><span class="sd"> Reorder joins by topological sort order based on predicate references.</span> +</span><span id="reorder_joins-49"><a href="#reorder_joins-49"><span class="linenos">49</span></a><span class="sd"> """</span> +</span><span id="reorder_joins-50"><a href="#reorder_joins-50"><span class="linenos">50</span></a> <span class="k">for</span> <span class="n">from_</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">):</span> +</span><span id="reorder_joins-51"><a href="#reorder_joins-51"><span class="linenos">51</span></a> <span class="n">head</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="reorder_joins-52"><a href="#reorder_joins-52"><span class="linenos">52</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">from_</span><span class="o">.</span><span class="n">parent</span> +</span><span id="reorder_joins-53"><a href="#reorder_joins-53"><span class="linenos">53</span></a> <span class="n">joins</span> <span class="o">=</span> <span class="p">{</span><span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">join</span> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">,</span> <span class="p">[])}</span> +</span><span id="reorder_joins-54"><a href="#reorder_joins-54"><span class="linenos">54</span></a> <span class="n">dag</span> <span class="o">=</span> <span class="p">{</span><span class="n">head</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="p">[]}</span> +</span><span id="reorder_joins-55"><a href="#reorder_joins-55"><span class="linenos">55</span></a> +</span><span id="reorder_joins-56"><a href="#reorder_joins-56"><span class="linenos">56</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">joins</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="reorder_joins-57"><a href="#reorder_joins-57"><span class="linenos">57</span></a> <span class="n">dag</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> +</span><span id="reorder_joins-58"><a href="#reorder_joins-58"><span class="linenos">58</span></a> +</span><span id="reorder_joins-59"><a href="#reorder_joins-59"><span class="linenos">59</span></a> <span class="n">parent</span><span class="o">.</span><span class="n">set</span><span class="p">(</span> +</span><span id="reorder_joins-60"><a href="#reorder_joins-60"><span class="linenos">60</span></a> <span class="s2">"joins"</span><span class="p">,</span> +</span><span id="reorder_joins-61"><a href="#reorder_joins-61"><span class="linenos">61</span></a> <span class="p">[</span><span class="n">joins</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">tsort</span><span class="p">(</span><span class="n">dag</span><span class="p">)</span> <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="n">head</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">],</span> +</span><span id="reorder_joins-62"><a href="#reorder_joins-62"><span class="linenos">62</span></a> <span class="p">)</span> +</span><span id="reorder_joins-63"><a href="#reorder_joins-63"><span class="linenos">63</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Reorder joins by topological sort order based on predicate references.</p> +</div> + + + </section> + <section id="normalize"> + <input id="normalize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">normalize</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="normalize-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#normalize"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="normalize-66"><a href="#normalize-66"><span class="linenos">66</span></a><span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="normalize-67"><a href="#normalize-67"><span class="linenos">67</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="normalize-68"><a href="#normalize-68"><span class="linenos">68</span></a><span class="sd"> Remove INNER and OUTER from joins as they are optional.</span> +</span><span id="normalize-69"><a href="#normalize-69"><span class="linenos">69</span></a><span class="sd"> """</span> +</span><span id="normalize-70"><a href="#normalize-70"><span class="linenos">70</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="normalize-71"><a href="#normalize-71"><span class="linenos">71</span></a> <span class="k">if</span> <span class="n">join</span><span class="o">.</span><span class="n">kind</span> <span class="o">!=</span> <span class="s2">"CROSS"</span><span class="p">:</span> +</span><span id="normalize-72"><a href="#normalize-72"><span class="linenos">72</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"kind"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="normalize-73"><a href="#normalize-73"><span class="linenos">73</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Remove INNER and OUTER from joins as they are optional.</p> +</div> + + + </section> + <section id="other_table_names"> + <input id="other_table_names-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">other_table_names</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">join</span>, </span><span class="param"><span class="n">exclude</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="other_table_names-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#other_table_names"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="other_table_names-76"><a href="#other_table_names-76"><span class="linenos">76</span></a><span class="k">def</span> <span class="nf">other_table_names</span><span class="p">(</span><span class="n">join</span><span class="p">,</span> <span class="n">exclude</span><span class="p">):</span> +</span><span id="other_table_names-77"><a href="#other_table_names-77"><span class="linenos">77</span></a> <span class="k">return</span> <span class="p">[</span> +</span><span id="other_table_names-78"><a href="#other_table_names-78"><span class="linenos">78</span></a> <span class="n">name</span> +</span><span id="other_table_names-79"><a href="#other_table_names-79"><span class="linenos">79</span></a> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()))</span> +</span><span id="other_table_names-80"><a href="#other_table_names-80"><span class="linenos">80</span></a> <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="n">exclude</span> +</span><span id="other_table_names-81"><a href="#other_table_names-81"><span class="linenos">81</span></a> <span class="p">]</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/optimizer.html b/docs/sqlglot/optimizer/optimizer.html new file mode 100644 index 0000000..ca82c2f --- /dev/null +++ b/docs/sqlglot/optimizer/optimizer.html @@ -0,0 +1,401 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.optimizer API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#optimize">optimize</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/optimizer.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.optimizer </h1> + + + <input id="mod-optimizer-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-optimizer-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">sqlglot</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.annotate_types</span> <span class="kn">import</span> <span class="n">annotate_types</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.canonicalize</span> <span class="kn">import</span> <span class="n">canonicalize</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_ctes</span> <span class="kn">import</span> <span class="n">eliminate_ctes</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_joins</span> <span class="kn">import</span> <span class="n">eliminate_joins</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.eliminate_subqueries</span> <span class="kn">import</span> <span class="n">eliminate_subqueries</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_laterals</span> <span class="kn">import</span> <span class="n">expand_laterals</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.expand_multi_table_selects</span> <span class="kn">import</span> <span class="n">expand_multi_table_selects</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.isolate_table_selects</span> <span class="kn">import</span> <span class="n">isolate_table_selects</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.lower_identities</span> <span class="kn">import</span> <span class="n">lower_identities</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.merge_subqueries</span> <span class="kn">import</span> <span class="n">merge_subqueries</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.normalize</span> <span class="kn">import</span> <span class="n">normalize</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.optimize_joins</span> <span class="kn">import</span> <span class="n">optimize_joins</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_predicates</span> <span class="kn">import</span> <span class="n">pushdown_predicates</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.pushdown_projections</span> <span class="kn">import</span> <span class="n">pushdown_projections</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_columns</span> <span class="kn">import</span> <span class="n">qualify_columns</span><span class="p">,</span> <span class="n">validate_qualify_columns</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.qualify_tables</span> <span class="kn">import</span> <span class="n">qualify_tables</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.unnest_subqueries</span> <span class="kn">import</span> <span class="n">unnest_subqueries</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="n">RULES</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="n">lower_identities</span><span class="p">,</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="n">qualify_tables</span><span class="p">,</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="n">isolate_table_selects</span><span class="p">,</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="n">qualify_columns</span><span class="p">,</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="n">expand_laterals</span><span class="p">,</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">validate_qualify_columns</span><span class="p">,</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="n">pushdown_projections</span><span class="p">,</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="n">normalize</span><span class="p">,</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="n">unnest_subqueries</span><span class="p">,</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="n">expand_multi_table_selects</span><span class="p">,</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="n">pushdown_predicates</span><span class="p">,</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="n">optimize_joins</span><span class="p">,</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">eliminate_subqueries</span><span class="p">,</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">merge_subqueries</span><span class="p">,</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos">36</span></a> <span class="n">eliminate_joins</span><span class="p">,</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos">37</span></a> <span class="n">eliminate_ctes</span><span class="p">,</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="n">annotate_types</span><span class="p">,</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="n">canonicalize</span><span class="p">,</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">rules</span><span class="o">=</span><span class="n">RULES</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos">46</span></a> +</span><span id="L-47"><a href="#L-47"><span class="linenos">47</span></a><span class="sd"> Args:</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): database schema.</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a><span class="sd"> the following forms:</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a><span class="sd"> 1. {table: {col: type}}</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos">54</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos">55</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a><span class="sd"> db (str): specify the default database, as might be set by a `USE DATABASE db` statement</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a><span class="sd"> catalog (str): specify the default catalog, as might be set by a `USE CATALOG c` statement</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a><span class="sd"> rules (sequence): sequence of optimizer rules to use.</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a><span class="sd"> what you're doing!</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos">62</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a><span class="sd"> Returns:</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a><span class="sd"> """</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos">67</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos">68</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos">70</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos">71</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos">72</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos">73</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos">74</span></a> <span class="p">}</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos">75</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos">76</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="optimize"> + <input id="optimize-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">optimize</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span>,</span><span class="param"> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">db</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">rules</span><span class="o">=</span><span class="p">(</span><span class="o"><</span><span class="n">function</span> <span class="n">lower_identities</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2f80</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_tables</span> <span class="n">at</span> <span class="mh">0x7ff75a9d9240</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">isolate_table_selects</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2e60</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">qualify_columns</span> <span class="n">at</span> <span class="mh">0x7ff75a9d8820</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_laterals</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2b90</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">validate_qualify_columns</span> <span class="n">at</span> <span class="mh">0x7ff75a9d8c10</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_projections</span> <span class="n">at</span> <span class="mh">0x7ff75a9d85e0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">normalize</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0820</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">unnest_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9d9900</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">expand_multi_table_selects</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2dd0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">pushdown_predicates</span> <span class="n">at</span> <span class="mh">0x7ff75a9d81f0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">optimize_joins</span> <span class="n">at</span> <span class="mh">0x7ff75a9b3d00</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9b2830</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">merge_subqueries</span> <span class="n">at</span> <span class="mh">0x7ff75a9b35b0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_joins</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0700</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">eliminate_ctes</span> <span class="n">at</span> <span class="mh">0x7ff75a9b05e0</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">annotate_types</span> <span class="n">at</span> <span class="mh">0x7ff75a989480</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">function</span> <span class="n">canonicalize</span> <span class="n">at</span> <span class="mh">0x7ff75a9b0160</span><span class="o">></span><span class="p">)</span>,</span><span class="param"> <span class="o">**</span><span class="n">kwargs</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="optimize-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#optimize"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="optimize-44"><a href="#optimize-44"><span class="linenos">44</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">rules</span><span class="o">=</span><span class="n">RULES</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> +</span><span id="optimize-45"><a href="#optimize-45"><span class="linenos">45</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="optimize-46"><a href="#optimize-46"><span class="linenos">46</span></a><span class="sd"> Rewrite a sqlglot AST into an optimized form.</span> +</span><span id="optimize-47"><a href="#optimize-47"><span class="linenos">47</span></a> +</span><span id="optimize-48"><a href="#optimize-48"><span class="linenos">48</span></a><span class="sd"> Args:</span> +</span><span id="optimize-49"><a href="#optimize-49"><span class="linenos">49</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="optimize-50"><a href="#optimize-50"><span class="linenos">50</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): database schema.</span> +</span><span id="optimize-51"><a href="#optimize-51"><span class="linenos">51</span></a><span class="sd"> This can either be an instance of `sqlglot.optimizer.Schema` or a mapping in one of</span> +</span><span id="optimize-52"><a href="#optimize-52"><span class="linenos">52</span></a><span class="sd"> the following forms:</span> +</span><span id="optimize-53"><a href="#optimize-53"><span class="linenos">53</span></a><span class="sd"> 1. {table: {col: type}}</span> +</span><span id="optimize-54"><a href="#optimize-54"><span class="linenos">54</span></a><span class="sd"> 2. {db: {table: {col: type}}}</span> +</span><span id="optimize-55"><a href="#optimize-55"><span class="linenos">55</span></a><span class="sd"> 3. {catalog: {db: {table: {col: type}}}}</span> +</span><span id="optimize-56"><a href="#optimize-56"><span class="linenos">56</span></a><span class="sd"> If no schema is provided then the default schema defined at `sqlgot.schema` will be used</span> +</span><span id="optimize-57"><a href="#optimize-57"><span class="linenos">57</span></a><span class="sd"> db (str): specify the default database, as might be set by a `USE DATABASE db` statement</span> +</span><span id="optimize-58"><a href="#optimize-58"><span class="linenos">58</span></a><span class="sd"> catalog (str): specify the default catalog, as might be set by a `USE CATALOG c` statement</span> +</span><span id="optimize-59"><a href="#optimize-59"><span class="linenos">59</span></a><span class="sd"> rules (sequence): sequence of optimizer rules to use.</span> +</span><span id="optimize-60"><a href="#optimize-60"><span class="linenos">60</span></a><span class="sd"> Many of the rules require tables and columns to be qualified.</span> +</span><span id="optimize-61"><a href="#optimize-61"><span class="linenos">61</span></a><span class="sd"> Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know</span> +</span><span id="optimize-62"><a href="#optimize-62"><span class="linenos">62</span></a><span class="sd"> what you're doing!</span> +</span><span id="optimize-63"><a href="#optimize-63"><span class="linenos">63</span></a><span class="sd"> **kwargs: If a rule has a keyword argument with a same name in **kwargs, it will be passed in.</span> +</span><span id="optimize-64"><a href="#optimize-64"><span class="linenos">64</span></a><span class="sd"> Returns:</span> +</span><span id="optimize-65"><a href="#optimize-65"><span class="linenos">65</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="optimize-66"><a href="#optimize-66"><span class="linenos">66</span></a><span class="sd"> """</span> +</span><span id="optimize-67"><a href="#optimize-67"><span class="linenos">67</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span> <span class="ow">or</span> <span class="n">sqlglot</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span> +</span><span id="optimize-68"><a href="#optimize-68"><span class="linenos">68</span></a> <span class="n">possible_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"db"</span><span class="p">:</span> <span class="n">db</span><span class="p">,</span> <span class="s2">"catalog"</span><span class="p">:</span> <span class="n">catalog</span><span class="p">,</span> <span class="s2">"schema"</span><span class="p">:</span> <span class="n">schema</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span> +</span><span id="optimize-69"><a href="#optimize-69"><span class="linenos">69</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="optimize-70"><a href="#optimize-70"><span class="linenos">70</span></a> <span class="k">for</span> <span class="n">rule</span> <span class="ow">in</span> <span class="n">rules</span><span class="p">:</span> +</span><span id="optimize-71"><a href="#optimize-71"><span class="linenos">71</span></a> <span class="c1"># Find any additional rule parameters, beyond `expression`</span> +</span><span id="optimize-72"><a href="#optimize-72"><span class="linenos">72</span></a> <span class="n">rule_params</span> <span class="o">=</span> <span class="n">rule</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_varnames</span> +</span><span id="optimize-73"><a href="#optimize-73"><span class="linenos">73</span></a> <span class="n">rule_kwargs</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="optimize-74"><a href="#optimize-74"><span class="linenos">74</span></a> <span class="n">param</span><span class="p">:</span> <span class="n">possible_kwargs</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">rule_params</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">possible_kwargs</span> +</span><span id="optimize-75"><a href="#optimize-75"><span class="linenos">75</span></a> <span class="p">}</span> +</span><span id="optimize-76"><a href="#optimize-76"><span class="linenos">76</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">rule</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="o">**</span><span class="n">rule_kwargs</span><span class="p">)</span> +</span><span id="optimize-77"><a href="#optimize-77"><span class="linenos">77</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite a sqlglot AST into an optimized form.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +<li><strong>schema (dict|sqlglot.optimizer.Schema):</strong> database schema. +This can either be an instance of <code>sqlglot.optimizer.Schema</code> or a mapping in one of +the following forms: + 1. {table: {col: type}} + 2. {db: {table: {col: type}}} + 3. {catalog: {db: {table: {col: type}}}} +If no schema is provided then the default schema defined at <code>sqlgot.schema</code> will be used</li> +<li><strong>db (str):</strong> specify the default database, as might be set by a <code>USE DATABASE db</code> statement</li> +<li><strong>catalog (str):</strong> specify the default catalog, as might be set by a <code>USE CATALOG c</code> statement</li> +<li><strong>rules (sequence):</strong> sequence of optimizer rules to use. +Many of the rules require tables and columns to be qualified. +Do not remove qualify_tables or qualify_columns from the sequence of rules unless you know +what you're doing!</li> +<li><strong><em>*kwargs:</strong> If a rule has a keyword argument with a same name in *</em>kwargs, it will be passed in.</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/pushdown_predicates.html b/docs/sqlglot/optimizer/pushdown_predicates.html new file mode 100644 index 0000000..e4e1add --- /dev/null +++ b/docs/sqlglot/optimizer/pushdown_predicates.html @@ -0,0 +1,773 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.pushdown_predicates API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#pushdown_predicates">pushdown_predicates</a> + </li> + <li> + <a class="function" href="#pushdown">pushdown</a> + </li> + <li> + <a class="function" href="#pushdown_cnf">pushdown_cnf</a> + </li> + <li> + <a class="function" href="#pushdown_dnf">pushdown_dnf</a> + </li> + <li> + <a class="function" href="#nodes_for_predicate">nodes_for_predicate</a> + </li> + <li> + <a class="function" href="#replace_aliases">replace_aliases</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/pushdown_predicates.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.pushdown_predicates </h1> + + + <input id="mod-pushdown_predicates-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-pushdown_predicates-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.normalize</span> <span class="kn">import</span> <span class="n">normalized</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">build_scope</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.simplify</span> <span class="kn">import</span> <span class="n">simplify</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">pushdown_predicates</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Rewrite sqlglot AST to pushdown predicates in FROMS and JOINS</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> Example:</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="sd"> >>> sql = "SELECT * FROM (SELECT * FROM x AS x) AS y WHERE y.a = 1"</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> pushdown_predicates(expression).sql()</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> 'SELECT * FROM (SELECT * FROM x AS x WHERE y.a = 1) AS y WHERE TRUE'</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> Args:</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Returns:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> """</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> <span class="n">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a> <span class="n">scope_ref_count</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">ref_count</span><span class="p">()</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">())):</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> <span class="n">where</span> <span class="o">=</span> <span class="n">select</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">"where"</span><span class="p">)</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="k">if</span> <span class="n">where</span><span class="p">:</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="n">selected_sources</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="c1"># a right join can only push down to itself and not the source FROM table</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span> <span class="ow">in</span> <span class="n">selected_sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</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-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> <span class="ow">and</span> <span class="n">parent</span><span class="o">.</span><span class="n">side</span> <span class="o">==</span> <span class="s2">"RIGHT"</span><span class="p">:</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">selected_sources</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">source</span><span class="p">)}</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="k">break</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="n">pushdown</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">selected_sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</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="c1"># joins should only pushdown into itself, not to other joins</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="c1"># so we limit the selected sources to only itself</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="n">pushdown</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">),</span> <span class="p">{</span><span class="n">name</span><span class="p">:</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">[</span><span class="n">name</span><span class="p">]},</span> <span class="n">scope_ref_count</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">expression</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><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a><span class="k">def</span> <span class="nf">pushdown</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">condition</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><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="n">condition</span> <span class="o">=</span> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">condition</span><span class="p">))</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="n">cnf_like</span> <span class="o">=</span> <span class="n">normalized</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">normalized</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">True</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">predicates</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">if</span> <span class="n">cnf_like</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="k">else</span> <span class="p">[</span><span class="n">condition</span><span class="p">]</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="p">)</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="k">if</span> <span class="n">cnf_like</span><span class="p">:</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="n">pushdown_cnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="n">pushdown_dnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a><span class="k">def</span> <span class="nf">pushdown_cnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a><span class="sd"> If the predicates are in CNF like form, we can simply replace each block in the parent.</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a><span class="sd"> """</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="n">node</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="k">break</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">node</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">replace_aliases</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">predicate</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-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><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a><span class="k">def</span> <span class="nf">pushdown_dnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a><span class="sd"> If the predicates are in DNF form, we can only push down conditions that are in all blocks.</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a><span class="sd"> Additionally, we can't remove predicates from their original form.</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a><span class="sd"> """</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="c1"># find all the tables that can be pushdown too</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="c1"># these are tables that are referenced in all blocks of a DNF</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="c1"># (a.x AND b.x) OR (a.y AND c.y)</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="c1"># only table a can be push down</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="n">pushdown_tables</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="n">a_tables</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">a</span><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="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">a_tables</span> <span class="o">&=</span> <span class="nb">set</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">b</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="n">pushdown_tables</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">a_tables</span><span class="p">)</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="p">{}</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="c1"># for every pushdown table, find all related conditions in all predicates</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="c1"># combine them with ORS</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="c1"># (a.x AND and a.y AND b.x) OR (a.z AND c.y) -> (a.x AND a.y) OR (a.z)</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">pushdown_tables</span><span class="p">):</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="n">nodes</span> <span class="o">=</span> <span class="n">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="k">continue</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="n">predicate_condition</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">table</span><span class="p">:</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="n">condition</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">predicate_condition</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">predicate_condition</span><span class="p">,</span> <span class="n">condition</span><span class="p">)</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="k">if</span> <span class="n">predicate_condition</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">else</span> <span class="n">condition</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <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">if</span> <span class="n">predicate_condition</span><span class="p">:</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">conditions</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span><span class="n">conditions</span><span class="p">[</span><span class="n">table</span><span class="p">],</span> <span class="n">predicate_condition</span><span class="p">)</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">conditions</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="k">else</span> <span class="n">predicate_condition</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="p">)</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">nodes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">conditions</span><span class="p">:</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="k">continue</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">conditions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="n">node</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="n">node</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">replace_aliases</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">predicate</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a><span class="k">def</span> <span class="nf">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="n">nodes</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">predicate</span><span class="p">)</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="n">where_condition</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="n">node</span><span class="p">,</span> <span class="n">source</span> <span class="o">=</span> <span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="c1"># if the predicate is in a where statement we can try to push it down</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="c1"># we want to find the root join or from statement</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">and</span> <span class="n">where_condition</span><span class="p">:</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</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-155"><a href="#L-155"><span class="linenos">155</span></a> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="c1"># a node can reference a CTE which should be pushed down</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">parent</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">"with"</span><span class="p">)</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="k">if</span> <span class="n">with_</span> <span class="ow">and</span> <span class="n">with_</span><span class="o">.</span><span class="n">recursive</span><span class="p">:</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="k">return</span> <span class="p">{}</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">side</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">side</span> <span class="o">!=</span> <span class="s2">"RIGHT"</span><span class="p">:</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="k">return</span> <span class="p">{}</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="n">nodes</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">node</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">tables</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="c1"># We can't push down window expressions</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">has_window_expression</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> <span class="n">select</span> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">selects</span> <span class="k">if</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Window</span><span class="p">)</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="p">)</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="c1"># we can't push down predicates to select statements if they are referenced in</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="c1"># multiple places.</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="ow">not</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"group"</span><span class="p">)</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="ow">and</span> <span class="n">scope_ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o"><</span> <span class="mi">2</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">has_window_expression</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="p">):</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="n">nodes</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">node</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="k">return</span> <span class="n">nodes</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a><span class="k">def</span> <span class="nf">replace_aliases</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">predicate</span><span class="p">):</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="n">aliases</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">source</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="n">aliases</span><span class="p">[</span><span class="n">select</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="n">aliases</span><span class="p">[</span><span class="n">select</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">select</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="k">def</span> <span class="nf">_replace_alias</span><span class="p">(</span><span class="n">column</span><span class="p">):</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">aliases</span><span class="p">:</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="k">return</span> <span class="n">aliases</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> <span class="k">return</span> <span class="n">column</span> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> <span class="k">return</span> <span class="n">predicate</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_replace_alias</span><span class="p">)</span> +</span></pre></div> + + + </section> + <section id="pushdown_predicates"> + <input id="pushdown_predicates-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">pushdown_predicates</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="pushdown_predicates-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#pushdown_predicates"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="pushdown_predicates-8"><a href="#pushdown_predicates-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">pushdown_predicates</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="pushdown_predicates-9"><a href="#pushdown_predicates-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="pushdown_predicates-10"><a href="#pushdown_predicates-10"><span class="linenos">10</span></a><span class="sd"> Rewrite sqlglot AST to pushdown predicates in FROMS and JOINS</span> +</span><span id="pushdown_predicates-11"><a href="#pushdown_predicates-11"><span class="linenos">11</span></a> +</span><span id="pushdown_predicates-12"><a href="#pushdown_predicates-12"><span class="linenos">12</span></a><span class="sd"> Example:</span> +</span><span id="pushdown_predicates-13"><a href="#pushdown_predicates-13"><span class="linenos">13</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="pushdown_predicates-14"><a href="#pushdown_predicates-14"><span class="linenos">14</span></a><span class="sd"> >>> sql = "SELECT * FROM (SELECT * FROM x AS x) AS y WHERE y.a = 1"</span> +</span><span id="pushdown_predicates-15"><a href="#pushdown_predicates-15"><span class="linenos">15</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="pushdown_predicates-16"><a href="#pushdown_predicates-16"><span class="linenos">16</span></a><span class="sd"> >>> pushdown_predicates(expression).sql()</span> +</span><span id="pushdown_predicates-17"><a href="#pushdown_predicates-17"><span class="linenos">17</span></a><span class="sd"> 'SELECT * FROM (SELECT * FROM x AS x WHERE y.a = 1) AS y WHERE TRUE'</span> +</span><span id="pushdown_predicates-18"><a href="#pushdown_predicates-18"><span class="linenos">18</span></a> +</span><span id="pushdown_predicates-19"><a href="#pushdown_predicates-19"><span class="linenos">19</span></a><span class="sd"> Args:</span> +</span><span id="pushdown_predicates-20"><a href="#pushdown_predicates-20"><span class="linenos">20</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="pushdown_predicates-21"><a href="#pushdown_predicates-21"><span class="linenos">21</span></a><span class="sd"> Returns:</span> +</span><span id="pushdown_predicates-22"><a href="#pushdown_predicates-22"><span class="linenos">22</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="pushdown_predicates-23"><a href="#pushdown_predicates-23"><span class="linenos">23</span></a><span class="sd"> """</span> +</span><span id="pushdown_predicates-24"><a href="#pushdown_predicates-24"><span class="linenos">24</span></a> <span class="n">root</span> <span class="o">=</span> <span class="n">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="pushdown_predicates-25"><a href="#pushdown_predicates-25"><span class="linenos">25</span></a> <span class="n">scope_ref_count</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">ref_count</span><span class="p">()</span> +</span><span id="pushdown_predicates-26"><a href="#pushdown_predicates-26"><span class="linenos">26</span></a> +</span><span id="pushdown_predicates-27"><a href="#pushdown_predicates-27"><span class="linenos">27</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">traverse</span><span class="p">())):</span> +</span><span id="pushdown_predicates-28"><a href="#pushdown_predicates-28"><span class="linenos">28</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span> +</span><span id="pushdown_predicates-29"><a href="#pushdown_predicates-29"><span class="linenos">29</span></a> <span class="n">where</span> <span class="o">=</span> <span class="n">select</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">"where"</span><span class="p">)</span> +</span><span id="pushdown_predicates-30"><a href="#pushdown_predicates-30"><span class="linenos">30</span></a> <span class="k">if</span> <span class="n">where</span><span class="p">:</span> +</span><span id="pushdown_predicates-31"><a href="#pushdown_predicates-31"><span class="linenos">31</span></a> <span class="n">selected_sources</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span> +</span><span id="pushdown_predicates-32"><a href="#pushdown_predicates-32"><span class="linenos">32</span></a> <span class="c1"># a right join can only push down to itself and not the source FROM table</span> +</span><span id="pushdown_predicates-33"><a href="#pushdown_predicates-33"><span class="linenos">33</span></a> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span> <span class="ow">in</span> <span class="n">selected_sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="pushdown_predicates-34"><a href="#pushdown_predicates-34"><span class="linenos">34</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</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="pushdown_predicates-35"><a href="#pushdown_predicates-35"><span class="linenos">35</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)</span> <span class="ow">and</span> <span class="n">parent</span><span class="o">.</span><span class="n">side</span> <span class="o">==</span> <span class="s2">"RIGHT"</span><span class="p">:</span> +</span><span id="pushdown_predicates-36"><a href="#pushdown_predicates-36"><span class="linenos">36</span></a> <span class="n">selected_sources</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">source</span><span class="p">)}</span> +</span><span id="pushdown_predicates-37"><a href="#pushdown_predicates-37"><span class="linenos">37</span></a> <span class="k">break</span> +</span><span id="pushdown_predicates-38"><a href="#pushdown_predicates-38"><span class="linenos">38</span></a> <span class="n">pushdown</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">selected_sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="pushdown_predicates-39"><a href="#pushdown_predicates-39"><span class="linenos">39</span></a> +</span><span id="pushdown_predicates-40"><a href="#pushdown_predicates-40"><span class="linenos">40</span></a> <span class="c1"># joins should only pushdown into itself, not to other joins</span> +</span><span id="pushdown_predicates-41"><a href="#pushdown_predicates-41"><span class="linenos">41</span></a> <span class="c1"># so we limit the selected sources to only itself</span> +</span><span id="pushdown_predicates-42"><a href="#pushdown_predicates-42"><span class="linenos">42</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"joins"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">[]:</span> +</span><span id="pushdown_predicates-43"><a href="#pushdown_predicates-43"><span class="linenos">43</span></a> <span class="n">name</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</span> +</span><span id="pushdown_predicates-44"><a href="#pushdown_predicates-44"><span class="linenos">44</span></a> <span class="n">pushdown</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">),</span> <span class="p">{</span><span class="n">name</span><span class="p">:</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">[</span><span class="n">name</span><span class="p">]},</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="pushdown_predicates-45"><a href="#pushdown_predicates-45"><span class="linenos">45</span></a> +</span><span id="pushdown_predicates-46"><a href="#pushdown_predicates-46"><span class="linenos">46</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to pushdown predicates in FROMS and JOINS</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"SELECT * FROM (SELECT * FROM x AS x) AS y WHERE y.a = 1"</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">pushdown_predicates</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT * FROM (SELECT * FROM x AS x WHERE y.a = 1) AS y WHERE TRUE'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + <section id="pushdown"> + <input id="pushdown-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">pushdown</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">condition</span>, </span><span class="param"><span class="n">sources</span>, </span><span class="param"><span class="n">scope_ref_count</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="pushdown-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#pushdown"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="pushdown-49"><a href="#pushdown-49"><span class="linenos">49</span></a><span class="k">def</span> <span class="nf">pushdown</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="pushdown-50"><a href="#pushdown-50"><span class="linenos">50</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">condition</span><span class="p">:</span> +</span><span id="pushdown-51"><a href="#pushdown-51"><span class="linenos">51</span></a> <span class="k">return</span> +</span><span id="pushdown-52"><a href="#pushdown-52"><span class="linenos">52</span></a> +</span><span id="pushdown-53"><a href="#pushdown-53"><span class="linenos">53</span></a> <span class="n">condition</span> <span class="o">=</span> <span class="n">condition</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">condition</span><span class="p">))</span> +</span><span id="pushdown-54"><a href="#pushdown-54"><span class="linenos">54</span></a> <span class="n">cnf_like</span> <span class="o">=</span> <span class="n">normalized</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">normalized</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">dnf</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="pushdown-55"><a href="#pushdown-55"><span class="linenos">55</span></a> +</span><span id="pushdown-56"><a href="#pushdown-56"><span class="linenos">56</span></a> <span class="n">predicates</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span> +</span><span id="pushdown-57"><a href="#pushdown-57"><span class="linenos">57</span></a> <span class="n">condition</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span> +</span><span id="pushdown-58"><a href="#pushdown-58"><span class="linenos">58</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">if</span> <span class="n">cnf_like</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> +</span><span id="pushdown-59"><a href="#pushdown-59"><span class="linenos">59</span></a> <span class="k">else</span> <span class="p">[</span><span class="n">condition</span><span class="p">]</span> +</span><span id="pushdown-60"><a href="#pushdown-60"><span class="linenos">60</span></a> <span class="p">)</span> +</span><span id="pushdown-61"><a href="#pushdown-61"><span class="linenos">61</span></a> +</span><span id="pushdown-62"><a href="#pushdown-62"><span class="linenos">62</span></a> <span class="k">if</span> <span class="n">cnf_like</span><span class="p">:</span> +</span><span id="pushdown-63"><a href="#pushdown-63"><span class="linenos">63</span></a> <span class="n">pushdown_cnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="pushdown-64"><a href="#pushdown-64"><span class="linenos">64</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="pushdown-65"><a href="#pushdown-65"><span class="linenos">65</span></a> <span class="n">pushdown_dnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="pushdown_cnf"> + <input id="pushdown_cnf-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">pushdown_cnf</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">predicates</span>, </span><span class="param"><span class="n">scope</span>, </span><span class="param"><span class="n">scope_ref_count</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="pushdown_cnf-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#pushdown_cnf"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="pushdown_cnf-68"><a href="#pushdown_cnf-68"><span class="linenos">68</span></a><span class="k">def</span> <span class="nf">pushdown_cnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="pushdown_cnf-69"><a href="#pushdown_cnf-69"><span class="linenos">69</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="pushdown_cnf-70"><a href="#pushdown_cnf-70"><span class="linenos">70</span></a><span class="sd"> If the predicates are in CNF like form, we can simply replace each block in the parent.</span> +</span><span id="pushdown_cnf-71"><a href="#pushdown_cnf-71"><span class="linenos">71</span></a><span class="sd"> """</span> +</span><span id="pushdown_cnf-72"><a href="#pushdown_cnf-72"><span class="linenos">72</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="pushdown_cnf-73"><a href="#pushdown_cnf-73"><span class="linenos">73</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="pushdown_cnf-74"><a href="#pushdown_cnf-74"><span class="linenos">74</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="pushdown_cnf-75"><a href="#pushdown_cnf-75"><span class="linenos">75</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="pushdown_cnf-76"><a href="#pushdown_cnf-76"><span class="linenos">76</span></a> <span class="n">node</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="pushdown_cnf-77"><a href="#pushdown_cnf-77"><span class="linenos">77</span></a> <span class="k">break</span> +</span><span id="pushdown_cnf-78"><a href="#pushdown_cnf-78"><span class="linenos">78</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="pushdown_cnf-79"><a href="#pushdown_cnf-79"><span class="linenos">79</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="pushdown_cnf-80"><a href="#pushdown_cnf-80"><span class="linenos">80</span></a> <span class="n">node</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">replace_aliases</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">predicate</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>If the predicates are in CNF like form, we can simply replace each block in the parent.</p> +</div> + + + </section> + <section id="pushdown_dnf"> + <input id="pushdown_dnf-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">pushdown_dnf</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">predicates</span>, </span><span class="param"><span class="n">scope</span>, </span><span class="param"><span class="n">scope_ref_count</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="pushdown_dnf-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#pushdown_dnf"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="pushdown_dnf-83"><a href="#pushdown_dnf-83"><span class="linenos"> 83</span></a><span class="k">def</span> <span class="nf">pushdown_dnf</span><span class="p">(</span><span class="n">predicates</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="pushdown_dnf-84"><a href="#pushdown_dnf-84"><span class="linenos"> 84</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="pushdown_dnf-85"><a href="#pushdown_dnf-85"><span class="linenos"> 85</span></a><span class="sd"> If the predicates are in DNF form, we can only push down conditions that are in all blocks.</span> +</span><span id="pushdown_dnf-86"><a href="#pushdown_dnf-86"><span class="linenos"> 86</span></a><span class="sd"> Additionally, we can't remove predicates from their original form.</span> +</span><span id="pushdown_dnf-87"><a href="#pushdown_dnf-87"><span class="linenos"> 87</span></a><span class="sd"> """</span> +</span><span id="pushdown_dnf-88"><a href="#pushdown_dnf-88"><span class="linenos"> 88</span></a> <span class="c1"># find all the tables that can be pushdown too</span> +</span><span id="pushdown_dnf-89"><a href="#pushdown_dnf-89"><span class="linenos"> 89</span></a> <span class="c1"># these are tables that are referenced in all blocks of a DNF</span> +</span><span id="pushdown_dnf-90"><a href="#pushdown_dnf-90"><span class="linenos"> 90</span></a> <span class="c1"># (a.x AND b.x) OR (a.y AND c.y)</span> +</span><span id="pushdown_dnf-91"><a href="#pushdown_dnf-91"><span class="linenos"> 91</span></a> <span class="c1"># only table a can be push down</span> +</span><span id="pushdown_dnf-92"><a href="#pushdown_dnf-92"><span class="linenos"> 92</span></a> <span class="n">pushdown_tables</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="pushdown_dnf-93"><a href="#pushdown_dnf-93"><span class="linenos"> 93</span></a> +</span><span id="pushdown_dnf-94"><a href="#pushdown_dnf-94"><span class="linenos"> 94</span></a> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="pushdown_dnf-95"><a href="#pushdown_dnf-95"><span class="linenos"> 95</span></a> <span class="n">a_tables</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> +</span><span id="pushdown_dnf-96"><a href="#pushdown_dnf-96"><span class="linenos"> 96</span></a> +</span><span id="pushdown_dnf-97"><a href="#pushdown_dnf-97"><span class="linenos"> 97</span></a> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="pushdown_dnf-98"><a href="#pushdown_dnf-98"><span class="linenos"> 98</span></a> <span class="n">a_tables</span> <span class="o">&=</span> <span class="nb">set</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">b</span><span class="p">))</span> +</span><span id="pushdown_dnf-99"><a href="#pushdown_dnf-99"><span class="linenos"> 99</span></a> +</span><span id="pushdown_dnf-100"><a href="#pushdown_dnf-100"><span class="linenos">100</span></a> <span class="n">pushdown_tables</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">a_tables</span><span class="p">)</span> +</span><span id="pushdown_dnf-101"><a href="#pushdown_dnf-101"><span class="linenos">101</span></a> +</span><span id="pushdown_dnf-102"><a href="#pushdown_dnf-102"><span class="linenos">102</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="pushdown_dnf-103"><a href="#pushdown_dnf-103"><span class="linenos">103</span></a> +</span><span id="pushdown_dnf-104"><a href="#pushdown_dnf-104"><span class="linenos">104</span></a> <span class="c1"># for every pushdown table, find all related conditions in all predicates</span> +</span><span id="pushdown_dnf-105"><a href="#pushdown_dnf-105"><span class="linenos">105</span></a> <span class="c1"># combine them with ORS</span> +</span><span id="pushdown_dnf-106"><a href="#pushdown_dnf-106"><span class="linenos">106</span></a> <span class="c1"># (a.x AND and a.y AND b.x) OR (a.z AND c.y) -> (a.x AND a.y) OR (a.z)</span> +</span><span id="pushdown_dnf-107"><a href="#pushdown_dnf-107"><span class="linenos">107</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">pushdown_tables</span><span class="p">):</span> +</span><span id="pushdown_dnf-108"><a href="#pushdown_dnf-108"><span class="linenos">108</span></a> <span class="k">for</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">predicates</span><span class="p">:</span> +</span><span id="pushdown_dnf-109"><a href="#pushdown_dnf-109"><span class="linenos">109</span></a> <span class="n">nodes</span> <span class="o">=</span> <span class="n">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">)</span> +</span><span id="pushdown_dnf-110"><a href="#pushdown_dnf-110"><span class="linenos">110</span></a> +</span><span id="pushdown_dnf-111"><a href="#pushdown_dnf-111"><span class="linenos">111</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span> +</span><span id="pushdown_dnf-112"><a href="#pushdown_dnf-112"><span class="linenos">112</span></a> <span class="k">continue</span> +</span><span id="pushdown_dnf-113"><a href="#pushdown_dnf-113"><span class="linenos">113</span></a> +</span><span id="pushdown_dnf-114"><a href="#pushdown_dnf-114"><span class="linenos">114</span></a> <span class="n">predicate_condition</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="pushdown_dnf-115"><a href="#pushdown_dnf-115"><span class="linenos">115</span></a> +</span><span id="pushdown_dnf-116"><a href="#pushdown_dnf-116"><span class="linenos">116</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="pushdown_dnf-117"><a href="#pushdown_dnf-117"><span class="linenos">117</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">table</span><span class="p">:</span> +</span><span id="pushdown_dnf-118"><a href="#pushdown_dnf-118"><span class="linenos">118</span></a> <span class="n">condition</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> +</span><span id="pushdown_dnf-119"><a href="#pushdown_dnf-119"><span class="linenos">119</span></a> <span class="n">predicate_condition</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="pushdown_dnf-120"><a href="#pushdown_dnf-120"><span class="linenos">120</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">predicate_condition</span><span class="p">,</span> <span class="n">condition</span><span class="p">)</span> +</span><span id="pushdown_dnf-121"><a href="#pushdown_dnf-121"><span class="linenos">121</span></a> <span class="k">if</span> <span class="n">predicate_condition</span> +</span><span id="pushdown_dnf-122"><a href="#pushdown_dnf-122"><span class="linenos">122</span></a> <span class="k">else</span> <span class="n">condition</span> +</span><span id="pushdown_dnf-123"><a href="#pushdown_dnf-123"><span class="linenos">123</span></a> <span class="p">)</span> +</span><span id="pushdown_dnf-124"><a href="#pushdown_dnf-124"><span class="linenos">124</span></a> +</span><span id="pushdown_dnf-125"><a href="#pushdown_dnf-125"><span class="linenos">125</span></a> <span class="k">if</span> <span class="n">predicate_condition</span><span class="p">:</span> +</span><span id="pushdown_dnf-126"><a href="#pushdown_dnf-126"><span class="linenos">126</span></a> <span class="n">conditions</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="pushdown_dnf-127"><a href="#pushdown_dnf-127"><span class="linenos">127</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span><span class="n">conditions</span><span class="p">[</span><span class="n">table</span><span class="p">],</span> <span class="n">predicate_condition</span><span class="p">)</span> +</span><span id="pushdown_dnf-128"><a href="#pushdown_dnf-128"><span class="linenos">128</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">conditions</span> +</span><span id="pushdown_dnf-129"><a href="#pushdown_dnf-129"><span class="linenos">129</span></a> <span class="k">else</span> <span class="n">predicate_condition</span> +</span><span id="pushdown_dnf-130"><a href="#pushdown_dnf-130"><span class="linenos">130</span></a> <span class="p">)</span> +</span><span id="pushdown_dnf-131"><a href="#pushdown_dnf-131"><span class="linenos">131</span></a> +</span><span id="pushdown_dnf-132"><a href="#pushdown_dnf-132"><span class="linenos">132</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">nodes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="pushdown_dnf-133"><a href="#pushdown_dnf-133"><span class="linenos">133</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">conditions</span><span class="p">:</span> +</span><span id="pushdown_dnf-134"><a href="#pushdown_dnf-134"><span class="linenos">134</span></a> <span class="k">continue</span> +</span><span id="pushdown_dnf-135"><a href="#pushdown_dnf-135"><span class="linenos">135</span></a> +</span><span id="pushdown_dnf-136"><a href="#pushdown_dnf-136"><span class="linenos">136</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">conditions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> +</span><span id="pushdown_dnf-137"><a href="#pushdown_dnf-137"><span class="linenos">137</span></a> +</span><span id="pushdown_dnf-138"><a href="#pushdown_dnf-138"><span class="linenos">138</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="pushdown_dnf-139"><a href="#pushdown_dnf-139"><span class="linenos">139</span></a> <span class="n">node</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="pushdown_dnf-140"><a href="#pushdown_dnf-140"><span class="linenos">140</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="pushdown_dnf-141"><a href="#pushdown_dnf-141"><span class="linenos">141</span></a> <span class="n">node</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">replace_aliases</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">predicate</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>If the predicates are in DNF form, we can only push down conditions that are in all blocks. +Additionally, we can't remove predicates from their original form.</p> +</div> + + + </section> + <section id="nodes_for_predicate"> + <input id="nodes_for_predicate-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">nodes_for_predicate</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">predicate</span>, </span><span class="param"><span class="n">sources</span>, </span><span class="param"><span class="n">scope_ref_count</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="nodes_for_predicate-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#nodes_for_predicate"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="nodes_for_predicate-144"><a href="#nodes_for_predicate-144"><span class="linenos">144</span></a><span class="k">def</span> <span class="nf">nodes_for_predicate</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">scope_ref_count</span><span class="p">):</span> +</span><span id="nodes_for_predicate-145"><a href="#nodes_for_predicate-145"><span class="linenos">145</span></a> <span class="n">nodes</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="nodes_for_predicate-146"><a href="#nodes_for_predicate-146"><span class="linenos">146</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">column_table_names</span><span class="p">(</span><span class="n">predicate</span><span class="p">)</span> +</span><span id="nodes_for_predicate-147"><a href="#nodes_for_predicate-147"><span class="linenos">147</span></a> <span class="n">where_condition</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> +</span><span id="nodes_for_predicate-148"><a href="#nodes_for_predicate-148"><span class="linenos">148</span></a> +</span><span id="nodes_for_predicate-149"><a href="#nodes_for_predicate-149"><span class="linenos">149</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="nodes_for_predicate-150"><a href="#nodes_for_predicate-150"><span class="linenos">150</span></a> <span class="n">node</span><span class="p">,</span> <span class="n">source</span> <span class="o">=</span> <span class="n">sources</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="nodes_for_predicate-151"><a href="#nodes_for_predicate-151"><span class="linenos">151</span></a> +</span><span id="nodes_for_predicate-152"><a href="#nodes_for_predicate-152"><span class="linenos">152</span></a> <span class="c1"># if the predicate is in a where statement we can try to push it down</span> +</span><span id="nodes_for_predicate-153"><a href="#nodes_for_predicate-153"><span class="linenos">153</span></a> <span class="c1"># we want to find the root join or from statement</span> +</span><span id="nodes_for_predicate-154"><a href="#nodes_for_predicate-154"><span class="linenos">154</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">and</span> <span class="n">where_condition</span><span class="p">:</span> +</span><span id="nodes_for_predicate-155"><a href="#nodes_for_predicate-155"><span class="linenos">155</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</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="nodes_for_predicate-156"><a href="#nodes_for_predicate-156"><span class="linenos">156</span></a> +</span><span id="nodes_for_predicate-157"><a href="#nodes_for_predicate-157"><span class="linenos">157</span></a> <span class="c1"># a node can reference a CTE which should be pushed down</span> +</span><span id="nodes_for_predicate-158"><a href="#nodes_for_predicate-158"><span class="linenos">158</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="nodes_for_predicate-159"><a href="#nodes_for_predicate-159"><span class="linenos">159</span></a> <span class="n">with_</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">parent</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">"with"</span><span class="p">)</span> +</span><span id="nodes_for_predicate-160"><a href="#nodes_for_predicate-160"><span class="linenos">160</span></a> <span class="k">if</span> <span class="n">with_</span> <span class="ow">and</span> <span class="n">with_</span><span class="o">.</span><span class="n">recursive</span><span class="p">:</span> +</span><span id="nodes_for_predicate-161"><a href="#nodes_for_predicate-161"><span class="linenos">161</span></a> <span class="k">return</span> <span class="p">{}</span> +</span><span id="nodes_for_predicate-162"><a href="#nodes_for_predicate-162"><span class="linenos">162</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span> +</span><span id="nodes_for_predicate-163"><a href="#nodes_for_predicate-163"><span class="linenos">163</span></a> +</span><span id="nodes_for_predicate-164"><a href="#nodes_for_predicate-164"><span class="linenos">164</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="nodes_for_predicate-165"><a href="#nodes_for_predicate-165"><span class="linenos">165</span></a> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">side</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">side</span> <span class="o">!=</span> <span class="s2">"RIGHT"</span><span class="p">:</span> +</span><span id="nodes_for_predicate-166"><a href="#nodes_for_predicate-166"><span class="linenos">166</span></a> <span class="k">return</span> <span class="p">{}</span> +</span><span id="nodes_for_predicate-167"><a href="#nodes_for_predicate-167"><span class="linenos">167</span></a> <span class="n">nodes</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">node</span> +</span><span id="nodes_for_predicate-168"><a href="#nodes_for_predicate-168"><span class="linenos">168</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">tables</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="nodes_for_predicate-169"><a href="#nodes_for_predicate-169"><span class="linenos">169</span></a> <span class="c1"># We can't push down window expressions</span> +</span><span id="nodes_for_predicate-170"><a href="#nodes_for_predicate-170"><span class="linenos">170</span></a> <span class="n">has_window_expression</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="nodes_for_predicate-171"><a href="#nodes_for_predicate-171"><span class="linenos">171</span></a> <span class="n">select</span> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">selects</span> <span class="k">if</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Window</span><span class="p">)</span> +</span><span id="nodes_for_predicate-172"><a href="#nodes_for_predicate-172"><span class="linenos">172</span></a> <span class="p">)</span> +</span><span id="nodes_for_predicate-173"><a href="#nodes_for_predicate-173"><span class="linenos">173</span></a> <span class="c1"># we can't push down predicates to select statements if they are referenced in</span> +</span><span id="nodes_for_predicate-174"><a href="#nodes_for_predicate-174"><span class="linenos">174</span></a> <span class="c1"># multiple places.</span> +</span><span id="nodes_for_predicate-175"><a href="#nodes_for_predicate-175"><span class="linenos">175</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="nodes_for_predicate-176"><a href="#nodes_for_predicate-176"><span class="linenos">176</span></a> <span class="ow">not</span> <span class="n">node</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"group"</span><span class="p">)</span> +</span><span id="nodes_for_predicate-177"><a href="#nodes_for_predicate-177"><span class="linenos">177</span></a> <span class="ow">and</span> <span class="n">scope_ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o"><</span> <span class="mi">2</span> +</span><span id="nodes_for_predicate-178"><a href="#nodes_for_predicate-178"><span class="linenos">178</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="n">has_window_expression</span> +</span><span id="nodes_for_predicate-179"><a href="#nodes_for_predicate-179"><span class="linenos">179</span></a> <span class="p">):</span> +</span><span id="nodes_for_predicate-180"><a href="#nodes_for_predicate-180"><span class="linenos">180</span></a> <span class="n">nodes</span><span class="p">[</span><span class="n">table</span><span class="p">]</span> <span class="o">=</span> <span class="n">node</span> +</span><span id="nodes_for_predicate-181"><a href="#nodes_for_predicate-181"><span class="linenos">181</span></a> <span class="k">return</span> <span class="n">nodes</span> +</span></pre></div> + + + + + </section> + <section id="replace_aliases"> + <input id="replace_aliases-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">replace_aliases</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">source</span>, </span><span class="param"><span class="n">predicate</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="replace_aliases-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#replace_aliases"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="replace_aliases-184"><a href="#replace_aliases-184"><span class="linenos">184</span></a><span class="k">def</span> <span class="nf">replace_aliases</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">predicate</span><span class="p">):</span> +</span><span id="replace_aliases-185"><a href="#replace_aliases-185"><span class="linenos">185</span></a> <span class="n">aliases</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="replace_aliases-186"><a href="#replace_aliases-186"><span class="linenos">186</span></a> +</span><span id="replace_aliases-187"><a href="#replace_aliases-187"><span class="linenos">187</span></a> <span class="k">for</span> <span class="n">select</span> <span class="ow">in</span> <span class="n">source</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="replace_aliases-188"><a href="#replace_aliases-188"><span class="linenos">188</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="replace_aliases-189"><a href="#replace_aliases-189"><span class="linenos">189</span></a> <span class="n">aliases</span><span class="p">[</span><span class="n">select</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="replace_aliases-190"><a href="#replace_aliases-190"><span class="linenos">190</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="replace_aliases-191"><a href="#replace_aliases-191"><span class="linenos">191</span></a> <span class="n">aliases</span><span class="p">[</span><span class="n">select</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">select</span> +</span><span id="replace_aliases-192"><a href="#replace_aliases-192"><span class="linenos">192</span></a> +</span><span id="replace_aliases-193"><a href="#replace_aliases-193"><span class="linenos">193</span></a> <span class="k">def</span> <span class="nf">_replace_alias</span><span class="p">(</span><span class="n">column</span><span class="p">):</span> +</span><span id="replace_aliases-194"><a href="#replace_aliases-194"><span class="linenos">194</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">aliases</span><span class="p">:</span> +</span><span id="replace_aliases-195"><a href="#replace_aliases-195"><span class="linenos">195</span></a> <span class="k">return</span> <span class="n">aliases</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="replace_aliases-196"><a href="#replace_aliases-196"><span class="linenos">196</span></a> <span class="k">return</span> <span class="n">column</span> +</span><span id="replace_aliases-197"><a href="#replace_aliases-197"><span class="linenos">197</span></a> +</span><span id="replace_aliases-198"><a href="#replace_aliases-198"><span class="linenos">198</span></a> <span class="k">return</span> <span class="n">predicate</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">_replace_alias</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/pushdown_projections.html b/docs/sqlglot/optimizer/pushdown_projections.html new file mode 100644 index 0000000..385a18a --- /dev/null +++ b/docs/sqlglot/optimizer/pushdown_projections.html @@ -0,0 +1,477 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.pushdown_projections API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#DEFAULT_SELECTION">DEFAULT_SELECTION</a> + </li> + <li> + <a class="function" href="#pushdown_projections">pushdown_projections</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/pushdown_projections.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.pushdown_projections </h1> + + + <input id="mod-pushdown_projections-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-pushdown_projections-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">alias</span><span class="p">,</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="c1"># Sentinel value that means an outer query selecting ALL columns</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="n">SELECT_ALL</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="c1"># Selection to use if selection list is empty</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="n">DEFAULT_SELECTION</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">alias</span><span class="p">(</span><span class="s2">"1"</span><span class="p">,</span> <span class="s2">"_"</span><span class="p">)</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a><span class="k">def</span> <span class="nf">pushdown_projections</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> Rewrite sqlglot AST to remove unused columns projections.</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> Example:</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> >>> sql = "SELECT y.a AS a FROM (SELECT x.a AS a, x.b AS b FROM x) AS y"</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> >>> pushdown_projections(expression).sql()</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> 'SELECT y.a AS a FROM (SELECT x.a AS a FROM x) AS y'</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> Args:</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> Returns:</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a><span class="sd"> """</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="c1"># Map of Scope to all columns being selected by outer queries.</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="n">referenced_columns</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">set</span><span class="p">)</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="n">left_union</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="n">right_union</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="c1"># We build the scope tree (which is traversed in DFS postorder), then iterate</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="c1"># over the result in reverse order. This should ensure that the set of selected</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="c1"># columns for a particular scope are completely build by the time we get to it.</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)):</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="n">parent_selections</span> <span class="o">=</span> <span class="n">referenced_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="p">{</span><span class="n">SELECT_ALL</span><span class="p">})</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">if</span> <span class="n">scope</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">"distinct"</span><span class="p">):</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="c1"># We can't remove columns SELECT DISTINCT nor UNION DISTINCT</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="n">parent_selections</span> <span class="o">=</span> <span class="p">{</span><span class="n">SELECT_ALL</span><span class="p">}</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="n">left_union</span><span class="p">,</span> <span class="n">right_union</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">union_scopes</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">left_union</span><span class="p">]</span> <span class="o">=</span> <span class="n">parent_selections</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">right_union</span><span class="p">]</span> <span class="o">=</span> <span class="n">parent_selections</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="n">scope</span> <span class="o">!=</span> <span class="n">right_union</span><span class="p">:</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="n">removed_indexes</span> <span class="o">=</span> <span class="n">_remove_unused_selections</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">parent_selections</span><span class="p">)</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="c1"># The left union is used for column names to select and if we remove columns from the left</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="c1"># we need to also remove those same columns in the right that were at the same position</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="n">left_union</span><span class="p">:</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="n">_remove_indexed_selections</span><span class="p">(</span><span class="n">right_union</span><span class="p">,</span> <span class="n">removed_indexes</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="c1"># Group columns by source name</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">set</span><span class="p">)</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="n">table_name</span> <span class="o">=</span> <span class="n">col</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="n">col_name</span> <span class="o">=</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">table_name</span><span class="p">]</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="c1"># Push the selected columns down to the next scope</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="n">selects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">source</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">columns</span><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">return</span> <span class="n">expression</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> +</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="k">def</span> <span class="nf">_remove_unused_selections</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">parent_selections</span><span class="p">):</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="n">removed_indexes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">order</span> <span class="o">=</span> <span class="n">scope</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">"order"</span><span class="p">)</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="k">if</span> <span class="n">order</span><span class="p">:</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="c1"># Assume columns without a qualified table are references to output columns</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="n">order_refs</span> <span class="o">=</span> <span class="p">{</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">order</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span><span class="p">}</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">order_refs</span> <span class="o">=</span> <span class="nb">set</span><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">new_selections</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="n">removed</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">selection</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">):</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">SELECT_ALL</span> <span class="ow">in</span> <span class="n">parent_selections</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="ow">or</span> <span class="n">selection</span><span class="o">.</span><span class="n">alias_or_name</span> <span class="ow">in</span> <span class="n">parent_selections</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="ow">or</span> <span class="n">selection</span><span class="o">.</span><span class="n">alias_or_name</span> <span class="ow">in</span> <span class="n">order_refs</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="p">):</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selection</span><span class="p">)</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="n">removed_indexes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="n">removed</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="c1"># If there are no remaining selections, just select a single constant</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">new_selections</span><span class="p">:</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">DEFAULT_SELECTION</span><span class="p">())</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">new_selections</span><span class="p">)</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">if</span> <span class="n">removed</span><span class="p">:</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="k">return</span> <span class="n">removed_indexes</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><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a><span class="k">def</span> <span class="nf">_remove_indexed_selections</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">indexes_to_remove</span><span class="p">):</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="n">new_selections</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="n">selection</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">selection</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">)</span> <span class="k">if</span> <span class="n">i</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">indexes_to_remove</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <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="ow">not</span> <span class="n">new_selections</span><span class="p">:</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">DEFAULT_SELECTION</span><span class="p">())</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">new_selections</span><span class="p">)</span> +</span></pre></div> + + + </section> + <section id="DEFAULT_SELECTION"> + <input id="DEFAULT_SELECTION-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">DEFAULT_SELECTION</span><span class="signature pdoc-code condensed">(<span class="return-annotation">):</span></span> + + <label class="view-source-button" for="DEFAULT_SELECTION-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#DEFAULT_SELECTION"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="DEFAULT_SELECTION-11"><a href="#DEFAULT_SELECTION-11"><span class="linenos">11</span></a><span class="n">DEFAULT_SELECTION</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">alias</span><span class="p">(</span><span class="s2">"1"</span><span class="p">,</span> <span class="s2">"_"</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="pushdown_projections"> + <input id="pushdown_projections-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">pushdown_projections</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="pushdown_projections-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#pushdown_projections"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="pushdown_projections-14"><a href="#pushdown_projections-14"><span class="linenos">14</span></a><span class="k">def</span> <span class="nf">pushdown_projections</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="pushdown_projections-15"><a href="#pushdown_projections-15"><span class="linenos">15</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="pushdown_projections-16"><a href="#pushdown_projections-16"><span class="linenos">16</span></a><span class="sd"> Rewrite sqlglot AST to remove unused columns projections.</span> +</span><span id="pushdown_projections-17"><a href="#pushdown_projections-17"><span class="linenos">17</span></a> +</span><span id="pushdown_projections-18"><a href="#pushdown_projections-18"><span class="linenos">18</span></a><span class="sd"> Example:</span> +</span><span id="pushdown_projections-19"><a href="#pushdown_projections-19"><span class="linenos">19</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="pushdown_projections-20"><a href="#pushdown_projections-20"><span class="linenos">20</span></a><span class="sd"> >>> sql = "SELECT y.a AS a FROM (SELECT x.a AS a, x.b AS b FROM x) AS y"</span> +</span><span id="pushdown_projections-21"><a href="#pushdown_projections-21"><span class="linenos">21</span></a><span class="sd"> >>> expression = sqlglot.parse_one(sql)</span> +</span><span id="pushdown_projections-22"><a href="#pushdown_projections-22"><span class="linenos">22</span></a><span class="sd"> >>> pushdown_projections(expression).sql()</span> +</span><span id="pushdown_projections-23"><a href="#pushdown_projections-23"><span class="linenos">23</span></a><span class="sd"> 'SELECT y.a AS a FROM (SELECT x.a AS a FROM x) AS y'</span> +</span><span id="pushdown_projections-24"><a href="#pushdown_projections-24"><span class="linenos">24</span></a> +</span><span id="pushdown_projections-25"><a href="#pushdown_projections-25"><span class="linenos">25</span></a><span class="sd"> Args:</span> +</span><span id="pushdown_projections-26"><a href="#pushdown_projections-26"><span class="linenos">26</span></a><span class="sd"> expression (sqlglot.Expression): expression to optimize</span> +</span><span id="pushdown_projections-27"><a href="#pushdown_projections-27"><span class="linenos">27</span></a><span class="sd"> Returns:</span> +</span><span id="pushdown_projections-28"><a href="#pushdown_projections-28"><span class="linenos">28</span></a><span class="sd"> sqlglot.Expression: optimized expression</span> +</span><span id="pushdown_projections-29"><a href="#pushdown_projections-29"><span class="linenos">29</span></a><span class="sd"> """</span> +</span><span id="pushdown_projections-30"><a href="#pushdown_projections-30"><span class="linenos">30</span></a> <span class="c1"># Map of Scope to all columns being selected by outer queries.</span> +</span><span id="pushdown_projections-31"><a href="#pushdown_projections-31"><span class="linenos">31</span></a> <span class="n">referenced_columns</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">set</span><span class="p">)</span> +</span><span id="pushdown_projections-32"><a href="#pushdown_projections-32"><span class="linenos">32</span></a> <span class="n">left_union</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="pushdown_projections-33"><a href="#pushdown_projections-33"><span class="linenos">33</span></a> <span class="n">right_union</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="pushdown_projections-34"><a href="#pushdown_projections-34"><span class="linenos">34</span></a> <span class="c1"># We build the scope tree (which is traversed in DFS postorder), then iterate</span> +</span><span id="pushdown_projections-35"><a href="#pushdown_projections-35"><span class="linenos">35</span></a> <span class="c1"># over the result in reverse order. This should ensure that the set of selected</span> +</span><span id="pushdown_projections-36"><a href="#pushdown_projections-36"><span class="linenos">36</span></a> <span class="c1"># columns for a particular scope are completely build by the time we get to it.</span> +</span><span id="pushdown_projections-37"><a href="#pushdown_projections-37"><span class="linenos">37</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)):</span> +</span><span id="pushdown_projections-38"><a href="#pushdown_projections-38"><span class="linenos">38</span></a> <span class="n">parent_selections</span> <span class="o">=</span> <span class="n">referenced_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="p">{</span><span class="n">SELECT_ALL</span><span class="p">})</span> +</span><span id="pushdown_projections-39"><a href="#pushdown_projections-39"><span class="linenos">39</span></a> +</span><span id="pushdown_projections-40"><a href="#pushdown_projections-40"><span class="linenos">40</span></a> <span class="k">if</span> <span class="n">scope</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">"distinct"</span><span class="p">):</span> +</span><span id="pushdown_projections-41"><a href="#pushdown_projections-41"><span class="linenos">41</span></a> <span class="c1"># We can't remove columns SELECT DISTINCT nor UNION DISTINCT</span> +</span><span id="pushdown_projections-42"><a href="#pushdown_projections-42"><span class="linenos">42</span></a> <span class="n">parent_selections</span> <span class="o">=</span> <span class="p">{</span><span class="n">SELECT_ALL</span><span class="p">}</span> +</span><span id="pushdown_projections-43"><a href="#pushdown_projections-43"><span class="linenos">43</span></a> +</span><span id="pushdown_projections-44"><a href="#pushdown_projections-44"><span class="linenos">44</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="pushdown_projections-45"><a href="#pushdown_projections-45"><span class="linenos">45</span></a> <span class="n">left_union</span><span class="p">,</span> <span class="n">right_union</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">union_scopes</span> +</span><span id="pushdown_projections-46"><a href="#pushdown_projections-46"><span class="linenos">46</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">left_union</span><span class="p">]</span> <span class="o">=</span> <span class="n">parent_selections</span> +</span><span id="pushdown_projections-47"><a href="#pushdown_projections-47"><span class="linenos">47</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">right_union</span><span class="p">]</span> <span class="o">=</span> <span class="n">parent_selections</span> +</span><span id="pushdown_projections-48"><a href="#pushdown_projections-48"><span class="linenos">48</span></a> +</span><span id="pushdown_projections-49"><a href="#pushdown_projections-49"><span class="linenos">49</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> <span class="ow">and</span> <span class="n">scope</span> <span class="o">!=</span> <span class="n">right_union</span><span class="p">:</span> +</span><span id="pushdown_projections-50"><a href="#pushdown_projections-50"><span class="linenos">50</span></a> <span class="n">removed_indexes</span> <span class="o">=</span> <span class="n">_remove_unused_selections</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">parent_selections</span><span class="p">)</span> +</span><span id="pushdown_projections-51"><a href="#pushdown_projections-51"><span class="linenos">51</span></a> <span class="c1"># The left union is used for column names to select and if we remove columns from the left</span> +</span><span id="pushdown_projections-52"><a href="#pushdown_projections-52"><span class="linenos">52</span></a> <span class="c1"># we need to also remove those same columns in the right that were at the same position</span> +</span><span id="pushdown_projections-53"><a href="#pushdown_projections-53"><span class="linenos">53</span></a> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="n">left_union</span><span class="p">:</span> +</span><span id="pushdown_projections-54"><a href="#pushdown_projections-54"><span class="linenos">54</span></a> <span class="n">_remove_indexed_selections</span><span class="p">(</span><span class="n">right_union</span><span class="p">,</span> <span class="n">removed_indexes</span><span class="p">)</span> +</span><span id="pushdown_projections-55"><a href="#pushdown_projections-55"><span class="linenos">55</span></a> +</span><span id="pushdown_projections-56"><a href="#pushdown_projections-56"><span class="linenos">56</span></a> <span class="c1"># Group columns by source name</span> +</span><span id="pushdown_projections-57"><a href="#pushdown_projections-57"><span class="linenos">57</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">set</span><span class="p">)</span> +</span><span id="pushdown_projections-58"><a href="#pushdown_projections-58"><span class="linenos">58</span></a> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="pushdown_projections-59"><a href="#pushdown_projections-59"><span class="linenos">59</span></a> <span class="n">table_name</span> <span class="o">=</span> <span class="n">col</span><span class="o">.</span><span class="n">table</span> +</span><span id="pushdown_projections-60"><a href="#pushdown_projections-60"><span class="linenos">60</span></a> <span class="n">col_name</span> <span class="o">=</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span> +</span><span id="pushdown_projections-61"><a href="#pushdown_projections-61"><span class="linenos">61</span></a> <span class="n">selects</span><span class="p">[</span><span class="n">table_name</span><span class="p">]</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span> +</span><span id="pushdown_projections-62"><a href="#pushdown_projections-62"><span class="linenos">62</span></a> +</span><span id="pushdown_projections-63"><a href="#pushdown_projections-63"><span class="linenos">63</span></a> <span class="c1"># Push the selected columns down to the next scope</span> +</span><span id="pushdown_projections-64"><a href="#pushdown_projections-64"><span class="linenos">64</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="pushdown_projections-65"><a href="#pushdown_projections-65"><span class="linenos">65</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">):</span> +</span><span id="pushdown_projections-66"><a href="#pushdown_projections-66"><span class="linenos">66</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="n">selects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">set</span><span class="p">()</span> +</span><span id="pushdown_projections-67"><a href="#pushdown_projections-67"><span class="linenos">67</span></a> <span class="n">referenced_columns</span><span class="p">[</span><span class="n">source</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span> +</span><span id="pushdown_projections-68"><a href="#pushdown_projections-68"><span class="linenos">68</span></a> +</span><span id="pushdown_projections-69"><a href="#pushdown_projections-69"><span class="linenos">69</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to remove unused columns projections.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">"SELECT y.a AS a FROM (SELECT x.a AS a, x.b AS b FROM x) AS y"</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">pushdown_projections</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT y.a AS a FROM (SELECT x.a AS a FROM x) AS y'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to optimize</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: optimized expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/qualify_columns.html b/docs/sqlglot/optimizer/qualify_columns.html new file mode 100644 index 0000000..9722c27 --- /dev/null +++ b/docs/sqlglot/optimizer/qualify_columns.html @@ -0,0 +1,804 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.qualify_columns API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#qualify_columns">qualify_columns</a> + </li> + <li> + <a class="function" href="#validate_qualify_columns">validate_qualify_columns</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/qualify_columns.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.qualify_columns </h1> + + + <input id="mod-qualify_columns-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-qualify_columns-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">alias</span><span class="p">,</span> <span class="n">exp</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.errors</span> <span class="kn">import</span> <span class="n">OptimizeError</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="kn">from</span> <span class="nn">sqlglot.schema</span> <span class="kn">import</span> <span class="n">ensure_schema</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="k">def</span> <span class="nf">qualify_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="p">):</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="sd"> Rewrite sqlglot AST to have fully qualified columns.</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> Example:</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> schema = {"tbl": {"col": "INT"}}</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT col FROM tbl")</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> >>> qualify_columns(expression, schema).sql()</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> 'SELECT tbl.col AS col FROM tbl'</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> Args:</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> expression (sqlglot.Expression): expression to qualify</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): Database schema</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> Returns:</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> sqlglot.Expression: qualified expression</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> """</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="n">resolver</span> <span class="o">=</span> <span class="n">_Resolver</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="n">_pop_table_column_aliases</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">ctes</span><span class="p">)</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="n">_pop_table_column_aliases</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">)</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="n">_expand_using</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="n">_expand_group_by</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">_qualify_columns</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="n">_expand_order_by</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="n">_expand_stars</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="n">_qualify_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a><span class="k">def</span> <span class="nf">validate_qualify_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a><span class="w"> </span><span class="sd">"""Raise an `OptimizeError` if any columns aren't qualified"""</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="n">unqualified_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="n">unqualified_columns</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">unqualified_columns</span><span class="p">)</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_correlated_subquery</span><span class="p">:</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown table: </span><span class="si">{</span><span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">table</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="k">if</span> <span class="n">unqualified_columns</span><span class="p">:</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Ambiguous columns: </span><span class="si">{</span><span class="n">unqualified_columns</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a><span class="k">def</span> <span class="nf">_pop_table_column_aliases</span><span class="p">(</span><span class="n">derived_tables</span><span class="p">):</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a><span class="sd"> Remove table column aliases.</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a><span class="sd"> (e.g. SELECT ... FROM (SELECT ...) AS foo(col1, col2)</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a><span class="sd"> """</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="n">derived_tables</span><span class="p">:</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">derived_table</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="k">continue</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> <span class="n">table_alias</span> <span class="o">=</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> <span class="k">if</span> <span class="n">table_alias</span><span class="p">:</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="n">table_alias</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">"columns"</span><span class="p">,</span> <span class="kc">None</span><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><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a><span class="k">def</span> <span class="nf">_expand_using</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">):</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="n">joins</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">))</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="n">names</span> <span class="o">=</span> <span class="p">{</span><span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias</span> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">joins</span><span class="p">}</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="n">ordered</span> <span class="o">=</span> <span class="p">[</span><span class="n">key</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">names</span><span class="p">]</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="c1"># Mapping of automatically joined column names to source names</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">column_tables</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">joins</span><span class="p">:</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="n">using</span> <span class="o">=</span> <span class="n">join</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">"using"</span><span class="p">)</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">using</span><span class="p">:</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">continue</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="n">join_table</span> <span class="o">=</span> <span class="n">join</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">alias_or_name</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="n">columns</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">:</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ordered</span><span class="p">:</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_source_columns</span><span class="p">(</span><span class="n">k</span><span class="p">):</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="k">if</span> <span class="n">column</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">:</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="n">columns</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="n">k</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="n">ordered</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">join_table</span><span class="p">)</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">join_columns</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_source_columns</span><span class="p">(</span><span class="n">join_table</span><span class="p">)</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="n">conditions</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="k">for</span> <span class="n">identifier</span> <span class="ow">in</span> <span class="n">using</span><span class="p">:</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="n">identifier</span> <span class="o">=</span> <span class="n">identifier</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="n">table</span> <span class="o">=</span> <span class="n">columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">identifier</span><span class="p">)</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">table</span> <span class="ow">or</span> <span class="n">identifier</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">join_columns</span><span class="p">:</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Cannot automatically join: </span><span class="si">{</span><span class="n">identifier</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="n">conditions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">condition</span><span class="p">(</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">(</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="n">this</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="n">identifier</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">table</span><span class="p">),</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="n">expression</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="n">identifier</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">join_table</span><span class="p">),</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="p">)</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="p">)</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="p">)</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">column_tables</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">identifier</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="n">tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">table</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="n">join_table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="n">tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">join_table</span><span class="p">)</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> <span class="n">join</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">"using"</span><span class="p">)</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"on"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="o">*</span><span class="n">conditions</span><span class="p">))</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="k">if</span> <span class="n">column_tables</span><span class="p">:</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">column_tables</span><span class="p">:</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="n">column_tables</span><span class="p">[</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="n">coalesce</span> <span class="o">=</span> <span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">table</span><span class="o">=</span><span class="n">table</span><span class="p">)</span> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">]</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">replacement</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">coalesce</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">expressions</span><span class="o">=</span><span class="n">coalesce</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="c1"># Ensure selects keep their output name</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">column</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">Select</span><span class="p">):</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="n">replacement</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">replacement</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">replacement</span><span class="p">)</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><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a><span class="k">def</span> <span class="nf">_expand_group_by</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">):</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="n">group</span> <span class="o">=</span> <span class="n">scope</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">"group"</span><span class="p">)</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">group</span><span class="p">:</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="k">return</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> <span class="c1"># Replace references to select aliases</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="o">*</span><span class="n">_</span><span class="p">):</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">node</span><span class="o">.</span><span class="n">table</span><span class="p">:</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="n">table</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_table</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="c1"># Source columns get priority over select aliases</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="n">table</span><span class="p">:</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="n">node</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"table"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">table</span><span class="p">))</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="n">selects</span> <span class="o">=</span> <span class="p">{</span><span class="n">s</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">:</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">}</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">selects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="k">if</span> <span class="n">select</span><span class="p">:</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> <span class="k">return</span> <span class="n">select</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="n">group</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">transform</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="n">group</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">_expand_positional_references</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">group</span><span class="o">.</span><span class="n">expressions</span><span class="p">))</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"group"</span><span class="p">,</span> <span class="n">group</span><span class="p">)</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a><span class="k">def</span> <span class="nf">_expand_order_by</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="n">order</span> <span class="o">=</span> <span class="n">scope</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">"order"</span><span class="p">)</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">order</span><span class="p">:</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> <span class="k">return</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="n">ordereds</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">expressions</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="k">for</span> <span class="n">ordered</span><span class="p">,</span> <span class="n">new_expression</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="n">ordereds</span><span class="p">,</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="n">_expand_positional_references</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="p">(</span><span class="n">o</span><span class="o">.</span><span class="n">this</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">ordereds</span><span class="p">)),</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="p">):</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="n">ordered</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">new_expression</span><span class="p">)</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a><span class="k">def</span> <span class="nf">_expand_positional_references</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">expressions</span><span class="p">):</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="n">new_nodes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">expressions</span><span class="p">:</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">is_int</span><span class="p">:</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown output column: </span><span class="si">{</span><span class="n">node</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-191"><a href="#L-191"><span class="linenos">191</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="n">new_nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a> <span class="n">new_nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a> <span class="k">return</span> <span class="n">new_nodes</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a><span class="k">def</span> <span class="nf">_qualify_columns</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">):</span> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a><span class="w"> </span><span class="sd">"""Disambiguate columns, ensuring each column specifies a source"""</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="n">column_table</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> <span class="n">column_name</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="k">if</span> <span class="n">column_table</span> <span class="ow">and</span> <span class="n">column_table</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> <span class="n">source_columns</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_source_columns</span><span class="p">(</span><span class="n">column_table</span><span class="p">)</span> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="k">if</span> <span class="n">source_columns</span> <span class="ow">and</span> <span class="n">column_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">source_columns</span><span class="p">:</span> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown column: </span><span class="si">{</span><span class="n">column_name</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">column_table</span><span class="p">:</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> <span class="n">column_table</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_table</span><span class="p">(</span><span class="n">column_name</span><span class="p">)</span> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> <span class="c1"># column_table can be a '' because bigquery unnest has no table alias</span> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> <span class="k">if</span> <span class="n">column_table</span><span class="p">:</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="n">column</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"table"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">column_table</span><span class="p">))</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a> <span class="n">columns_missing_from_scope</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> <span class="c1"># Determine whether each reference in the order by clause is to a column or an alias.</span> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a> <span class="k">for</span> <span class="n">ordered</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Ordered</span><span class="p">):</span> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">ordered</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">ordered</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">resolver</span><span class="o">.</span><span class="n">all_columns</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a> <span class="p">):</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a> <span class="n">columns_missing_from_scope</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="c1"># Determine whether each reference in the having clause is to a column or an alias.</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="k">for</span> <span class="n">having</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">):</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">having</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">):</span> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">)</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a> <span class="ow">and</span> <span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">resolver</span><span class="o">.</span><span class="n">all_columns</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> <span class="p">):</span> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> <span class="n">columns_missing_from_scope</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns_missing_from_scope</span><span class="p">:</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> <span class="n">column_table</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_table</span><span class="p">(</span><span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a> <span class="k">if</span> <span class="n">column_table</span><span class="p">:</span> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> <span class="n">column</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"table"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">column_table</span><span class="p">))</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a><span class="k">def</span> <span class="nf">_expand_stars</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">):</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a><span class="w"> </span><span class="sd">"""Expand stars to lists of column selections"""</span> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> <span class="n">new_selections</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a> <span class="n">except_columns</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> <span class="n">replace_columns</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a> <span class="k">for</span> <span class="n">expression</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">:</span> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Star</span><span class="p">):</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="p">)</span> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="n">_add_except_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">except_columns</span><span class="p">)</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> <span class="n">_add_replace_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">replace_columns</span><span class="p">)</span> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Star</span><span class="p">):</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> <span class="n">tables</span> <span class="o">=</span> <span class="p">[</span><span class="n">expression</span><span class="o">.</span><span class="n">table</span><span class="p">]</span> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> <span class="n">_add_except_columns</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">except_columns</span><span class="p">)</span> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> <span class="n">_add_replace_columns</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">replace_columns</span><span class="p">)</span> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> <span class="k">continue</span> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a> <span class="k">if</span> <span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown table: </span><span class="si">{</span><span class="n">table</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">get_source_columns</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">only_visible</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="L-271"><a href="#L-271"><span class="linenos">271</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">columns</span><span class="p">:</span> +</span><span id="L-272"><a href="#L-272"><span class="linenos">272</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span> +</span><span id="L-273"><a href="#L-273"><span class="linenos">273</span></a> <span class="sa">f</span><span class="s2">"Table has no schema/columns. Cannot expand star for table: </span><span class="si">{</span><span class="n">table</span><span class="si">}</span><span class="s2">."</span> +</span><span id="L-274"><a href="#L-274"><span class="linenos">274</span></a> <span class="p">)</span> +</span><span id="L-275"><a href="#L-275"><span class="linenos">275</span></a> <span class="n">table_id</span> <span class="o">=</span> <span class="nb">id</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> +</span><span id="L-276"><a href="#L-276"><span class="linenos">276</span></a> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">:</span> +</span><span id="L-277"><a href="#L-277"><span class="linenos">277</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">except_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table_id</span><span class="p">,</span> <span class="nb">set</span><span class="p">()):</span> +</span><span id="L-278"><a href="#L-278"><span class="linenos">278</span></a> <span class="n">alias_</span> <span class="o">=</span> <span class="n">replace_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">table_id</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> +</span><span id="L-279"><a href="#L-279"><span class="linenos">279</span></a> <span class="n">column</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="n">name</span><span class="p">,</span> <span class="n">table</span><span class="p">)</span> +</span><span id="L-280"><a href="#L-280"><span class="linenos">280</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">alias</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">alias_</span><span class="p">)</span> <span class="k">if</span> <span class="n">alias_</span> <span class="o">!=</span> <span class="n">name</span> <span class="k">else</span> <span class="n">column</span><span class="p">)</span> +</span><span id="L-281"><a href="#L-281"><span class="linenos">281</span></a> +</span><span id="L-282"><a href="#L-282"><span class="linenos">282</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">new_selections</span><span class="p">)</span> +</span><span id="L-283"><a href="#L-283"><span class="linenos">283</span></a> +</span><span id="L-284"><a href="#L-284"><span class="linenos">284</span></a> +</span><span id="L-285"><a href="#L-285"><span class="linenos">285</span></a><span class="k">def</span> <span class="nf">_add_except_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">except_columns</span><span class="p">):</span> +</span><span id="L-286"><a href="#L-286"><span class="linenos">286</span></a> <span class="n">except_</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">"except"</span><span class="p">)</span> +</span><span id="L-287"><a href="#L-287"><span class="linenos">287</span></a> +</span><span id="L-288"><a href="#L-288"><span class="linenos">288</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">except_</span><span class="p">:</span> +</span><span id="L-289"><a href="#L-289"><span class="linenos">289</span></a> <span class="k">return</span> +</span><span id="L-290"><a href="#L-290"><span class="linenos">290</span></a> +</span><span id="L-291"><a href="#L-291"><span class="linenos">291</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="p">{</span><span class="n">e</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">except_</span><span class="p">}</span> +</span><span id="L-292"><a href="#L-292"><span class="linenos">292</span></a> +</span><span id="L-293"><a href="#L-293"><span class="linenos">293</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-294"><a href="#L-294"><span class="linenos">294</span></a> <span class="n">except_columns</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">table</span><span class="p">)]</span> <span class="o">=</span> <span class="n">columns</span> +</span><span id="L-295"><a href="#L-295"><span class="linenos">295</span></a> +</span><span id="L-296"><a href="#L-296"><span class="linenos">296</span></a> +</span><span id="L-297"><a href="#L-297"><span class="linenos">297</span></a><span class="k">def</span> <span class="nf">_add_replace_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">replace_columns</span><span class="p">):</span> +</span><span id="L-298"><a href="#L-298"><span class="linenos">298</span></a> <span class="n">replace</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">"replace"</span><span class="p">)</span> +</span><span id="L-299"><a href="#L-299"><span class="linenos">299</span></a> +</span><span id="L-300"><a href="#L-300"><span class="linenos">300</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">replace</span><span class="p">:</span> +</span><span id="L-301"><a href="#L-301"><span class="linenos">301</span></a> <span class="k">return</span> +</span><span id="L-302"><a href="#L-302"><span class="linenos">302</span></a> +</span><span id="L-303"><a href="#L-303"><span class="linenos">303</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="p">{</span><span class="n">e</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">alias</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">replace</span><span class="p">}</span> +</span><span id="L-304"><a href="#L-304"><span class="linenos">304</span></a> +</span><span id="L-305"><a href="#L-305"><span class="linenos">305</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span> +</span><span id="L-306"><a href="#L-306"><span class="linenos">306</span></a> <span class="n">replace_columns</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">table</span><span class="p">)]</span> <span class="o">=</span> <span class="n">columns</span> +</span><span id="L-307"><a href="#L-307"><span class="linenos">307</span></a> +</span><span id="L-308"><a href="#L-308"><span class="linenos">308</span></a> +</span><span id="L-309"><a href="#L-309"><span class="linenos">309</span></a><span class="k">def</span> <span class="nf">_qualify_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-310"><a href="#L-310"><span class="linenos">310</span></a><span class="w"> </span><span class="sd">"""Ensure all output columns are aliased"""</span> +</span><span id="L-311"><a href="#L-311"><span class="linenos">311</span></a> <span class="n">new_selections</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-312"><a href="#L-312"><span class="linenos">312</span></a> +</span><span id="L-313"><a href="#L-313"><span class="linenos">313</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">selection</span><span class="p">,</span> <span class="n">aliased_column</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span> +</span><span id="L-314"><a href="#L-314"><span class="linenos">314</span></a> <span class="n">itertools</span><span class="o">.</span><span class="n">zip_longest</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">selects</span><span class="p">,</span> <span class="n">scope</span><span class="o">.</span><span class="n">outer_column_list</span><span class="p">)</span> +</span><span id="L-315"><a href="#L-315"><span class="linenos">315</span></a> <span class="p">):</span> +</span><span id="L-316"><a href="#L-316"><span class="linenos">316</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">selection</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-317"><a href="#L-317"><span class="linenos">317</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">selection</span><span class="o">.</span><span class="n">output_name</span><span class="p">:</span> +</span><span id="L-318"><a href="#L-318"><span class="linenos">318</span></a> <span class="n">selection</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="sa">f</span><span class="s2">"_col_</span><span class="si">{</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span><span class="p">)))</span> +</span><span id="L-319"><a href="#L-319"><span class="linenos">319</span></a> <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">selection</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Alias</span><span class="p">):</span> +</span><span id="L-320"><a href="#L-320"><span class="linenos">320</span></a> <span class="n">alias_</span> <span class="o">=</span> <span class="n">alias</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s2">""</span><span class="p">),</span> <span class="n">alias</span><span class="o">=</span><span class="n">selection</span><span class="o">.</span><span class="n">output_name</span> <span class="ow">or</span> <span class="sa">f</span><span class="s2">"_col_</span><span class="si">{</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-321"><a href="#L-321"><span class="linenos">321</span></a> <span class="n">alias_</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">selection</span><span class="p">)</span> +</span><span id="L-322"><a href="#L-322"><span class="linenos">322</span></a> <span class="n">selection</span> <span class="o">=</span> <span class="n">alias_</span> +</span><span id="L-323"><a href="#L-323"><span class="linenos">323</span></a> +</span><span id="L-324"><a href="#L-324"><span class="linenos">324</span></a> <span class="k">if</span> <span class="n">aliased_column</span><span class="p">:</span> +</span><span id="L-325"><a href="#L-325"><span class="linenos">325</span></a> <span class="n">selection</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">aliased_column</span><span class="p">))</span> +</span><span id="L-326"><a href="#L-326"><span class="linenos">326</span></a> +</span><span id="L-327"><a href="#L-327"><span class="linenos">327</span></a> <span class="n">new_selections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selection</span><span class="p">)</span> +</span><span id="L-328"><a href="#L-328"><span class="linenos">328</span></a> +</span><span id="L-329"><a href="#L-329"><span class="linenos">329</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"expressions"</span><span class="p">,</span> <span class="n">new_selections</span><span class="p">)</span> +</span><span id="L-330"><a href="#L-330"><span class="linenos">330</span></a> +</span><span id="L-331"><a href="#L-331"><span class="linenos">331</span></a> +</span><span id="L-332"><a href="#L-332"><span class="linenos">332</span></a><span class="k">class</span> <span class="nc">_Resolver</span><span class="p">:</span> +</span><span id="L-333"><a href="#L-333"><span class="linenos">333</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-334"><a href="#L-334"><span class="linenos">334</span></a><span class="sd"> Helper for resolving columns.</span> +</span><span id="L-335"><a href="#L-335"><span class="linenos">335</span></a> +</span><span id="L-336"><a href="#L-336"><span class="linenos">336</span></a><span class="sd"> This is a class so we can lazily load some things and easily share them across functions.</span> +</span><span id="L-337"><a href="#L-337"><span class="linenos">337</span></a><span class="sd"> """</span> +</span><span id="L-338"><a href="#L-338"><span class="linenos">338</span></a> +</span><span id="L-339"><a href="#L-339"><span class="linenos">339</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">schema</span><span class="p">):</span> +</span><span id="L-340"><a href="#L-340"><span class="linenos">340</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span> <span class="o">=</span> <span class="n">scope</span> +</span><span id="L-341"><a href="#L-341"><span class="linenos">341</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span> +</span><span id="L-342"><a href="#L-342"><span class="linenos">342</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_source_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-343"><a href="#L-343"><span class="linenos">343</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_unambiguous_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-344"><a href="#L-344"><span class="linenos">344</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_all_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-345"><a href="#L-345"><span class="linenos">345</span></a> +</span><span id="L-346"><a href="#L-346"><span class="linenos">346</span></a> <span class="k">def</span> <span class="nf">get_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">column_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span> +</span><span id="L-347"><a href="#L-347"><span class="linenos">347</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-348"><a href="#L-348"><span class="linenos">348</span></a><span class="sd"> Get the table for a column name.</span> +</span><span id="L-349"><a href="#L-349"><span class="linenos">349</span></a> +</span><span id="L-350"><a href="#L-350"><span class="linenos">350</span></a><span class="sd"> Args:</span> +</span><span id="L-351"><a href="#L-351"><span class="linenos">351</span></a><span class="sd"> column_name: The column name to find the table for.</span> +</span><span id="L-352"><a href="#L-352"><span class="linenos">352</span></a><span class="sd"> Returns:</span> +</span><span id="L-353"><a href="#L-353"><span class="linenos">353</span></a><span class="sd"> The table name if it can be found/inferred.</span> +</span><span id="L-354"><a href="#L-354"><span class="linenos">354</span></a><span class="sd"> """</span> +</span><span id="L-355"><a href="#L-355"><span class="linenos">355</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unambiguous_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-356"><a href="#L-356"><span class="linenos">356</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_unambiguous_columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_unambiguous_columns</span><span class="p">(</span> +</span><span id="L-357"><a href="#L-357"><span class="linenos">357</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_get_all_source_columns</span><span class="p">()</span> +</span><span id="L-358"><a href="#L-358"><span class="linenos">358</span></a> <span class="p">)</span> +</span><span id="L-359"><a href="#L-359"><span class="linenos">359</span></a> +</span><span id="L-360"><a href="#L-360"><span class="linenos">360</span></a> <span class="n">table</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unambiguous_columns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">column_name</span><span class="p">)</span> +</span><span id="L-361"><a href="#L-361"><span class="linenos">361</span></a> +</span><span id="L-362"><a href="#L-362"><span class="linenos">362</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">table</span><span class="p">:</span> +</span><span id="L-363"><a href="#L-363"><span class="linenos">363</span></a> <span class="n">sources_without_schema</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span> +</span><span id="L-364"><a href="#L-364"><span class="linenos">364</span></a> <span class="n">source</span> <span class="k">for</span> <span class="n">source</span><span class="p">,</span> <span class="n">columns</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_all_source_columns</span><span class="p">()</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">columns</span> +</span><span id="L-365"><a href="#L-365"><span class="linenos">365</span></a> <span class="p">)</span> +</span><span id="L-366"><a href="#L-366"><span class="linenos">366</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sources_without_schema</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> +</span><span id="L-367"><a href="#L-367"><span class="linenos">367</span></a> <span class="k">return</span> <span class="n">sources_without_schema</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="L-368"><a href="#L-368"><span class="linenos">368</span></a> +</span><span id="L-369"><a href="#L-369"><span class="linenos">369</span></a> <span class="k">return</span> <span class="n">table</span> +</span><span id="L-370"><a href="#L-370"><span class="linenos">370</span></a> +</span><span id="L-371"><a href="#L-371"><span class="linenos">371</span></a> <span class="nd">@property</span> +</span><span id="L-372"><a href="#L-372"><span class="linenos">372</span></a> <span class="k">def</span> <span class="nf">all_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-373"><a href="#L-373"><span class="linenos">373</span></a><span class="w"> </span><span class="sd">"""All available columns of all sources in this scope"""</span> +</span><span id="L-374"><a href="#L-374"><span class="linenos">374</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-375"><a href="#L-375"><span class="linenos">375</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_all_columns</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-376"><a href="#L-376"><span class="linenos">376</span></a> <span class="n">column</span> <span class="k">for</span> <span class="n">columns</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_all_source_columns</span><span class="p">()</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span> +</span><span id="L-377"><a href="#L-377"><span class="linenos">377</span></a> <span class="p">}</span> +</span><span id="L-378"><a href="#L-378"><span class="linenos">378</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_columns</span> +</span><span id="L-379"><a href="#L-379"><span class="linenos">379</span></a> +</span><span id="L-380"><a href="#L-380"><span class="linenos">380</span></a> <span class="k">def</span> <span class="nf">get_source_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">only_visible</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-381"><a href="#L-381"><span class="linenos">381</span></a><span class="w"> </span><span class="sd">"""Resolve the source columns for a given source `name`"""</span> +</span><span id="L-382"><a href="#L-382"><span class="linenos">382</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="L-383"><a href="#L-383"><span class="linenos">383</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown table: </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-384"><a href="#L-384"><span class="linenos">384</span></a> +</span><span id="L-385"><a href="#L-385"><span class="linenos">385</span></a> <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> +</span><span id="L-386"><a href="#L-386"><span class="linenos">386</span></a> +</span><span id="L-387"><a href="#L-387"><span class="linenos">387</span></a> <span class="c1"># If referencing a table, return the columns from the schema</span> +</span><span id="L-388"><a href="#L-388"><span class="linenos">388</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-389"><a href="#L-389"><span class="linenos">389</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="o">.</span><span class="n">column_names</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">only_visible</span><span class="p">)</span> +</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a> +</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Values</span><span class="p">):</span> +</span><span id="L-392"><a href="#L-392"><span class="linenos">392</span></a> <span class="k">return</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">alias_column_names</span> +</span><span id="L-393"><a href="#L-393"><span class="linenos">393</span></a> +</span><span id="L-394"><a href="#L-394"><span class="linenos">394</span></a> <span class="c1"># Otherwise, if referencing another scope, return that scope's named selects</span> +</span><span id="L-395"><a href="#L-395"><span class="linenos">395</span></a> <span class="k">return</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span> +</span><span id="L-396"><a href="#L-396"><span class="linenos">396</span></a> +</span><span id="L-397"><a href="#L-397"><span class="linenos">397</span></a> <span class="k">def</span> <span class="nf">_get_all_source_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-398"><a href="#L-398"><span class="linenos">398</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_source_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-399"><a href="#L-399"><span class="linenos">399</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_source_columns</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-400"><a href="#L-400"><span class="linenos">400</span></a> <span class="n">k</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_source_columns</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span> +</span><span id="L-401"><a href="#L-401"><span class="linenos">401</span></a> <span class="p">}</span> +</span><span id="L-402"><a href="#L-402"><span class="linenos">402</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_source_columns</span> +</span><span id="L-403"><a href="#L-403"><span class="linenos">403</span></a> +</span><span id="L-404"><a href="#L-404"><span class="linenos">404</span></a> <span class="k">def</span> <span class="nf">_get_unambiguous_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_columns</span><span class="p">):</span> +</span><span id="L-405"><a href="#L-405"><span class="linenos">405</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-406"><a href="#L-406"><span class="linenos">406</span></a><span class="sd"> Find all the unambiguous columns in sources.</span> +</span><span id="L-407"><a href="#L-407"><span class="linenos">407</span></a> +</span><span id="L-408"><a href="#L-408"><span class="linenos">408</span></a><span class="sd"> Args:</span> +</span><span id="L-409"><a href="#L-409"><span class="linenos">409</span></a><span class="sd"> source_columns (dict): Mapping of names to source columns</span> +</span><span id="L-410"><a href="#L-410"><span class="linenos">410</span></a><span class="sd"> Returns:</span> +</span><span id="L-411"><a href="#L-411"><span class="linenos">411</span></a><span class="sd"> dict: Mapping of column name to source name</span> +</span><span id="L-412"><a href="#L-412"><span class="linenos">412</span></a><span class="sd"> """</span> +</span><span id="L-413"><a href="#L-413"><span class="linenos">413</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source_columns</span><span class="p">:</span> +</span><span id="L-414"><a href="#L-414"><span class="linenos">414</span></a> <span class="k">return</span> <span class="p">{}</span> +</span><span id="L-415"><a href="#L-415"><span class="linenos">415</span></a> +</span><span id="L-416"><a href="#L-416"><span class="linenos">416</span></a> <span class="n">source_columns</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">source_columns</span><span class="o">.</span><span class="n">items</span><span class="p">())</span> +</span><span id="L-417"><a href="#L-417"><span class="linenos">417</span></a> +</span><span id="L-418"><a href="#L-418"><span class="linenos">418</span></a> <span class="n">first_table</span><span class="p">,</span> <span class="n">first_columns</span> <span class="o">=</span> <span class="n">source_columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> +</span><span id="L-419"><a href="#L-419"><span class="linenos">419</span></a> <span class="n">unambiguous_columns</span> <span class="o">=</span> <span class="p">{</span><span class="n">col</span><span class="p">:</span> <span class="n">first_table</span> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_unique_columns</span><span class="p">(</span><span class="n">first_columns</span><span class="p">)}</span> +</span><span id="L-420"><a href="#L-420"><span class="linenos">420</span></a> <span class="n">all_columns</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">unambiguous_columns</span><span class="p">)</span> +</span><span id="L-421"><a href="#L-421"><span class="linenos">421</span></a> +</span><span id="L-422"><a href="#L-422"><span class="linenos">422</span></a> <span class="k">for</span> <span class="n">table</span><span class="p">,</span> <span class="n">columns</span> <span class="ow">in</span> <span class="n">source_columns</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> +</span><span id="L-423"><a href="#L-423"><span class="linenos">423</span></a> <span class="n">unique</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_unique_columns</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span> +</span><span id="L-424"><a href="#L-424"><span class="linenos">424</span></a> <span class="n">ambiguous</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">all_columns</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">unique</span><span class="p">)</span> +</span><span id="L-425"><a href="#L-425"><span class="linenos">425</span></a> <span class="n">all_columns</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span> +</span><span id="L-426"><a href="#L-426"><span class="linenos">426</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">ambiguous</span><span class="p">:</span> +</span><span id="L-427"><a href="#L-427"><span class="linenos">427</span></a> <span class="n">unambiguous_columns</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-428"><a href="#L-428"><span class="linenos">428</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">unique</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">ambiguous</span><span class="p">):</span> +</span><span id="L-429"><a href="#L-429"><span class="linenos">429</span></a> <span class="n">unambiguous_columns</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="n">table</span> +</span><span id="L-430"><a href="#L-430"><span class="linenos">430</span></a> +</span><span id="L-431"><a href="#L-431"><span class="linenos">431</span></a> <span class="k">return</span> <span class="n">unambiguous_columns</span> +</span><span id="L-432"><a href="#L-432"><span class="linenos">432</span></a> +</span><span id="L-433"><a href="#L-433"><span class="linenos">433</span></a> <span class="nd">@staticmethod</span> +</span><span id="L-434"><a href="#L-434"><span class="linenos">434</span></a> <span class="k">def</span> <span class="nf">_find_unique_columns</span><span class="p">(</span><span class="n">columns</span><span class="p">):</span> +</span><span id="L-435"><a href="#L-435"><span class="linenos">435</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-436"><a href="#L-436"><span class="linenos">436</span></a><span class="sd"> Find the unique columns in a list of columns.</span> +</span><span id="L-437"><a href="#L-437"><span class="linenos">437</span></a> +</span><span id="L-438"><a href="#L-438"><span class="linenos">438</span></a><span class="sd"> Example:</span> +</span><span id="L-439"><a href="#L-439"><span class="linenos">439</span></a><span class="sd"> >>> sorted(_Resolver._find_unique_columns(["a", "b", "b", "c"]))</span> +</span><span id="L-440"><a href="#L-440"><span class="linenos">440</span></a><span class="sd"> ['a', 'c']</span> +</span><span id="L-441"><a href="#L-441"><span class="linenos">441</span></a> +</span><span id="L-442"><a href="#L-442"><span class="linenos">442</span></a><span class="sd"> This is necessary because duplicate column names are ambiguous.</span> +</span><span id="L-443"><a href="#L-443"><span class="linenos">443</span></a><span class="sd"> """</span> +</span><span id="L-444"><a href="#L-444"><span class="linenos">444</span></a> <span class="n">counts</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-445"><a href="#L-445"><span class="linenos">445</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">:</span> +</span><span id="L-446"><a href="#L-446"><span class="linenos">446</span></a> <span class="n">counts</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="n">counts</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> +</span><span id="L-447"><a href="#L-447"><span class="linenos">447</span></a> <span class="k">return</span> <span class="p">{</span><span class="n">column</span> <span class="k">for</span> <span class="n">column</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> <span class="n">counts</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">count</span> <span class="o">==</span> <span class="mi">1</span><span class="p">}</span> +</span></pre></div> + + + </section> + <section id="qualify_columns"> + <input id="qualify_columns-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">qualify_columns</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">schema</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="qualify_columns-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#qualify_columns"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="qualify_columns-11"><a href="#qualify_columns-11"><span class="linenos">11</span></a><span class="k">def</span> <span class="nf">qualify_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="p">):</span> +</span><span id="qualify_columns-12"><a href="#qualify_columns-12"><span class="linenos">12</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="qualify_columns-13"><a href="#qualify_columns-13"><span class="linenos">13</span></a><span class="sd"> Rewrite sqlglot AST to have fully qualified columns.</span> +</span><span id="qualify_columns-14"><a href="#qualify_columns-14"><span class="linenos">14</span></a> +</span><span id="qualify_columns-15"><a href="#qualify_columns-15"><span class="linenos">15</span></a><span class="sd"> Example:</span> +</span><span id="qualify_columns-16"><a href="#qualify_columns-16"><span class="linenos">16</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="qualify_columns-17"><a href="#qualify_columns-17"><span class="linenos">17</span></a><span class="sd"> >>> schema = {"tbl": {"col": "INT"}}</span> +</span><span id="qualify_columns-18"><a href="#qualify_columns-18"><span class="linenos">18</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT col FROM tbl")</span> +</span><span id="qualify_columns-19"><a href="#qualify_columns-19"><span class="linenos">19</span></a><span class="sd"> >>> qualify_columns(expression, schema).sql()</span> +</span><span id="qualify_columns-20"><a href="#qualify_columns-20"><span class="linenos">20</span></a><span class="sd"> 'SELECT tbl.col AS col FROM tbl'</span> +</span><span id="qualify_columns-21"><a href="#qualify_columns-21"><span class="linenos">21</span></a> +</span><span id="qualify_columns-22"><a href="#qualify_columns-22"><span class="linenos">22</span></a><span class="sd"> Args:</span> +</span><span id="qualify_columns-23"><a href="#qualify_columns-23"><span class="linenos">23</span></a><span class="sd"> expression (sqlglot.Expression): expression to qualify</span> +</span><span id="qualify_columns-24"><a href="#qualify_columns-24"><span class="linenos">24</span></a><span class="sd"> schema (dict|sqlglot.optimizer.Schema): Database schema</span> +</span><span id="qualify_columns-25"><a href="#qualify_columns-25"><span class="linenos">25</span></a><span class="sd"> Returns:</span> +</span><span id="qualify_columns-26"><a href="#qualify_columns-26"><span class="linenos">26</span></a><span class="sd"> sqlglot.Expression: qualified expression</span> +</span><span id="qualify_columns-27"><a href="#qualify_columns-27"><span class="linenos">27</span></a><span class="sd"> """</span> +</span><span id="qualify_columns-28"><a href="#qualify_columns-28"><span class="linenos">28</span></a> <span class="n">schema</span> <span class="o">=</span> <span class="n">ensure_schema</span><span class="p">(</span><span class="n">schema</span><span class="p">)</span> +</span><span id="qualify_columns-29"><a href="#qualify_columns-29"><span class="linenos">29</span></a> +</span><span id="qualify_columns-30"><a href="#qualify_columns-30"><span class="linenos">30</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="qualify_columns-31"><a href="#qualify_columns-31"><span class="linenos">31</span></a> <span class="n">resolver</span> <span class="o">=</span> <span class="n">_Resolver</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span> +</span><span id="qualify_columns-32"><a href="#qualify_columns-32"><span class="linenos">32</span></a> <span class="n">_pop_table_column_aliases</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">ctes</span><span class="p">)</span> +</span><span id="qualify_columns-33"><a href="#qualify_columns-33"><span class="linenos">33</span></a> <span class="n">_pop_table_column_aliases</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">)</span> +</span><span id="qualify_columns-34"><a href="#qualify_columns-34"><span class="linenos">34</span></a> <span class="n">_expand_using</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="qualify_columns-35"><a href="#qualify_columns-35"><span class="linenos">35</span></a> <span class="n">_expand_group_by</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="qualify_columns-36"><a href="#qualify_columns-36"><span class="linenos">36</span></a> <span class="n">_qualify_columns</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="qualify_columns-37"><a href="#qualify_columns-37"><span class="linenos">37</span></a> <span class="n">_expand_order_by</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="qualify_columns-38"><a href="#qualify_columns-38"><span class="linenos">38</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="qualify_columns-39"><a href="#qualify_columns-39"><span class="linenos">39</span></a> <span class="n">_expand_stars</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span> +</span><span id="qualify_columns-40"><a href="#qualify_columns-40"><span class="linenos">40</span></a> <span class="n">_qualify_outputs</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="qualify_columns-41"><a href="#qualify_columns-41"><span class="linenos">41</span></a> +</span><span id="qualify_columns-42"><a href="#qualify_columns-42"><span class="linenos">42</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to have fully qualified columns.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">schema</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"tbl"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"col"</span><span class="p">:</span> <span class="s2">"INT"</span><span class="p">}}</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT col FROM tbl"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">qualify_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT tbl.col AS col FROM tbl'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to qualify</li> +<li><strong>schema (dict|sqlglot.optimizer.Schema):</strong> Database schema</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: qualified expression</p> +</blockquote> +</div> + + + </section> + <section id="validate_qualify_columns"> + <input id="validate_qualify_columns-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">validate_qualify_columns</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="validate_qualify_columns-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#validate_qualify_columns"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="validate_qualify_columns-45"><a href="#validate_qualify_columns-45"><span class="linenos">45</span></a><span class="k">def</span> <span class="nf">validate_qualify_columns</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="validate_qualify_columns-46"><a href="#validate_qualify_columns-46"><span class="linenos">46</span></a><span class="w"> </span><span class="sd">"""Raise an `OptimizeError` if any columns aren't qualified"""</span> +</span><span id="validate_qualify_columns-47"><a href="#validate_qualify_columns-47"><span class="linenos">47</span></a> <span class="n">unqualified_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="validate_qualify_columns-48"><a href="#validate_qualify_columns-48"><span class="linenos">48</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="validate_qualify_columns-49"><a href="#validate_qualify_columns-49"><span class="linenos">49</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="validate_qualify_columns-50"><a href="#validate_qualify_columns-50"><span class="linenos">50</span></a> <span class="n">unqualified_columns</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">unqualified_columns</span><span class="p">)</span> +</span><span id="validate_qualify_columns-51"><a href="#validate_qualify_columns-51"><span class="linenos">51</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_correlated_subquery</span><span class="p">:</span> +</span><span id="validate_qualify_columns-52"><a href="#validate_qualify_columns-52"><span class="linenos">52</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unknown table: </span><span class="si">{</span><span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">table</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="validate_qualify_columns-53"><a href="#validate_qualify_columns-53"><span class="linenos">53</span></a> +</span><span id="validate_qualify_columns-54"><a href="#validate_qualify_columns-54"><span class="linenos">54</span></a> <span class="k">if</span> <span class="n">unqualified_columns</span><span class="p">:</span> +</span><span id="validate_qualify_columns-55"><a href="#validate_qualify_columns-55"><span class="linenos">55</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Ambiguous columns: </span><span class="si">{</span><span class="n">unqualified_columns</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="validate_qualify_columns-56"><a href="#validate_qualify_columns-56"><span class="linenos">56</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Raise an <code>OptimizeError</code> if any columns aren't qualified</p> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/qualify_tables.html b/docs/sqlglot/optimizer/qualify_tables.html new file mode 100644 index 0000000..acde3e6 --- /dev/null +++ b/docs/sqlglot/optimizer/qualify_tables.html @@ -0,0 +1,427 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.qualify_tables API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#qualify_tables">qualify_tables</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/qualify_tables.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.qualify_tables </h1> + + + <input id="mod-qualify_tables-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-qualify_tables-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">alias</span><span class="p">,</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">csv_reader</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">Scope</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">qualify_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> Rewrite sqlglot AST to have fully qualified tables.</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a><span class="sd"> Example:</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT 1 FROM tbl")</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a><span class="sd"> >>> qualify_tables(expression, db="db").sql()</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a><span class="sd"> 'SELECT 1 FROM db.tbl AS tbl'</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> +</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a><span class="sd"> Args:</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a><span class="sd"> expression (sqlglot.Expression): expression to qualify</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a><span class="sd"> db (str): Database name</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a><span class="sd"> catalog (str): Catalog name</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a><span class="sd"> schema: A schema to populate</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a><span class="sd"> Returns:</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a><span class="sd"> sqlglot.Expression: qualified expression</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a><span class="sd"> """</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="n">sequence</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">()</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> +</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a> <span class="n">next_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"_q_</span><span class="si">{</span><span class="nb">next</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> +</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">ctes</span> <span class="o">+</span> <span class="n">scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">):</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a> <span class="n">alias_</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"_q_</span><span class="si">{</span><span class="nb">next</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">derived_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">alias_</span><span class="p">)))</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">rename_source</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">alias_</span><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">for</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos">38</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos">39</span></a> <span class="n">identifier</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">)</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos">40</span></a> +</span><span id="L-41"><a href="#L-41"><span class="linenos">41</span></a> <span class="k">if</span> <span class="n">identifier</span><span class="p">:</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos">42</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</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">"db"</span><span class="p">):</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos">43</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"db"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">db</span><span class="p">))</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos">44</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</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">"catalog"</span><span class="p">):</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos">45</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"catalog"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">catalog</span><span class="p">))</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">if</span> <span class="ow">not</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos">48</span></a> <span class="n">source</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos">49</span></a> <span class="n">alias</span><span class="p">(</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos">50</span></a> <span class="n">source</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos">51</span></a> <span class="n">source</span><span class="o">.</span><span class="n">this</span> <span class="k">if</span> <span class="n">identifier</span> <span class="k">else</span> <span class="n">next_name</span><span class="p">(),</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos">52</span></a> <span class="n">table</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos">53</span></a> <span class="p">)</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos">54</span></a> <span class="p">)</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos">55</span></a> +</span><span id="L-56"><a href="#L-56"><span class="linenos">56</span></a> <span class="k">if</span> <span class="n">schema</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ReadCSV</span><span class="p">):</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos">57</span></a> <span class="k">with</span> <span class="n">csv_reader</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> <span class="k">as</span> <span class="n">reader</span><span class="p">:</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos">58</span></a> <span class="n">header</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">reader</span><span class="p">)</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos">59</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">reader</span><span class="p">)</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos">60</span></a> <span class="n">schema</span><span class="o">.</span><span class="n">add_table</span><span class="p">(</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos">61</span></a> <span class="n">source</span><span class="p">,</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">type</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">header</span><span class="p">,</span> <span class="n">columns</span><span class="p">)}</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos">62</span></a> <span class="p">)</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos">63</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">source</span><span class="o">.</span><span class="n">is_udtf</span><span class="p">:</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos">64</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos">65</span></a> <span class="n">table_alias</span> <span class="o">=</span> <span class="n">udtf</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">next_name</span><span class="p">())</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos">66</span></a> <span class="n">udtf</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">table_alias</span><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">if</span> <span class="ow">not</span> <span class="n">table_alias</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos">69</span></a> <span class="n">table_alias</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">next_name</span><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="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="qualify_tables"> + <input id="qualify_tables-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">qualify_tables</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">db</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">catalog</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="n">schema</span><span class="o">=</span><span class="kc">None</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="qualify_tables-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#qualify_tables"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="qualify_tables-9"><a href="#qualify_tables-9"><span class="linenos"> 9</span></a><span class="k">def</span> <span class="nf">qualify_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">catalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> +</span><span id="qualify_tables-10"><a href="#qualify_tables-10"><span class="linenos">10</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="qualify_tables-11"><a href="#qualify_tables-11"><span class="linenos">11</span></a><span class="sd"> Rewrite sqlglot AST to have fully qualified tables.</span> +</span><span id="qualify_tables-12"><a href="#qualify_tables-12"><span class="linenos">12</span></a> +</span><span id="qualify_tables-13"><a href="#qualify_tables-13"><span class="linenos">13</span></a><span class="sd"> Example:</span> +</span><span id="qualify_tables-14"><a href="#qualify_tables-14"><span class="linenos">14</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="qualify_tables-15"><a href="#qualify_tables-15"><span class="linenos">15</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT 1 FROM tbl")</span> +</span><span id="qualify_tables-16"><a href="#qualify_tables-16"><span class="linenos">16</span></a><span class="sd"> >>> qualify_tables(expression, db="db").sql()</span> +</span><span id="qualify_tables-17"><a href="#qualify_tables-17"><span class="linenos">17</span></a><span class="sd"> 'SELECT 1 FROM db.tbl AS tbl'</span> +</span><span id="qualify_tables-18"><a href="#qualify_tables-18"><span class="linenos">18</span></a> +</span><span id="qualify_tables-19"><a href="#qualify_tables-19"><span class="linenos">19</span></a><span class="sd"> Args:</span> +</span><span id="qualify_tables-20"><a href="#qualify_tables-20"><span class="linenos">20</span></a><span class="sd"> expression (sqlglot.Expression): expression to qualify</span> +</span><span id="qualify_tables-21"><a href="#qualify_tables-21"><span class="linenos">21</span></a><span class="sd"> db (str): Database name</span> +</span><span id="qualify_tables-22"><a href="#qualify_tables-22"><span class="linenos">22</span></a><span class="sd"> catalog (str): Catalog name</span> +</span><span id="qualify_tables-23"><a href="#qualify_tables-23"><span class="linenos">23</span></a><span class="sd"> schema: A schema to populate</span> +</span><span id="qualify_tables-24"><a href="#qualify_tables-24"><span class="linenos">24</span></a><span class="sd"> Returns:</span> +</span><span id="qualify_tables-25"><a href="#qualify_tables-25"><span class="linenos">25</span></a><span class="sd"> sqlglot.Expression: qualified expression</span> +</span><span id="qualify_tables-26"><a href="#qualify_tables-26"><span class="linenos">26</span></a><span class="sd"> """</span> +</span><span id="qualify_tables-27"><a href="#qualify_tables-27"><span class="linenos">27</span></a> <span class="n">sequence</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">()</span> +</span><span id="qualify_tables-28"><a href="#qualify_tables-28"><span class="linenos">28</span></a> +</span><span id="qualify_tables-29"><a href="#qualify_tables-29"><span class="linenos">29</span></a> <span class="n">next_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"_q_</span><span class="si">{</span><span class="nb">next</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> +</span><span id="qualify_tables-30"><a href="#qualify_tables-30"><span class="linenos">30</span></a> +</span><span id="qualify_tables-31"><a href="#qualify_tables-31"><span class="linenos">31</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="qualify_tables-32"><a href="#qualify_tables-32"><span class="linenos">32</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">ctes</span> <span class="o">+</span> <span class="n">scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="qualify_tables-33"><a href="#qualify_tables-33"><span class="linenos">33</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">):</span> +</span><span id="qualify_tables-34"><a href="#qualify_tables-34"><span class="linenos">34</span></a> <span class="n">alias_</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"_q_</span><span class="si">{</span><span class="nb">next</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> +</span><span id="qualify_tables-35"><a href="#qualify_tables-35"><span class="linenos">35</span></a> <span class="n">derived_table</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">alias_</span><span class="p">)))</span> +</span><span id="qualify_tables-36"><a href="#qualify_tables-36"><span class="linenos">36</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">rename_source</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">alias_</span><span class="p">)</span> +</span><span id="qualify_tables-37"><a href="#qualify_tables-37"><span class="linenos">37</span></a> +</span><span id="qualify_tables-38"><a href="#qualify_tables-38"><span class="linenos">38</span></a> <span class="k">for</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="qualify_tables-39"><a href="#qualify_tables-39"><span class="linenos">39</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="qualify_tables-40"><a href="#qualify_tables-40"><span class="linenos">40</span></a> <span class="n">identifier</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Identifier</span><span class="p">)</span> +</span><span id="qualify_tables-41"><a href="#qualify_tables-41"><span class="linenos">41</span></a> +</span><span id="qualify_tables-42"><a href="#qualify_tables-42"><span class="linenos">42</span></a> <span class="k">if</span> <span class="n">identifier</span><span class="p">:</span> +</span><span id="qualify_tables-43"><a href="#qualify_tables-43"><span class="linenos">43</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</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">"db"</span><span class="p">):</span> +</span><span id="qualify_tables-44"><a href="#qualify_tables-44"><span class="linenos">44</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"db"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">db</span><span class="p">))</span> +</span><span id="qualify_tables-45"><a href="#qualify_tables-45"><span class="linenos">45</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</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">"catalog"</span><span class="p">):</span> +</span><span id="qualify_tables-46"><a href="#qualify_tables-46"><span class="linenos">46</span></a> <span class="n">source</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"catalog"</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="n">catalog</span><span class="p">))</span> +</span><span id="qualify_tables-47"><a href="#qualify_tables-47"><span class="linenos">47</span></a> +</span><span id="qualify_tables-48"><a href="#qualify_tables-48"><span class="linenos">48</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">source</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="qualify_tables-49"><a href="#qualify_tables-49"><span class="linenos">49</span></a> <span class="n">source</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span> +</span><span id="qualify_tables-50"><a href="#qualify_tables-50"><span class="linenos">50</span></a> <span class="n">alias</span><span class="p">(</span> +</span><span id="qualify_tables-51"><a href="#qualify_tables-51"><span class="linenos">51</span></a> <span class="n">source</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> +</span><span id="qualify_tables-52"><a href="#qualify_tables-52"><span class="linenos">52</span></a> <span class="n">source</span><span class="o">.</span><span class="n">this</span> <span class="k">if</span> <span class="n">identifier</span> <span class="k">else</span> <span class="n">next_name</span><span class="p">(),</span> +</span><span id="qualify_tables-53"><a href="#qualify_tables-53"><span class="linenos">53</span></a> <span class="n">table</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> +</span><span id="qualify_tables-54"><a href="#qualify_tables-54"><span class="linenos">54</span></a> <span class="p">)</span> +</span><span id="qualify_tables-55"><a href="#qualify_tables-55"><span class="linenos">55</span></a> <span class="p">)</span> +</span><span id="qualify_tables-56"><a href="#qualify_tables-56"><span class="linenos">56</span></a> +</span><span id="qualify_tables-57"><a href="#qualify_tables-57"><span class="linenos">57</span></a> <span class="k">if</span> <span class="n">schema</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">ReadCSV</span><span class="p">):</span> +</span><span id="qualify_tables-58"><a href="#qualify_tables-58"><span class="linenos">58</span></a> <span class="k">with</span> <span class="n">csv_reader</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">this</span><span class="p">)</span> <span class="k">as</span> <span class="n">reader</span><span class="p">:</span> +</span><span id="qualify_tables-59"><a href="#qualify_tables-59"><span class="linenos">59</span></a> <span class="n">header</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">reader</span><span class="p">)</span> +</span><span id="qualify_tables-60"><a href="#qualify_tables-60"><span class="linenos">60</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">reader</span><span class="p">)</span> +</span><span id="qualify_tables-61"><a href="#qualify_tables-61"><span class="linenos">61</span></a> <span class="n">schema</span><span class="o">.</span><span class="n">add_table</span><span class="p">(</span> +</span><span id="qualify_tables-62"><a href="#qualify_tables-62"><span class="linenos">62</span></a> <span class="n">source</span><span class="p">,</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">type</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">header</span><span class="p">,</span> <span class="n">columns</span><span class="p">)}</span> +</span><span id="qualify_tables-63"><a href="#qualify_tables-63"><span class="linenos">63</span></a> <span class="p">)</span> +</span><span id="qualify_tables-64"><a href="#qualify_tables-64"><span class="linenos">64</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">source</span><span class="o">.</span><span class="n">is_udtf</span><span class="p">:</span> +</span><span id="qualify_tables-65"><a href="#qualify_tables-65"><span class="linenos">65</span></a> <span class="n">udtf</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">expression</span> +</span><span id="qualify_tables-66"><a href="#qualify_tables-66"><span class="linenos">66</span></a> <span class="n">table_alias</span> <span class="o">=</span> <span class="n">udtf</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">)</span> <span class="ow">or</span> <span class="n">exp</span><span class="o">.</span><span class="n">TableAlias</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">next_name</span><span class="p">())</span> +</span><span id="qualify_tables-67"><a href="#qualify_tables-67"><span class="linenos">67</span></a> <span class="n">udtf</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"alias"</span><span class="p">,</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="qualify_tables-68"><a href="#qualify_tables-68"><span class="linenos">68</span></a> +</span><span id="qualify_tables-69"><a href="#qualify_tables-69"><span class="linenos">69</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">table_alias</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> +</span><span id="qualify_tables-70"><a href="#qualify_tables-70"><span class="linenos">70</span></a> <span class="n">table_alias</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">next_name</span><span class="p">())</span> +</span><span id="qualify_tables-71"><a href="#qualify_tables-71"><span class="linenos">71</span></a> +</span><span id="qualify_tables-72"><a href="#qualify_tables-72"><span class="linenos">72</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to have fully qualified tables.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT 1 FROM tbl"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">qualify_tables</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="s2">"db"</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT 1 FROM db.tbl AS tbl'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to qualify</li> +<li><strong>db (str):</strong> Database name</li> +<li><strong>catalog (str):</strong> Catalog name</li> +<li><strong>schema:</strong> A schema to populate</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: qualified expression</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/scope.html b/docs/sqlglot/optimizer/scope.html new file mode 100644 index 0000000..94b5f5b --- /dev/null +++ b/docs/sqlglot/optimizer/scope.html @@ -0,0 +1,2512 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.scope API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="class" href="#ScopeType">ScopeType</a> + <ul class="memberlist"> + <li> + <a class="variable" href="#ScopeType.ROOT">ROOT</a> + </li> + <li> + <a class="variable" href="#ScopeType.SUBQUERY">SUBQUERY</a> + </li> + <li> + <a class="variable" href="#ScopeType.DERIVED_TABLE">DERIVED_TABLE</a> + </li> + <li> + <a class="variable" href="#ScopeType.CTE">CTE</a> + </li> + <li> + <a class="variable" href="#ScopeType.UNION">UNION</a> + </li> + <li> + <a class="variable" href="#ScopeType.UDTF">UDTF</a> + </li> + </ul> + + </li> + <li> + <a class="class" href="#Scope">Scope</a> + <ul class="memberlist"> + <li> + <a class="function" href="#Scope.__init__">Scope</a> + </li> + <li> + <a class="function" href="#Scope.clear_cache">clear_cache</a> + </li> + <li> + <a class="function" href="#Scope.branch">branch</a> + </li> + <li> + <a class="function" href="#Scope.walk">walk</a> + </li> + <li> + <a class="function" href="#Scope.find">find</a> + </li> + <li> + <a class="function" href="#Scope.find_all">find_all</a> + </li> + <li> + <a class="function" href="#Scope.replace">replace</a> + </li> + <li> + <a class="variable" href="#Scope.tables">tables</a> + </li> + <li> + <a class="variable" href="#Scope.ctes">ctes</a> + </li> + <li> + <a class="variable" href="#Scope.derived_tables">derived_tables</a> + </li> + <li> + <a class="variable" href="#Scope.subqueries">subqueries</a> + </li> + <li> + <a class="variable" href="#Scope.columns">columns</a> + </li> + <li> + <a class="variable" href="#Scope.selected_sources">selected_sources</a> + </li> + <li> + <a class="variable" href="#Scope.cte_sources">cte_sources</a> + </li> + <li> + <a class="variable" href="#Scope.selects">selects</a> + </li> + <li> + <a class="variable" href="#Scope.external_columns">external_columns</a> + </li> + <li> + <a class="variable" href="#Scope.unqualified_columns">unqualified_columns</a> + </li> + <li> + <a class="variable" href="#Scope.join_hints">join_hints</a> + </li> + <li> + <a class="function" href="#Scope.source_columns">source_columns</a> + </li> + <li> + <a class="variable" href="#Scope.is_subquery">is_subquery</a> + </li> + <li> + <a class="variable" href="#Scope.is_derived_table">is_derived_table</a> + </li> + <li> + <a class="variable" href="#Scope.is_union">is_union</a> + </li> + <li> + <a class="variable" href="#Scope.is_cte">is_cte</a> + </li> + <li> + <a class="variable" href="#Scope.is_root">is_root</a> + </li> + <li> + <a class="variable" href="#Scope.is_udtf">is_udtf</a> + </li> + <li> + <a class="variable" href="#Scope.is_correlated_subquery">is_correlated_subquery</a> + </li> + <li> + <a class="function" href="#Scope.rename_source">rename_source</a> + </li> + <li> + <a class="function" href="#Scope.add_source">add_source</a> + </li> + <li> + <a class="function" href="#Scope.remove_source">remove_source</a> + </li> + <li> + <a class="function" href="#Scope.traverse">traverse</a> + </li> + <li> + <a class="function" href="#Scope.ref_count">ref_count</a> + </li> + </ul> + + </li> + <li> + <a class="function" href="#traverse_scope">traverse_scope</a> + </li> + <li> + <a class="function" href="#build_scope">build_scope</a> + </li> + <li> + <a class="function" href="#walk_in_scope">walk_in_scope</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/scope.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.scope </h1> + + + <input id="mod-scope-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-scope-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">Enum</span><span class="p">,</span> <span class="n">auto</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="kn">from</span> <span class="nn">sqlglot.errors</span> <span class="kn">import</span> <span class="n">OptimizeError</span> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="k">class</span> <span class="nc">ScopeType</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a> <span class="n">ROOT</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a> <span class="n">SUBQUERY</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a> <span class="n">DERIVED_TABLE</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a> <span class="n">CTE</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a> <span class="n">UNION</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a> <span class="n">UDTF</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="k">class</span> <span class="nc">Scope</span><span class="p">:</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Selection scope.</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> Attributes:</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> expression (exp.Select|exp.Union): Root expression of this scope</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> sources (dict[str, exp.Table|Scope]): Mapping of source name to either</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> a Table expression or another Scope instance. For example:</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> SELECT * FROM x {"x": Table(this="x")}</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> SELECT * FROM x AS y {"y": Table(this="x")}</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a><span class="sd"> SELECT * FROM (SELECT ...) AS y {"y": Scope(...)}</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a><span class="sd"> outer_column_list (list[str]): If this is a derived table or CTE, and the outer query</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a><span class="sd"> defines a column list of it's alias of this scope, this is that list of columns.</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a><span class="sd"> For example:</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a><span class="sd"> SELECT * FROM (SELECT ...) AS y(col1, col2)</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a><span class="sd"> The inner query would have `["col1", "col2"]` for its `outer_column_list`</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a><span class="sd"> parent (Scope): Parent scope</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a><span class="sd"> scope_type (ScopeType): Type of this scope, relative to it's parent</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a><span class="sd"> subquery_scopes (list[Scope]): List of all child scopes for subqueries</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a><span class="sd"> cte_scopes = (list[Scope]) List of all child scopes for CTEs</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a><span class="sd"> derived_table_scopes = (list[Scope]) List of all child scopes for derived_tables</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a><span class="sd"> union_scopes (list[Scope, Scope]): If this Scope is for a Union expression, this will be</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a><span class="sd"> a list of the left and right child scopes.</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a><span class="sd"> """</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="bp">self</span><span class="p">,</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="n">expression</span><span class="p">,</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="n">sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> <span class="n">outer_column_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">ROOT</span><span class="p">,</span> +</span><span id="L-50"><a href="#L-50"><span class="linenos"> 50</span></a> <span class="p">):</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="n">sources</span> <span class="ow">or</span> <span class="p">{}</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">outer_column_list</span> <span class="o">=</span> <span class="n">outer_column_list</span> <span class="ow">or</span> <span class="p">[]</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">=</span> <span class="n">scope_type</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="k">def</span> <span class="nf">clear_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="k">def</span> <span class="nf">branch</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">scope_type</span><span class="p">,</span> <span class="n">chain_sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a><span class="w"> </span><span class="sd">"""Branch from the current scope to a new, inner scope"""</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="k">return</span> <span class="n">Scope</span><span class="p">(</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">sources</span><span class="o">=</span><span class="p">{</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">cte_sources</span><span class="p">,</span> <span class="o">**</span><span class="p">(</span><span class="n">chain_sources</span> <span class="ow">or</span> <span class="p">{})},</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">scope_type</span><span class="p">,</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="p">)</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">def</span> <span class="nf">_collect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="o">=</span> <span class="p">[]</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="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">:</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="k">continue</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Star</span><span class="p">):</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</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">JoinHint</span><span class="p">):</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">JoinHint</span><span class="p">):</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">):</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)):</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subqueryable</span><span class="p">):</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="k">def</span> <span class="nf">_ensure_collected</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span><span class="p">:</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collect</span><span class="p">()</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="k">def</span> <span class="nf">walk</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">return</span> <span class="n">walk_in_scope</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">)</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a><span class="sd"> Returns the first node in this scope which matches at least one of the specified types.</span> +</span><span id="L-122"><a href="#L-122"><span class="linenos">122</span></a> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a><span class="sd"> Args:</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a><span class="sd"> Returns:</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a><span class="sd"> exp.Expression: the node which matches the criteria or None if no node matching</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a><span class="sd"> the criteria was found.</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a><span class="sd"> """</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a><span class="sd"> Returns a generator object which visits all nodes in this scope and only yields those that</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a><span class="sd"> match at least one of the specified expression types.</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a><span class="sd"> Args:</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a><span class="sd"> Yields:</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a><span class="sd"> exp.Expression: nodes</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a><span class="sd"> """</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="k">for</span> <span class="n">expression</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">):</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">expression_types</span><span class="p">):</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="k">yield</span> <span class="n">expression</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="k">def</span> <span class="nf">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a><span class="sd"> Replace `old` with `new`.</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a><span class="sd"> This can be used instead of `exp.Expression.replace` to ensure the `Scope` is kept up-to-date.</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a><span class="sd"> Args:</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a><span class="sd"> old (exp.Expression): old node</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a><span class="sd"> new (exp.Expression): new node</span> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a><span class="sd"> """</span> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="n">old</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">new</span><span class="p">)</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="nd">@property</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="k">def</span> <span class="nf">tables</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a><span class="sd"> List of tables in this scope.</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a><span class="sd"> Returns:</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a><span class="sd"> list[exp.Table]: tables</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a><span class="sd"> """</span> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="nd">@property</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="k">def</span> <span class="nf">ctes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a><span class="sd"> List of CTEs in this scope.</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a><span class="sd"> Returns:</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a><span class="sd"> list[exp.CTE]: ctes</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a><span class="sd"> """</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="nd">@property</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="k">def</span> <span class="nf">derived_tables</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a><span class="sd"> List of derived tables in this scope.</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a><span class="sd"> For example:</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a><span class="sd"> SELECT * FROM (SELECT ...) <- that's a derived table</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a><span class="sd"> Returns:</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a><span class="sd"> list[exp.Subquery]: derived tables</span> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a><span class="sd"> """</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> <span class="nd">@property</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">def</span> <span class="nf">subqueries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a><span class="sd"> List of subqueries in this scope.</span> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a><span class="sd"> For example:</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a><span class="sd"> SELECT * FROM x WHERE a IN (SELECT ...) <- that's a subquery</span> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a><span class="sd"> Returns:</span> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a><span class="sd"> list[exp.Subqueryable]: subqueries</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a><span class="sd"> """</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> <span class="nd">@property</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="k">def</span> <span class="nf">columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a><span class="sd"> List of columns in this scope.</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a><span class="sd"> Returns:</span> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a><span class="sd"> list[exp.Column]: Column instances in this scope, plus any</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a><span class="sd"> Columns that reference this scope from correlated subqueries.</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a><span class="sd"> """</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a> <span class="n">external_columns</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="n">column</span> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="p">]</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="n">named_selects</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span> <span class="o">+</span> <span class="n">external_columns</span><span class="p">:</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> <span class="n">ancestor</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Qualify</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Order</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a> <span class="ow">not</span> <span class="n">ancestor</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a> <span class="c1"># Window functions can have an ORDER BY clause</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ancestor</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">Select</span><span class="p">)</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> <span class="ow">or</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">named_selects</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ancestor</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">))</span> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> <span class="p">):</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> <span class="nd">@property</span> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> <span class="k">def</span> <span class="nf">selected_sources</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a><span class="sd"> Mapping of nodes and sources that are actually selected from in this scope.</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a><span class="sd"> That is, all tables in a schema are selectable at any point. But a</span> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a><span class="sd"> table only becomes a selected source if it's included in a FROM or JOIN clause.</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a><span class="sd"> Returns:</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a><span class="sd"> dict[str, (exp.Table|exp.Select, exp.Table|Scope)]: selected sources and nodes</span> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a><span class="sd"> """</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> <span class="n">referenced_names</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tables</span><span class="p">:</span> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> <span class="n">referenced_names</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">table</span><span class="p">))</span> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a> <span class="n">referenced_names</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">derived_table</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">unnest</span><span class="p">()))</span> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a> <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a> +</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">referenced_names</span><span class="p">:</span> +</span><span id="L-271"><a href="#L-271"><span class="linenos">271</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="L-272"><a href="#L-272"><span class="linenos">272</span></a> <span class="n">result</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">])</span> +</span><span id="L-273"><a href="#L-273"><span class="linenos">273</span></a> +</span><span id="L-274"><a href="#L-274"><span class="linenos">274</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="o">=</span> <span class="n">result</span> +</span><span id="L-275"><a href="#L-275"><span class="linenos">275</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> +</span><span id="L-276"><a href="#L-276"><span class="linenos">276</span></a> +</span><span id="L-277"><a href="#L-277"><span class="linenos">277</span></a> <span class="nd">@property</span> +</span><span id="L-278"><a href="#L-278"><span class="linenos">278</span></a> <span class="k">def</span> <span class="nf">cte_sources</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-279"><a href="#L-279"><span class="linenos">279</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-280"><a href="#L-280"><span class="linenos">280</span></a><span class="sd"> Sources that are CTEs.</span> +</span><span id="L-281"><a href="#L-281"><span class="linenos">281</span></a> +</span><span id="L-282"><a href="#L-282"><span class="linenos">282</span></a><span class="sd"> Returns:</span> +</span><span id="L-283"><a href="#L-283"><span class="linenos">283</span></a><span class="sd"> dict[str, Scope]: Mapping of source alias to Scope</span> +</span><span id="L-284"><a href="#L-284"><span class="linenos">284</span></a><span class="sd"> """</span> +</span><span id="L-285"><a href="#L-285"><span class="linenos">285</span></a> <span class="k">return</span> <span class="p">{</span> +</span><span id="L-286"><a href="#L-286"><span class="linenos">286</span></a> <span class="n">alias</span><span class="p">:</span> <span class="n">scope</span> +</span><span id="L-287"><a href="#L-287"><span class="linenos">287</span></a> <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> +</span><span id="L-288"><a href="#L-288"><span class="linenos">288</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_cte</span> +</span><span id="L-289"><a href="#L-289"><span class="linenos">289</span></a> <span class="p">}</span> +</span><span id="L-290"><a href="#L-290"><span class="linenos">290</span></a> +</span><span id="L-291"><a href="#L-291"><span class="linenos">291</span></a> <span class="nd">@property</span> +</span><span id="L-292"><a href="#L-292"><span class="linenos">292</span></a> <span class="k">def</span> <span class="nf">selects</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-293"><a href="#L-293"><span class="linenos">293</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-294"><a href="#L-294"><span class="linenos">294</span></a><span class="sd"> Select expressions of this scope.</span> +</span><span id="L-295"><a href="#L-295"><span class="linenos">295</span></a> +</span><span id="L-296"><a href="#L-296"><span class="linenos">296</span></a><span class="sd"> For example, for the following expression:</span> +</span><span id="L-297"><a href="#L-297"><span class="linenos">297</span></a><span class="sd"> SELECT 1 as a, 2 as b FROM x</span> +</span><span id="L-298"><a href="#L-298"><span class="linenos">298</span></a> +</span><span id="L-299"><a href="#L-299"><span class="linenos">299</span></a><span class="sd"> The outputs are the "1 as a" and "2 as b" expressions.</span> +</span><span id="L-300"><a href="#L-300"><span class="linenos">300</span></a> +</span><span id="L-301"><a href="#L-301"><span class="linenos">301</span></a><span class="sd"> Returns:</span> +</span><span id="L-302"><a href="#L-302"><span class="linenos">302</span></a><span class="sd"> list[exp.Expression]: expressions</span> +</span><span id="L-303"><a href="#L-303"><span class="linenos">303</span></a><span class="sd"> """</span> +</span><span id="L-304"><a href="#L-304"><span class="linenos">304</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="L-305"><a href="#L-305"><span class="linenos">305</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</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">selects</span> +</span><span id="L-306"><a href="#L-306"><span class="linenos">306</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">selects</span> +</span><span id="L-307"><a href="#L-307"><span class="linenos">307</span></a> +</span><span id="L-308"><a href="#L-308"><span class="linenos">308</span></a> <span class="nd">@property</span> +</span><span id="L-309"><a href="#L-309"><span class="linenos">309</span></a> <span class="k">def</span> <span class="nf">external_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-310"><a href="#L-310"><span class="linenos">310</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-311"><a href="#L-311"><span class="linenos">311</span></a><span class="sd"> Columns that appear to reference sources in outer scopes.</span> +</span><span id="L-312"><a href="#L-312"><span class="linenos">312</span></a> +</span><span id="L-313"><a href="#L-313"><span class="linenos">313</span></a><span class="sd"> Returns:</span> +</span><span id="L-314"><a href="#L-314"><span class="linenos">314</span></a><span class="sd"> list[exp.Column]: Column instances that don't reference</span> +</span><span id="L-315"><a href="#L-315"><span class="linenos">315</span></a><span class="sd"> sources in the current scope.</span> +</span><span id="L-316"><a href="#L-316"><span class="linenos">316</span></a><span class="sd"> """</span> +</span><span id="L-317"><a href="#L-317"><span class="linenos">317</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-318"><a href="#L-318"><span class="linenos">318</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="L-319"><a href="#L-319"><span class="linenos">319</span></a> <span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_sources</span> +</span><span id="L-320"><a href="#L-320"><span class="linenos">320</span></a> <span class="p">]</span> +</span><span id="L-321"><a href="#L-321"><span class="linenos">321</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> +</span><span id="L-322"><a href="#L-322"><span class="linenos">322</span></a> +</span><span id="L-323"><a href="#L-323"><span class="linenos">323</span></a> <span class="nd">@property</span> +</span><span id="L-324"><a href="#L-324"><span class="linenos">324</span></a> <span class="k">def</span> <span class="nf">unqualified_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-325"><a href="#L-325"><span class="linenos">325</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-326"><a href="#L-326"><span class="linenos">326</span></a><span class="sd"> Unqualified columns in the current scope.</span> +</span><span id="L-327"><a href="#L-327"><span class="linenos">327</span></a> +</span><span id="L-328"><a href="#L-328"><span class="linenos">328</span></a><span class="sd"> Returns:</span> +</span><span id="L-329"><a href="#L-329"><span class="linenos">329</span></a><span class="sd"> list[exp.Column]: Unqualified columns</span> +</span><span id="L-330"><a href="#L-330"><span class="linenos">330</span></a><span class="sd"> """</span> +</span><span id="L-331"><a href="#L-331"><span class="linenos">331</span></a> <span class="k">return</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span><span class="p">]</span> +</span><span id="L-332"><a href="#L-332"><span class="linenos">332</span></a> +</span><span id="L-333"><a href="#L-333"><span class="linenos">333</span></a> <span class="nd">@property</span> +</span><span id="L-334"><a href="#L-334"><span class="linenos">334</span></a> <span class="k">def</span> <span class="nf">join_hints</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-335"><a href="#L-335"><span class="linenos">335</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-336"><a href="#L-336"><span class="linenos">336</span></a><span class="sd"> Hints that exist in the scope that reference tables</span> +</span><span id="L-337"><a href="#L-337"><span class="linenos">337</span></a> +</span><span id="L-338"><a href="#L-338"><span class="linenos">338</span></a><span class="sd"> Returns:</span> +</span><span id="L-339"><a href="#L-339"><span class="linenos">339</span></a><span class="sd"> list[exp.JoinHint]: Join hints that are referenced within the scope</span> +</span><span id="L-340"><a href="#L-340"><span class="linenos">340</span></a><span class="sd"> """</span> +</span><span id="L-341"><a href="#L-341"><span class="linenos">341</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="L-342"><a href="#L-342"><span class="linenos">342</span></a> <span class="k">return</span> <span class="p">[]</span> +</span><span id="L-343"><a href="#L-343"><span class="linenos">343</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> +</span><span id="L-344"><a href="#L-344"><span class="linenos">344</span></a> +</span><span id="L-345"><a href="#L-345"><span class="linenos">345</span></a> <span class="k">def</span> <span class="nf">source_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_name</span><span class="p">):</span> +</span><span id="L-346"><a href="#L-346"><span class="linenos">346</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-347"><a href="#L-347"><span class="linenos">347</span></a><span class="sd"> Get all columns in the current scope for a particular source.</span> +</span><span id="L-348"><a href="#L-348"><span class="linenos">348</span></a> +</span><span id="L-349"><a href="#L-349"><span class="linenos">349</span></a><span class="sd"> Args:</span> +</span><span id="L-350"><a href="#L-350"><span class="linenos">350</span></a><span class="sd"> source_name (str): Name of the source</span> +</span><span id="L-351"><a href="#L-351"><span class="linenos">351</span></a><span class="sd"> Returns:</span> +</span><span id="L-352"><a href="#L-352"><span class="linenos">352</span></a><span class="sd"> list[exp.Column]: Column instances that reference `source_name`</span> +</span><span id="L-353"><a href="#L-353"><span class="linenos">353</span></a><span class="sd"> """</span> +</span><span id="L-354"><a href="#L-354"><span class="linenos">354</span></a> <span class="k">return</span> <span class="p">[</span><span class="n">column</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">source_name</span><span class="p">]</span> +</span><span id="L-355"><a href="#L-355"><span class="linenos">355</span></a> +</span><span id="L-356"><a href="#L-356"><span class="linenos">356</span></a> <span class="nd">@property</span> +</span><span id="L-357"><a href="#L-357"><span class="linenos">357</span></a> <span class="k">def</span> <span class="nf">is_subquery</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-358"><a href="#L-358"><span class="linenos">358</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a subquery"""</span> +</span><span id="L-359"><a href="#L-359"><span class="linenos">359</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">SUBQUERY</span> +</span><span id="L-360"><a href="#L-360"><span class="linenos">360</span></a> +</span><span id="L-361"><a href="#L-361"><span class="linenos">361</span></a> <span class="nd">@property</span> +</span><span id="L-362"><a href="#L-362"><span class="linenos">362</span></a> <span class="k">def</span> <span class="nf">is_derived_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-363"><a href="#L-363"><span class="linenos">363</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a derived table"""</span> +</span><span id="L-364"><a href="#L-364"><span class="linenos">364</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">DERIVED_TABLE</span> +</span><span id="L-365"><a href="#L-365"><span class="linenos">365</span></a> +</span><span id="L-366"><a href="#L-366"><span class="linenos">366</span></a> <span class="nd">@property</span> +</span><span id="L-367"><a href="#L-367"><span class="linenos">367</span></a> <span class="k">def</span> <span class="nf">is_union</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-368"><a href="#L-368"><span class="linenos">368</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a union"""</span> +</span><span id="L-369"><a href="#L-369"><span class="linenos">369</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">UNION</span> +</span><span id="L-370"><a href="#L-370"><span class="linenos">370</span></a> +</span><span id="L-371"><a href="#L-371"><span class="linenos">371</span></a> <span class="nd">@property</span> +</span><span id="L-372"><a href="#L-372"><span class="linenos">372</span></a> <span class="k">def</span> <span class="nf">is_cte</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-373"><a href="#L-373"><span class="linenos">373</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a common table expression"""</span> +</span><span id="L-374"><a href="#L-374"><span class="linenos">374</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span> +</span><span id="L-375"><a href="#L-375"><span class="linenos">375</span></a> +</span><span id="L-376"><a href="#L-376"><span class="linenos">376</span></a> <span class="nd">@property</span> +</span><span id="L-377"><a href="#L-377"><span class="linenos">377</span></a> <span class="k">def</span> <span class="nf">is_root</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-378"><a href="#L-378"><span class="linenos">378</span></a><span class="w"> </span><span class="sd">"""Determine if this is the root scope"""</span> +</span><span id="L-379"><a href="#L-379"><span class="linenos">379</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">ROOT</span> +</span><span id="L-380"><a href="#L-380"><span class="linenos">380</span></a> +</span><span id="L-381"><a href="#L-381"><span class="linenos">381</span></a> <span class="nd">@property</span> +</span><span id="L-382"><a href="#L-382"><span class="linenos">382</span></a> <span class="k">def</span> <span class="nf">is_udtf</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-383"><a href="#L-383"><span class="linenos">383</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a UDTF (User Defined Table Function)"""</span> +</span><span id="L-384"><a href="#L-384"><span class="linenos">384</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">UDTF</span> +</span><span id="L-385"><a href="#L-385"><span class="linenos">385</span></a> +</span><span id="L-386"><a href="#L-386"><span class="linenos">386</span></a> <span class="nd">@property</span> +</span><span id="L-387"><a href="#L-387"><span class="linenos">387</span></a> <span class="k">def</span> <span class="nf">is_correlated_subquery</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-388"><a href="#L-388"><span class="linenos">388</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a correlated subquery"""</span> +</span><span id="L-389"><a href="#L-389"><span class="linenos">389</span></a> <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">is_subquery</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_columns</span><span class="p">)</span> +</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a> +</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> <span class="k">def</span> <span class="nf">rename_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_name</span><span class="p">,</span> <span class="n">new_name</span><span class="p">):</span> +</span><span id="L-392"><a href="#L-392"><span class="linenos">392</span></a><span class="w"> </span><span class="sd">"""Rename a source in this scope"""</span> +</span><span id="L-393"><a href="#L-393"><span class="linenos">393</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">old_name</span> <span class="ow">or</span> <span class="s2">""</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="L-394"><a href="#L-394"><span class="linenos">394</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">new_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">columns</span> +</span><span id="L-395"><a href="#L-395"><span class="linenos">395</span></a> +</span><span id="L-396"><a href="#L-396"><span class="linenos">396</span></a> <span class="k">def</span> <span class="nf">add_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span><span class="p">):</span> +</span><span id="L-397"><a href="#L-397"><span class="linenos">397</span></a><span class="w"> </span><span class="sd">"""Add a source to this scope"""</span> +</span><span id="L-398"><a href="#L-398"><span class="linenos">398</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span> +</span><span id="L-399"><a href="#L-399"><span class="linenos">399</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-400"><a href="#L-400"><span class="linenos">400</span></a> +</span><span id="L-401"><a href="#L-401"><span class="linenos">401</span></a> <span class="k">def</span> <span class="nf">remove_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> +</span><span id="L-402"><a href="#L-402"><span class="linenos">402</span></a><span class="w"> </span><span class="sd">"""Remove a source from this scope"""</span> +</span><span id="L-403"><a href="#L-403"><span class="linenos">403</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-404"><a href="#L-404"><span class="linenos">404</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="L-405"><a href="#L-405"><span class="linenos">405</span></a> +</span><span id="L-406"><a href="#L-406"><span class="linenos">406</span></a> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-407"><a href="#L-407"><span class="linenos">407</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"Scope<</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span><span class="si">}</span><span class="s2">>"</span> +</span><span id="L-408"><a href="#L-408"><span class="linenos">408</span></a> +</span><span id="L-409"><a href="#L-409"><span class="linenos">409</span></a> <span class="k">def</span> <span class="nf">traverse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-410"><a href="#L-410"><span class="linenos">410</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-411"><a href="#L-411"><span class="linenos">411</span></a><span class="sd"> Traverse the scope tree from this node.</span> +</span><span id="L-412"><a href="#L-412"><span class="linenos">412</span></a> +</span><span id="L-413"><a href="#L-413"><span class="linenos">413</span></a><span class="sd"> Yields:</span> +</span><span id="L-414"><a href="#L-414"><span class="linenos">414</span></a><span class="sd"> Scope: scope instances in depth-first-search post-order</span> +</span><span id="L-415"><a href="#L-415"><span class="linenos">415</span></a><span class="sd"> """</span> +</span><span id="L-416"><a href="#L-416"><span class="linenos">416</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> +</span><span id="L-417"><a href="#L-417"><span class="linenos">417</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> +</span><span id="L-418"><a href="#L-418"><span class="linenos">418</span></a> <span class="p">):</span> +</span><span id="L-419"><a href="#L-419"><span class="linenos">419</span></a> <span class="k">yield from</span> <span class="n">child_scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">()</span> +</span><span id="L-420"><a href="#L-420"><span class="linenos">420</span></a> <span class="k">yield</span> <span class="bp">self</span> +</span><span id="L-421"><a href="#L-421"><span class="linenos">421</span></a> +</span><span id="L-422"><a href="#L-422"><span class="linenos">422</span></a> <span class="k">def</span> <span class="nf">ref_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="L-423"><a href="#L-423"><span class="linenos">423</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-424"><a href="#L-424"><span class="linenos">424</span></a><span class="sd"> Count the number of times each scope in this tree is referenced.</span> +</span><span id="L-425"><a href="#L-425"><span class="linenos">425</span></a> +</span><span id="L-426"><a href="#L-426"><span class="linenos">426</span></a><span class="sd"> Returns:</span> +</span><span id="L-427"><a href="#L-427"><span class="linenos">427</span></a><span class="sd"> dict[int, int]: Mapping of Scope instance ID to reference count</span> +</span><span id="L-428"><a href="#L-428"><span class="linenos">428</span></a><span class="sd"> """</span> +</span><span id="L-429"><a href="#L-429"><span class="linenos">429</span></a> <span class="n">scope_ref_count</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span> +</span><span id="L-430"><a href="#L-430"><span class="linenos">430</span></a> +</span><span id="L-431"><a href="#L-431"><span class="linenos">431</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="L-432"><a href="#L-432"><span class="linenos">432</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-433"><a href="#L-433"><span class="linenos">433</span></a> <span class="n">scope_ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o">+=</span> <span class="mi">1</span> +</span><span id="L-434"><a href="#L-434"><span class="linenos">434</span></a> +</span><span id="L-435"><a href="#L-435"><span class="linenos">435</span></a> <span class="k">return</span> <span class="n">scope_ref_count</span> +</span><span id="L-436"><a href="#L-436"><span class="linenos">436</span></a> +</span><span id="L-437"><a href="#L-437"><span class="linenos">437</span></a> +</span><span id="L-438"><a href="#L-438"><span class="linenos">438</span></a><span class="k">def</span> <span class="nf">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-439"><a href="#L-439"><span class="linenos">439</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-440"><a href="#L-440"><span class="linenos">440</span></a><span class="sd"> Traverse an expression by it's "scopes".</span> +</span><span id="L-441"><a href="#L-441"><span class="linenos">441</span></a> +</span><span id="L-442"><a href="#L-442"><span class="linenos">442</span></a><span class="sd"> "Scope" represents the current context of a Select statement.</span> +</span><span id="L-443"><a href="#L-443"><span class="linenos">443</span></a> +</span><span id="L-444"><a href="#L-444"><span class="linenos">444</span></a><span class="sd"> This is helpful for optimizing queries, where we need more information than</span> +</span><span id="L-445"><a href="#L-445"><span class="linenos">445</span></a><span class="sd"> the expression tree itself. For example, we might care about the source</span> +</span><span id="L-446"><a href="#L-446"><span class="linenos">446</span></a><span class="sd"> names within a subquery. Returns a list because a generator could result in</span> +</span><span id="L-447"><a href="#L-447"><span class="linenos">447</span></a><span class="sd"> incomplete properties which is confusing.</span> +</span><span id="L-448"><a href="#L-448"><span class="linenos">448</span></a> +</span><span id="L-449"><a href="#L-449"><span class="linenos">449</span></a><span class="sd"> Examples:</span> +</span><span id="L-450"><a href="#L-450"><span class="linenos">450</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-451"><a href="#L-451"><span class="linenos">451</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT a FROM x) AS y")</span> +</span><span id="L-452"><a href="#L-452"><span class="linenos">452</span></a><span class="sd"> >>> scopes = traverse_scope(expression)</span> +</span><span id="L-453"><a href="#L-453"><span class="linenos">453</span></a><span class="sd"> >>> scopes[0].expression.sql(), list(scopes[0].sources)</span> +</span><span id="L-454"><a href="#L-454"><span class="linenos">454</span></a><span class="sd"> ('SELECT a FROM x', ['x'])</span> +</span><span id="L-455"><a href="#L-455"><span class="linenos">455</span></a><span class="sd"> >>> scopes[1].expression.sql(), list(scopes[1].sources)</span> +</span><span id="L-456"><a href="#L-456"><span class="linenos">456</span></a><span class="sd"> ('SELECT a FROM (SELECT a FROM x) AS y', ['y'])</span> +</span><span id="L-457"><a href="#L-457"><span class="linenos">457</span></a> +</span><span id="L-458"><a href="#L-458"><span class="linenos">458</span></a><span class="sd"> Args:</span> +</span><span id="L-459"><a href="#L-459"><span class="linenos">459</span></a><span class="sd"> expression (exp.Expression): expression to traverse</span> +</span><span id="L-460"><a href="#L-460"><span class="linenos">460</span></a><span class="sd"> Returns:</span> +</span><span id="L-461"><a href="#L-461"><span class="linenos">461</span></a><span class="sd"> list[Scope]: scope instances</span> +</span><span id="L-462"><a href="#L-462"><span class="linenos">462</span></a><span class="sd"> """</span> +</span><span id="L-463"><a href="#L-463"><span class="linenos">463</span></a> <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">_traverse_scope</span><span class="p">(</span><span class="n">Scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)))</span> +</span><span id="L-464"><a href="#L-464"><span class="linenos">464</span></a> +</span><span id="L-465"><a href="#L-465"><span class="linenos">465</span></a> +</span><span id="L-466"><a href="#L-466"><span class="linenos">466</span></a><span class="k">def</span> <span class="nf">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-467"><a href="#L-467"><span class="linenos">467</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-468"><a href="#L-468"><span class="linenos">468</span></a><span class="sd"> Build a scope tree.</span> +</span><span id="L-469"><a href="#L-469"><span class="linenos">469</span></a> +</span><span id="L-470"><a href="#L-470"><span class="linenos">470</span></a><span class="sd"> Args:</span> +</span><span id="L-471"><a href="#L-471"><span class="linenos">471</span></a><span class="sd"> expression (exp.Expression): expression to build the scope tree for</span> +</span><span id="L-472"><a href="#L-472"><span class="linenos">472</span></a><span class="sd"> Returns:</span> +</span><span id="L-473"><a href="#L-473"><span class="linenos">473</span></a><span class="sd"> Scope: root scope</span> +</span><span id="L-474"><a href="#L-474"><span class="linenos">474</span></a><span class="sd"> """</span> +</span><span id="L-475"><a href="#L-475"><span class="linenos">475</span></a> <span class="k">return</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> +</span><span id="L-476"><a href="#L-476"><span class="linenos">476</span></a> +</span><span id="L-477"><a href="#L-477"><span class="linenos">477</span></a> +</span><span id="L-478"><a href="#L-478"><span class="linenos">478</span></a><span class="k">def</span> <span class="nf">_traverse_scope</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-479"><a href="#L-479"><span class="linenos">479</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">):</span> +</span><span id="L-480"><a href="#L-480"><span class="linenos">480</span></a> <span class="k">yield from</span> <span class="n">_traverse_select</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-481"><a href="#L-481"><span class="linenos">481</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="L-482"><a href="#L-482"><span class="linenos">482</span></a> <span class="k">yield from</span> <span class="n">_traverse_union</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-483"><a href="#L-483"><span class="linenos">483</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="L-484"><a href="#L-484"><span class="linenos">484</span></a> <span class="n">_set_udtf_scope</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-485"><a href="#L-485"><span class="linenos">485</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-486"><a href="#L-486"><span class="linenos">486</span></a> <span class="k">yield from</span> <span class="n">_traverse_subqueries</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-487"><a href="#L-487"><span class="linenos">487</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-488"><a href="#L-488"><span class="linenos">488</span></a> <span class="k">raise</span> <span class="n">OptimizeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Unexpected expression type: </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-489"><a href="#L-489"><span class="linenos">489</span></a> <span class="k">yield</span> <span class="n">scope</span> +</span><span id="L-490"><a href="#L-490"><span class="linenos">490</span></a> +</span><span id="L-491"><a href="#L-491"><span class="linenos">491</span></a> +</span><span id="L-492"><a href="#L-492"><span class="linenos">492</span></a><span class="k">def</span> <span class="nf">_traverse_select</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-493"><a href="#L-493"><span class="linenos">493</span></a> <span class="k">yield from</span> <span class="n">_traverse_derived_tables</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">ctes</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span><span class="p">)</span> +</span><span id="L-494"><a href="#L-494"><span class="linenos">494</span></a> <span class="k">yield from</span> <span class="n">_traverse_derived_tables</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">DERIVED_TABLE</span><span class="p">)</span> +</span><span id="L-495"><a href="#L-495"><span class="linenos">495</span></a> <span class="k">yield from</span> <span class="n">_traverse_subqueries</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-496"><a href="#L-496"><span class="linenos">496</span></a> <span class="n">_add_table_sources</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-497"><a href="#L-497"><span class="linenos">497</span></a> +</span><span id="L-498"><a href="#L-498"><span class="linenos">498</span></a> +</span><span id="L-499"><a href="#L-499"><span class="linenos">499</span></a><span class="k">def</span> <span class="nf">_traverse_union</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-500"><a href="#L-500"><span class="linenos">500</span></a> <span class="k">yield from</span> <span class="n">_traverse_derived_tables</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">ctes</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span><span class="p">)</span> +</span><span id="L-501"><a href="#L-501"><span class="linenos">501</span></a> +</span><span id="L-502"><a href="#L-502"><span class="linenos">502</span></a> <span class="c1"># The last scope to be yield should be the top most scope</span> +</span><span id="L-503"><a href="#L-503"><span class="linenos">503</span></a> <span class="n">left</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-504"><a href="#L-504"><span class="linenos">504</span></a> <span class="k">for</span> <span class="n">left</span> <span class="ow">in</span> <span class="n">_traverse_scope</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">branch</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">UNION</span><span class="p">)):</span> +</span><span id="L-505"><a href="#L-505"><span class="linenos">505</span></a> <span class="k">yield</span> <span class="n">left</span> +</span><span id="L-506"><a href="#L-506"><span class="linenos">506</span></a> +</span><span id="L-507"><a href="#L-507"><span class="linenos">507</span></a> <span class="n">right</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-508"><a href="#L-508"><span class="linenos">508</span></a> <span class="k">for</span> <span class="n">right</span> <span class="ow">in</span> <span class="n">_traverse_scope</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">branch</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">UNION</span><span class="p">)):</span> +</span><span id="L-509"><a href="#L-509"><span class="linenos">509</span></a> <span class="k">yield</span> <span class="n">right</span> +</span><span id="L-510"><a href="#L-510"><span class="linenos">510</span></a> +</span><span id="L-511"><a href="#L-511"><span class="linenos">511</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">union_scopes</span> <span class="o">=</span> <span class="p">[</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">]</span> +</span><span id="L-512"><a href="#L-512"><span class="linenos">512</span></a> +</span><span id="L-513"><a href="#L-513"><span class="linenos">513</span></a> +</span><span id="L-514"><a href="#L-514"><span class="linenos">514</span></a><span class="k">def</span> <span class="nf">_set_udtf_scope</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-515"><a href="#L-515"><span class="linenos">515</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-516"><a href="#L-516"><span class="linenos">516</span></a> <span class="n">from_</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"from"</span><span class="p">)</span> +</span><span id="L-517"><a href="#L-517"><span class="linenos">517</span></a> +</span><span id="L-518"><a href="#L-518"><span class="linenos">518</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">from_</span><span class="p">:</span> +</span><span id="L-519"><a href="#L-519"><span class="linenos">519</span></a> <span class="k">return</span> +</span><span id="L-520"><a href="#L-520"><span class="linenos">520</span></a> +</span><span id="L-521"><a href="#L-521"><span class="linenos">521</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">from_</span><span class="o">.</span><span class="n">expressions</span><span class="p">:</span> +</span><span id="L-522"><a href="#L-522"><span class="linenos">522</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">):</span> +</span><span id="L-523"><a href="#L-523"><span class="linenos">523</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> +</span><span id="L-524"><a href="#L-524"><span class="linenos">524</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">):</span> +</span><span id="L-525"><a href="#L-525"><span class="linenos">525</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">subqueries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">table</span><span class="p">)</span> +</span><span id="L-526"><a href="#L-526"><span class="linenos">526</span></a> <span class="n">_add_table_sources</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-527"><a href="#L-527"><span class="linenos">527</span></a> <span class="n">_traverse_subqueries</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> +</span><span id="L-528"><a href="#L-528"><span class="linenos">528</span></a> +</span><span id="L-529"><a href="#L-529"><span class="linenos">529</span></a> +</span><span id="L-530"><a href="#L-530"><span class="linenos">530</span></a><span class="k">def</span> <span class="nf">_traverse_derived_tables</span><span class="p">(</span><span class="n">derived_tables</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">scope_type</span><span class="p">):</span> +</span><span id="L-531"><a href="#L-531"><span class="linenos">531</span></a> <span class="n">sources</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-532"><a href="#L-532"><span class="linenos">532</span></a> <span class="n">is_cte</span> <span class="o">=</span> <span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span> +</span><span id="L-533"><a href="#L-533"><span class="linenos">533</span></a> +</span><span id="L-534"><a href="#L-534"><span class="linenos">534</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="n">derived_tables</span><span class="p">:</span> +</span><span id="L-535"><a href="#L-535"><span class="linenos">535</span></a> <span class="n">recursive_scope</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-536"><a href="#L-536"><span class="linenos">536</span></a> +</span><span id="L-537"><a href="#L-537"><span class="linenos">537</span></a> <span class="c1"># if the scope is a recursive cte, it must be in the form of</span> +</span><span id="L-538"><a href="#L-538"><span class="linenos">538</span></a> <span class="c1"># base_case UNION recursive. thus the recursive scope is the first</span> +</span><span id="L-539"><a href="#L-539"><span class="linenos">539</span></a> <span class="c1"># section of the union.</span> +</span><span id="L-540"><a href="#L-540"><span class="linenos">540</span></a> <span class="k">if</span> <span class="n">is_cte</span> <span class="ow">and</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"with"</span><span class="p">]</span><span class="o">.</span><span class="n">recursive</span><span class="p">:</span> +</span><span id="L-541"><a href="#L-541"><span class="linenos">541</span></a> <span class="n">union</span> <span class="o">=</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-542"><a href="#L-542"><span class="linenos">542</span></a> +</span><span id="L-543"><a href="#L-543"><span class="linenos">543</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">union</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="L-544"><a href="#L-544"><span class="linenos">544</span></a> <span class="n">recursive_scope</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">branch</span><span class="p">(</span><span class="n">union</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span><span class="p">)</span> +</span><span id="L-545"><a href="#L-545"><span class="linenos">545</span></a> +</span><span id="L-546"><a href="#L-546"><span class="linenos">546</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">_traverse_scope</span><span class="p">(</span> +</span><span id="L-547"><a href="#L-547"><span class="linenos">547</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">branch</span><span class="p">(</span> +</span><span id="L-548"><a href="#L-548"><span class="linenos">548</span></a> <span class="n">derived_table</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">derived_table</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">)</span> <span class="k">else</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> +</span><span id="L-549"><a href="#L-549"><span class="linenos">549</span></a> <span class="n">chain_sources</span><span class="o">=</span><span class="n">sources</span> <span class="k">if</span> <span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span> +</span><span id="L-550"><a href="#L-550"><span class="linenos">550</span></a> <span class="n">outer_column_list</span><span class="o">=</span><span class="n">derived_table</span><span class="o">.</span><span class="n">alias_column_names</span><span class="p">,</span> +</span><span id="L-551"><a href="#L-551"><span class="linenos">551</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">UDTF</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">derived_table</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">)</span> <span class="k">else</span> <span class="n">scope_type</span><span class="p">,</span> +</span><span id="L-552"><a href="#L-552"><span class="linenos">552</span></a> <span class="p">)</span> +</span><span id="L-553"><a href="#L-553"><span class="linenos">553</span></a> <span class="p">):</span> +</span><span id="L-554"><a href="#L-554"><span class="linenos">554</span></a> <span class="k">yield</span> <span class="n">child_scope</span> +</span><span id="L-555"><a href="#L-555"><span class="linenos">555</span></a> +</span><span id="L-556"><a href="#L-556"><span class="linenos">556</span></a> <span class="c1"># Tables without aliases will be set as ""</span> +</span><span id="L-557"><a href="#L-557"><span class="linenos">557</span></a> <span class="c1"># This shouldn't be a problem once qualify_columns runs, as it adds aliases on everything.</span> +</span><span id="L-558"><a href="#L-558"><span class="linenos">558</span></a> <span class="c1"># Until then, this means that only a single, unaliased derived table is allowed (rather,</span> +</span><span id="L-559"><a href="#L-559"><span class="linenos">559</span></a> <span class="c1"># the latest one wins.</span> +</span><span id="L-560"><a href="#L-560"><span class="linenos">560</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-561"><a href="#L-561"><span class="linenos">561</span></a> <span class="n">sources</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">child_scope</span> +</span><span id="L-562"><a href="#L-562"><span class="linenos">562</span></a> +</span><span id="L-563"><a href="#L-563"><span class="linenos">563</span></a> <span class="k">if</span> <span class="n">recursive_scope</span><span class="p">:</span> +</span><span id="L-564"><a href="#L-564"><span class="linenos">564</span></a> <span class="n">child_scope</span><span class="o">.</span><span class="n">add_source</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">recursive_scope</span><span class="p">)</span> +</span><span id="L-565"><a href="#L-565"><span class="linenos">565</span></a> +</span><span id="L-566"><a href="#L-566"><span class="linenos">566</span></a> <span class="c1"># append the final child_scope yielded</span> +</span><span id="L-567"><a href="#L-567"><span class="linenos">567</span></a> <span class="k">if</span> <span class="n">is_cte</span><span class="p">:</span> +</span><span id="L-568"><a href="#L-568"><span class="linenos">568</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">cte_scopes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child_scope</span><span class="p">)</span> +</span><span id="L-569"><a href="#L-569"><span class="linenos">569</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-570"><a href="#L-570"><span class="linenos">570</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">derived_table_scopes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child_scope</span><span class="p">)</span> +</span><span id="L-571"><a href="#L-571"><span class="linenos">571</span></a> +</span><span id="L-572"><a href="#L-572"><span class="linenos">572</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">sources</span><span class="p">)</span> +</span><span id="L-573"><a href="#L-573"><span class="linenos">573</span></a> +</span><span id="L-574"><a href="#L-574"><span class="linenos">574</span></a> +</span><span id="L-575"><a href="#L-575"><span class="linenos">575</span></a><span class="k">def</span> <span class="nf">_add_table_sources</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-576"><a href="#L-576"><span class="linenos">576</span></a> <span class="n">sources</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-577"><a href="#L-577"><span class="linenos">577</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">tables</span><span class="p">:</span> +</span><span id="L-578"><a href="#L-578"><span class="linenos">578</span></a> <span class="n">table_name</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-579"><a href="#L-579"><span class="linenos">579</span></a> +</span><span id="L-580"><a href="#L-580"><span class="linenos">580</span></a> <span class="k">if</span> <span class="n">table</span><span class="o">.</span><span class="n">alias</span><span class="p">:</span> +</span><span id="L-581"><a href="#L-581"><span class="linenos">581</span></a> <span class="n">source_name</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-582"><a href="#L-582"><span class="linenos">582</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-583"><a href="#L-583"><span class="linenos">583</span></a> <span class="n">source_name</span> <span class="o">=</span> <span class="n">table_name</span> +</span><span id="L-584"><a href="#L-584"><span class="linenos">584</span></a> +</span><span id="L-585"><a href="#L-585"><span class="linenos">585</span></a> <span class="k">if</span> <span class="n">table_name</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="L-586"><a href="#L-586"><span class="linenos">586</span></a> <span class="c1"># This is a reference to a parent source (e.g. a CTE), not an actual table.</span> +</span><span id="L-587"><a href="#L-587"><span class="linenos">587</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">source_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">table_name</span><span class="p">]</span> +</span><span id="L-588"><a href="#L-588"><span class="linenos">588</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-589"><a href="#L-589"><span class="linenos">589</span></a> <span class="n">sources</span><span class="p">[</span><span class="n">source_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">table</span> +</span><span id="L-590"><a href="#L-590"><span class="linenos">590</span></a> +</span><span id="L-591"><a href="#L-591"><span class="linenos">591</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">sources</span><span class="p">)</span> +</span><span id="L-592"><a href="#L-592"><span class="linenos">592</span></a> +</span><span id="L-593"><a href="#L-593"><span class="linenos">593</span></a> +</span><span id="L-594"><a href="#L-594"><span class="linenos">594</span></a><span class="k">def</span> <span class="nf">_traverse_subqueries</span><span class="p">(</span><span class="n">scope</span><span class="p">):</span> +</span><span id="L-595"><a href="#L-595"><span class="linenos">595</span></a> <span class="k">for</span> <span class="n">subquery</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">subqueries</span><span class="p">:</span> +</span><span id="L-596"><a href="#L-596"><span class="linenos">596</span></a> <span class="n">top</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="L-597"><a href="#L-597"><span class="linenos">597</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">_traverse_scope</span><span class="p">(</span><span class="n">scope</span><span class="o">.</span><span class="n">branch</span><span class="p">(</span><span class="n">subquery</span><span class="p">,</span> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">SUBQUERY</span><span class="p">)):</span> +</span><span id="L-598"><a href="#L-598"><span class="linenos">598</span></a> <span class="k">yield</span> <span class="n">child_scope</span> +</span><span id="L-599"><a href="#L-599"><span class="linenos">599</span></a> <span class="n">top</span> <span class="o">=</span> <span class="n">child_scope</span> +</span><span id="L-600"><a href="#L-600"><span class="linenos">600</span></a> <span class="n">scope</span><span class="o">.</span><span class="n">subquery_scopes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">top</span><span class="p">)</span> +</span><span id="L-601"><a href="#L-601"><span class="linenos">601</span></a> +</span><span id="L-602"><a href="#L-602"><span class="linenos">602</span></a> +</span><span id="L-603"><a href="#L-603"><span class="linenos">603</span></a><span class="k">def</span> <span class="nf">walk_in_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-604"><a href="#L-604"><span class="linenos">604</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-605"><a href="#L-605"><span class="linenos">605</span></a><span class="sd"> Returns a generator object which visits all nodes in the syntrax tree, stopping at</span> +</span><span id="L-606"><a href="#L-606"><span class="linenos">606</span></a><span class="sd"> nodes that start child scopes.</span> +</span><span id="L-607"><a href="#L-607"><span class="linenos">607</span></a> +</span><span id="L-608"><a href="#L-608"><span class="linenos">608</span></a><span class="sd"> Args:</span> +</span><span id="L-609"><a href="#L-609"><span class="linenos">609</span></a><span class="sd"> expression (exp.Expression):</span> +</span><span id="L-610"><a href="#L-610"><span class="linenos">610</span></a><span class="sd"> bfs (bool): if set to True the BFS traversal order will be applied,</span> +</span><span id="L-611"><a href="#L-611"><span class="linenos">611</span></a><span class="sd"> otherwise the DFS traversal will be used instead.</span> +</span><span id="L-612"><a href="#L-612"><span class="linenos">612</span></a> +</span><span id="L-613"><a href="#L-613"><span class="linenos">613</span></a><span class="sd"> Yields:</span> +</span><span id="L-614"><a href="#L-614"><span class="linenos">614</span></a><span class="sd"> tuple[exp.Expression, Optional[exp.Expression], str]: node, parent, arg key</span> +</span><span id="L-615"><a href="#L-615"><span class="linenos">615</span></a><span class="sd"> """</span> +</span><span id="L-616"><a href="#L-616"><span class="linenos">616</span></a> <span class="c1"># We'll use this variable to pass state into the dfs generator.</span> +</span><span id="L-617"><a href="#L-617"><span class="linenos">617</span></a> <span class="c1"># Whenever we set it to True, we exclude a subtree from traversal.</span> +</span><span id="L-618"><a href="#L-618"><span class="linenos">618</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-619"><a href="#L-619"><span class="linenos">619</span></a> +</span><span id="L-620"><a href="#L-620"><span class="linenos">620</span></a> <span class="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">,</span> <span class="n">prune</span><span class="o">=</span><span class="k">lambda</span> <span class="o">*</span><span class="n">_</span><span class="p">:</span> <span class="n">prune</span><span class="p">):</span> +</span><span id="L-621"><a href="#L-621"><span class="linenos">621</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-622"><a href="#L-622"><span class="linenos">622</span></a> +</span><span id="L-623"><a href="#L-623"><span class="linenos">623</span></a> <span class="k">yield</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">key</span> +</span><span id="L-624"><a href="#L-624"><span class="linenos">624</span></a> +</span><span id="L-625"><a href="#L-625"><span class="linenos">625</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="n">expression</span><span class="p">:</span> +</span><span id="L-626"><a href="#L-626"><span class="linenos">626</span></a> <span class="k">continue</span> +</span><span id="L-627"><a href="#L-627"><span class="linenos">627</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">):</span> +</span><span id="L-628"><a href="#L-628"><span class="linenos">628</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-629"><a href="#L-629"><span class="linenos">629</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)):</span> +</span><span id="L-630"><a href="#L-630"><span class="linenos">630</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-631"><a href="#L-631"><span class="linenos">631</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subqueryable</span><span class="p">):</span> +</span><span id="L-632"><a href="#L-632"><span class="linenos">632</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span></pre></div> + + + </section> + <section id="ScopeType"> + <input id="ScopeType-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr class"> + + <span class="def">class</span> + <span class="name">ScopeType</span><wbr>(<span class="base">enum.Enum</span>): + + <label class="view-source-button" for="ScopeType-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#ScopeType"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="ScopeType-10"><a href="#ScopeType-10"><span class="linenos">10</span></a><span class="k">class</span> <span class="nc">ScopeType</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span> +</span><span id="ScopeType-11"><a href="#ScopeType-11"><span class="linenos">11</span></a> <span class="n">ROOT</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="ScopeType-12"><a href="#ScopeType-12"><span class="linenos">12</span></a> <span class="n">SUBQUERY</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="ScopeType-13"><a href="#ScopeType-13"><span class="linenos">13</span></a> <span class="n">DERIVED_TABLE</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="ScopeType-14"><a href="#ScopeType-14"><span class="linenos">14</span></a> <span class="n">CTE</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="ScopeType-15"><a href="#ScopeType-15"><span class="linenos">15</span></a> <span class="n">UNION</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span><span id="ScopeType-16"><a href="#ScopeType-16"><span class="linenos">16</span></a> <span class="n">UDTF</span> <span class="o">=</span> <span class="n">auto</span><span class="p">()</span> +</span></pre></div> + + + <div class="docstring"><p>An enumeration.</p> +</div> + + + <div id="ScopeType.ROOT" class="classattr"> + <div class="attr variable"> + <span class="name">ROOT</span><span class="default_value"> = <<a href="#ScopeType.ROOT">ScopeType.ROOT</a>: 1></span> + + + </div> + <a class="headerlink" href="#ScopeType.ROOT"></a> + + + + </div> + <div id="ScopeType.SUBQUERY" class="classattr"> + <div class="attr variable"> + <span class="name">SUBQUERY</span><span class="default_value"> = <<a href="#ScopeType.SUBQUERY">ScopeType.SUBQUERY</a>: 2></span> + + + </div> + <a class="headerlink" href="#ScopeType.SUBQUERY"></a> + + + + </div> + <div id="ScopeType.DERIVED_TABLE" class="classattr"> + <div class="attr variable"> + <span class="name">DERIVED_TABLE</span><span class="default_value"> = <<a href="#ScopeType.DERIVED_TABLE">ScopeType.DERIVED_TABLE</a>: 3></span> + + + </div> + <a class="headerlink" href="#ScopeType.DERIVED_TABLE"></a> + + + + </div> + <div id="ScopeType.CTE" class="classattr"> + <div class="attr variable"> + <span class="name">CTE</span><span class="default_value"> = <<a href="#ScopeType.CTE">ScopeType.CTE</a>: 4></span> + + + </div> + <a class="headerlink" href="#ScopeType.CTE"></a> + + + + </div> + <div id="ScopeType.UNION" class="classattr"> + <div class="attr variable"> + <span class="name">UNION</span><span class="default_value"> = <<a href="#ScopeType.UNION">ScopeType.UNION</a>: 5></span> + + + </div> + <a class="headerlink" href="#ScopeType.UNION"></a> + + + + </div> + <div id="ScopeType.UDTF" class="classattr"> + <div class="attr variable"> + <span class="name">UDTF</span><span class="default_value"> = <<a href="#ScopeType.UDTF">ScopeType.UDTF</a>: 6></span> + + + </div> + <a class="headerlink" href="#ScopeType.UDTF"></a> + + + + </div> + <div class="inherited"> + <h5>Inherited Members</h5> + <dl> + <div><dt>enum.Enum</dt> + <dd id="ScopeType.name" class="variable">name</dd> + <dd id="ScopeType.value" class="variable">value</dd> + + </div> + </dl> + </div> + </section> + <section id="Scope"> + <input id="Scope-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr class"> + + <span class="def">class</span> + <span class="name">Scope</span>: + + <label class="view-source-button" for="Scope-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope-19"><a href="#Scope-19"><span class="linenos"> 19</span></a><span class="k">class</span> <span class="nc">Scope</span><span class="p">:</span> +</span><span id="Scope-20"><a href="#Scope-20"><span class="linenos"> 20</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-21"><a href="#Scope-21"><span class="linenos"> 21</span></a><span class="sd"> Selection scope.</span> +</span><span id="Scope-22"><a href="#Scope-22"><span class="linenos"> 22</span></a> +</span><span id="Scope-23"><a href="#Scope-23"><span class="linenos"> 23</span></a><span class="sd"> Attributes:</span> +</span><span id="Scope-24"><a href="#Scope-24"><span class="linenos"> 24</span></a><span class="sd"> expression (exp.Select|exp.Union): Root expression of this scope</span> +</span><span id="Scope-25"><a href="#Scope-25"><span class="linenos"> 25</span></a><span class="sd"> sources (dict[str, exp.Table|Scope]): Mapping of source name to either</span> +</span><span id="Scope-26"><a href="#Scope-26"><span class="linenos"> 26</span></a><span class="sd"> a Table expression or another Scope instance. For example:</span> +</span><span id="Scope-27"><a href="#Scope-27"><span class="linenos"> 27</span></a><span class="sd"> SELECT * FROM x {"x": Table(this="x")}</span> +</span><span id="Scope-28"><a href="#Scope-28"><span class="linenos"> 28</span></a><span class="sd"> SELECT * FROM x AS y {"y": Table(this="x")}</span> +</span><span id="Scope-29"><a href="#Scope-29"><span class="linenos"> 29</span></a><span class="sd"> SELECT * FROM (SELECT ...) AS y {"y": Scope(...)}</span> +</span><span id="Scope-30"><a href="#Scope-30"><span class="linenos"> 30</span></a><span class="sd"> outer_column_list (list[str]): If this is a derived table or CTE, and the outer query</span> +</span><span id="Scope-31"><a href="#Scope-31"><span class="linenos"> 31</span></a><span class="sd"> defines a column list of it's alias of this scope, this is that list of columns.</span> +</span><span id="Scope-32"><a href="#Scope-32"><span class="linenos"> 32</span></a><span class="sd"> For example:</span> +</span><span id="Scope-33"><a href="#Scope-33"><span class="linenos"> 33</span></a><span class="sd"> SELECT * FROM (SELECT ...) AS y(col1, col2)</span> +</span><span id="Scope-34"><a href="#Scope-34"><span class="linenos"> 34</span></a><span class="sd"> The inner query would have `["col1", "col2"]` for its `outer_column_list`</span> +</span><span id="Scope-35"><a href="#Scope-35"><span class="linenos"> 35</span></a><span class="sd"> parent (Scope): Parent scope</span> +</span><span id="Scope-36"><a href="#Scope-36"><span class="linenos"> 36</span></a><span class="sd"> scope_type (ScopeType): Type of this scope, relative to it's parent</span> +</span><span id="Scope-37"><a href="#Scope-37"><span class="linenos"> 37</span></a><span class="sd"> subquery_scopes (list[Scope]): List of all child scopes for subqueries</span> +</span><span id="Scope-38"><a href="#Scope-38"><span class="linenos"> 38</span></a><span class="sd"> cte_scopes = (list[Scope]) List of all child scopes for CTEs</span> +</span><span id="Scope-39"><a href="#Scope-39"><span class="linenos"> 39</span></a><span class="sd"> derived_table_scopes = (list[Scope]) List of all child scopes for derived_tables</span> +</span><span id="Scope-40"><a href="#Scope-40"><span class="linenos"> 40</span></a><span class="sd"> union_scopes (list[Scope, Scope]): If this Scope is for a Union expression, this will be</span> +</span><span id="Scope-41"><a href="#Scope-41"><span class="linenos"> 41</span></a><span class="sd"> a list of the left and right child scopes.</span> +</span><span id="Scope-42"><a href="#Scope-42"><span class="linenos"> 42</span></a><span class="sd"> """</span> +</span><span id="Scope-43"><a href="#Scope-43"><span class="linenos"> 43</span></a> +</span><span id="Scope-44"><a href="#Scope-44"><span class="linenos"> 44</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span> +</span><span id="Scope-45"><a href="#Scope-45"><span class="linenos"> 45</span></a> <span class="bp">self</span><span class="p">,</span> +</span><span id="Scope-46"><a href="#Scope-46"><span class="linenos"> 46</span></a> <span class="n">expression</span><span class="p">,</span> +</span><span id="Scope-47"><a href="#Scope-47"><span class="linenos"> 47</span></a> <span class="n">sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope-48"><a href="#Scope-48"><span class="linenos"> 48</span></a> <span class="n">outer_column_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope-49"><a href="#Scope-49"><span class="linenos"> 49</span></a> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope-50"><a href="#Scope-50"><span class="linenos"> 50</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">ROOT</span><span class="p">,</span> +</span><span id="Scope-51"><a href="#Scope-51"><span class="linenos"> 51</span></a> <span class="p">):</span> +</span><span id="Scope-52"><a href="#Scope-52"><span class="linenos"> 52</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span> +</span><span id="Scope-53"><a href="#Scope-53"><span class="linenos"> 53</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="n">sources</span> <span class="ow">or</span> <span class="p">{}</span> +</span><span id="Scope-54"><a href="#Scope-54"><span class="linenos"> 54</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">outer_column_list</span> <span class="o">=</span> <span class="n">outer_column_list</span> <span class="ow">or</span> <span class="p">[]</span> +</span><span id="Scope-55"><a href="#Scope-55"><span class="linenos"> 55</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span> +</span><span id="Scope-56"><a href="#Scope-56"><span class="linenos"> 56</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">=</span> <span class="n">scope_type</span> +</span><span id="Scope-57"><a href="#Scope-57"><span class="linenos"> 57</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-58"><a href="#Scope-58"><span class="linenos"> 58</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-59"><a href="#Scope-59"><span class="linenos"> 59</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-60"><a href="#Scope-60"><span class="linenos"> 60</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-61"><a href="#Scope-61"><span class="linenos"> 61</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="Scope-62"><a href="#Scope-62"><span class="linenos"> 62</span></a> +</span><span id="Scope-63"><a href="#Scope-63"><span class="linenos"> 63</span></a> <span class="k">def</span> <span class="nf">clear_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-64"><a href="#Scope-64"><span class="linenos"> 64</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="Scope-65"><a href="#Scope-65"><span class="linenos"> 65</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-66"><a href="#Scope-66"><span class="linenos"> 66</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-67"><a href="#Scope-67"><span class="linenos"> 67</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-68"><a href="#Scope-68"><span class="linenos"> 68</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-69"><a href="#Scope-69"><span class="linenos"> 69</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-70"><a href="#Scope-70"><span class="linenos"> 70</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-71"><a href="#Scope-71"><span class="linenos"> 71</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-72"><a href="#Scope-72"><span class="linenos"> 72</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-73"><a href="#Scope-73"><span class="linenos"> 73</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope-74"><a href="#Scope-74"><span class="linenos"> 74</span></a> +</span><span id="Scope-75"><a href="#Scope-75"><span class="linenos"> 75</span></a> <span class="k">def</span> <span class="nf">branch</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">scope_type</span><span class="p">,</span> <span class="n">chain_sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> +</span><span id="Scope-76"><a href="#Scope-76"><span class="linenos"> 76</span></a><span class="w"> </span><span class="sd">"""Branch from the current scope to a new, inner scope"""</span> +</span><span id="Scope-77"><a href="#Scope-77"><span class="linenos"> 77</span></a> <span class="k">return</span> <span class="n">Scope</span><span class="p">(</span> +</span><span id="Scope-78"><a href="#Scope-78"><span class="linenos"> 78</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> +</span><span id="Scope-79"><a href="#Scope-79"><span class="linenos"> 79</span></a> <span class="n">sources</span><span class="o">=</span><span class="p">{</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">cte_sources</span><span class="p">,</span> <span class="o">**</span><span class="p">(</span><span class="n">chain_sources</span> <span class="ow">or</span> <span class="p">{})},</span> +</span><span id="Scope-80"><a href="#Scope-80"><span class="linenos"> 80</span></a> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> +</span><span id="Scope-81"><a href="#Scope-81"><span class="linenos"> 81</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">scope_type</span><span class="p">,</span> +</span><span id="Scope-82"><a href="#Scope-82"><span class="linenos"> 82</span></a> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> +</span><span id="Scope-83"><a href="#Scope-83"><span class="linenos"> 83</span></a> <span class="p">)</span> +</span><span id="Scope-84"><a href="#Scope-84"><span class="linenos"> 84</span></a> +</span><span id="Scope-85"><a href="#Scope-85"><span class="linenos"> 85</span></a> <span class="k">def</span> <span class="nf">_collect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-86"><a href="#Scope-86"><span class="linenos"> 86</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-87"><a href="#Scope-87"><span class="linenos"> 87</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-88"><a href="#Scope-88"><span class="linenos"> 88</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-89"><a href="#Scope-89"><span class="linenos"> 89</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-90"><a href="#Scope-90"><span class="linenos"> 90</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-91"><a href="#Scope-91"><span class="linenos"> 91</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-92"><a href="#Scope-92"><span class="linenos"> 92</span></a> +</span><span id="Scope-93"><a href="#Scope-93"><span class="linenos"> 93</span></a> <span class="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="Scope-94"><a href="#Scope-94"><span class="linenos"> 94</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">:</span> +</span><span id="Scope-95"><a href="#Scope-95"><span class="linenos"> 95</span></a> <span class="k">continue</span> +</span><span id="Scope-96"><a href="#Scope-96"><span class="linenos"> 96</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Star</span><span class="p">):</span> +</span><span id="Scope-97"><a href="#Scope-97"><span class="linenos"> 97</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-98"><a href="#Scope-98"><span class="linenos"> 98</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Table</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</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">JoinHint</span><span class="p">):</span> +</span><span id="Scope-99"><a href="#Scope-99"><span class="linenos"> 99</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-100"><a href="#Scope-100"><span class="linenos">100</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">JoinHint</span><span class="p">):</span> +</span><span id="Scope-101"><a href="#Scope-101"><span class="linenos">101</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-102"><a href="#Scope-102"><span class="linenos">102</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">UDTF</span><span class="p">):</span> +</span><span id="Scope-103"><a href="#Scope-103"><span class="linenos">103</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-104"><a href="#Scope-104"><span class="linenos">104</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">):</span> +</span><span id="Scope-105"><a href="#Scope-105"><span class="linenos">105</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-106"><a href="#Scope-106"><span class="linenos">106</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)):</span> +</span><span id="Scope-107"><a href="#Scope-107"><span class="linenos">107</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-108"><a href="#Scope-108"><span class="linenos">108</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subqueryable</span><span class="p">):</span> +</span><span id="Scope-109"><a href="#Scope-109"><span class="linenos">109</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="Scope-110"><a href="#Scope-110"><span class="linenos">110</span></a> +</span><span id="Scope-111"><a href="#Scope-111"><span class="linenos">111</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="Scope-112"><a href="#Scope-112"><span class="linenos">112</span></a> +</span><span id="Scope-113"><a href="#Scope-113"><span class="linenos">113</span></a> <span class="k">def</span> <span class="nf">_ensure_collected</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-114"><a href="#Scope-114"><span class="linenos">114</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span><span class="p">:</span> +</span><span id="Scope-115"><a href="#Scope-115"><span class="linenos">115</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collect</span><span class="p">()</span> +</span><span id="Scope-116"><a href="#Scope-116"><span class="linenos">116</span></a> +</span><span id="Scope-117"><a href="#Scope-117"><span class="linenos">117</span></a> <span class="k">def</span> <span class="nf">walk</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope-118"><a href="#Scope-118"><span class="linenos">118</span></a> <span class="k">return</span> <span class="n">walk_in_scope</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">)</span> +</span><span id="Scope-119"><a href="#Scope-119"><span class="linenos">119</span></a> +</span><span id="Scope-120"><a href="#Scope-120"><span class="linenos">120</span></a> <span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope-121"><a href="#Scope-121"><span class="linenos">121</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-122"><a href="#Scope-122"><span class="linenos">122</span></a><span class="sd"> Returns the first node in this scope which matches at least one of the specified types.</span> +</span><span id="Scope-123"><a href="#Scope-123"><span class="linenos">123</span></a> +</span><span id="Scope-124"><a href="#Scope-124"><span class="linenos">124</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="Scope-125"><a href="#Scope-125"><span class="linenos">125</span></a> +</span><span id="Scope-126"><a href="#Scope-126"><span class="linenos">126</span></a><span class="sd"> Args:</span> +</span><span id="Scope-127"><a href="#Scope-127"><span class="linenos">127</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="Scope-128"><a href="#Scope-128"><span class="linenos">128</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="Scope-129"><a href="#Scope-129"><span class="linenos">129</span></a> +</span><span id="Scope-130"><a href="#Scope-130"><span class="linenos">130</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-131"><a href="#Scope-131"><span class="linenos">131</span></a><span class="sd"> exp.Expression: the node which matches the criteria or None if no node matching</span> +</span><span id="Scope-132"><a href="#Scope-132"><span class="linenos">132</span></a><span class="sd"> the criteria was found.</span> +</span><span id="Scope-133"><a href="#Scope-133"><span class="linenos">133</span></a><span class="sd"> """</span> +</span><span id="Scope-134"><a href="#Scope-134"><span class="linenos">134</span></a> <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Scope-135"><a href="#Scope-135"><span class="linenos">135</span></a> +</span><span id="Scope-136"><a href="#Scope-136"><span class="linenos">136</span></a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope-137"><a href="#Scope-137"><span class="linenos">137</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-138"><a href="#Scope-138"><span class="linenos">138</span></a><span class="sd"> Returns a generator object which visits all nodes in this scope and only yields those that</span> +</span><span id="Scope-139"><a href="#Scope-139"><span class="linenos">139</span></a><span class="sd"> match at least one of the specified expression types.</span> +</span><span id="Scope-140"><a href="#Scope-140"><span class="linenos">140</span></a> +</span><span id="Scope-141"><a href="#Scope-141"><span class="linenos">141</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="Scope-142"><a href="#Scope-142"><span class="linenos">142</span></a> +</span><span id="Scope-143"><a href="#Scope-143"><span class="linenos">143</span></a><span class="sd"> Args:</span> +</span><span id="Scope-144"><a href="#Scope-144"><span class="linenos">144</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="Scope-145"><a href="#Scope-145"><span class="linenos">145</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="Scope-146"><a href="#Scope-146"><span class="linenos">146</span></a> +</span><span id="Scope-147"><a href="#Scope-147"><span class="linenos">147</span></a><span class="sd"> Yields:</span> +</span><span id="Scope-148"><a href="#Scope-148"><span class="linenos">148</span></a><span class="sd"> exp.Expression: nodes</span> +</span><span id="Scope-149"><a href="#Scope-149"><span class="linenos">149</span></a><span class="sd"> """</span> +</span><span id="Scope-150"><a href="#Scope-150"><span class="linenos">150</span></a> <span class="k">for</span> <span class="n">expression</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">):</span> +</span><span id="Scope-151"><a href="#Scope-151"><span class="linenos">151</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">expression_types</span><span class="p">):</span> +</span><span id="Scope-152"><a href="#Scope-152"><span class="linenos">152</span></a> <span class="k">yield</span> <span class="n">expression</span> +</span><span id="Scope-153"><a href="#Scope-153"><span class="linenos">153</span></a> +</span><span id="Scope-154"><a href="#Scope-154"><span class="linenos">154</span></a> <span class="k">def</span> <span class="nf">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span> +</span><span id="Scope-155"><a href="#Scope-155"><span class="linenos">155</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-156"><a href="#Scope-156"><span class="linenos">156</span></a><span class="sd"> Replace `old` with `new`.</span> +</span><span id="Scope-157"><a href="#Scope-157"><span class="linenos">157</span></a> +</span><span id="Scope-158"><a href="#Scope-158"><span class="linenos">158</span></a><span class="sd"> This can be used instead of `exp.Expression.replace` to ensure the `Scope` is kept up-to-date.</span> +</span><span id="Scope-159"><a href="#Scope-159"><span class="linenos">159</span></a> +</span><span id="Scope-160"><a href="#Scope-160"><span class="linenos">160</span></a><span class="sd"> Args:</span> +</span><span id="Scope-161"><a href="#Scope-161"><span class="linenos">161</span></a><span class="sd"> old (exp.Expression): old node</span> +</span><span id="Scope-162"><a href="#Scope-162"><span class="linenos">162</span></a><span class="sd"> new (exp.Expression): new node</span> +</span><span id="Scope-163"><a href="#Scope-163"><span class="linenos">163</span></a><span class="sd"> """</span> +</span><span id="Scope-164"><a href="#Scope-164"><span class="linenos">164</span></a> <span class="n">old</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">new</span><span class="p">)</span> +</span><span id="Scope-165"><a href="#Scope-165"><span class="linenos">165</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="Scope-166"><a href="#Scope-166"><span class="linenos">166</span></a> +</span><span id="Scope-167"><a href="#Scope-167"><span class="linenos">167</span></a> <span class="nd">@property</span> +</span><span id="Scope-168"><a href="#Scope-168"><span class="linenos">168</span></a> <span class="k">def</span> <span class="nf">tables</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-169"><a href="#Scope-169"><span class="linenos">169</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-170"><a href="#Scope-170"><span class="linenos">170</span></a><span class="sd"> List of tables in this scope.</span> +</span><span id="Scope-171"><a href="#Scope-171"><span class="linenos">171</span></a> +</span><span id="Scope-172"><a href="#Scope-172"><span class="linenos">172</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-173"><a href="#Scope-173"><span class="linenos">173</span></a><span class="sd"> list[exp.Table]: tables</span> +</span><span id="Scope-174"><a href="#Scope-174"><span class="linenos">174</span></a><span class="sd"> """</span> +</span><span id="Scope-175"><a href="#Scope-175"><span class="linenos">175</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="Scope-176"><a href="#Scope-176"><span class="linenos">176</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> +</span><span id="Scope-177"><a href="#Scope-177"><span class="linenos">177</span></a> +</span><span id="Scope-178"><a href="#Scope-178"><span class="linenos">178</span></a> <span class="nd">@property</span> +</span><span id="Scope-179"><a href="#Scope-179"><span class="linenos">179</span></a> <span class="k">def</span> <span class="nf">ctes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-180"><a href="#Scope-180"><span class="linenos">180</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-181"><a href="#Scope-181"><span class="linenos">181</span></a><span class="sd"> List of CTEs in this scope.</span> +</span><span id="Scope-182"><a href="#Scope-182"><span class="linenos">182</span></a> +</span><span id="Scope-183"><a href="#Scope-183"><span class="linenos">183</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-184"><a href="#Scope-184"><span class="linenos">184</span></a><span class="sd"> list[exp.CTE]: ctes</span> +</span><span id="Scope-185"><a href="#Scope-185"><span class="linenos">185</span></a><span class="sd"> """</span> +</span><span id="Scope-186"><a href="#Scope-186"><span class="linenos">186</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="Scope-187"><a href="#Scope-187"><span class="linenos">187</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> +</span><span id="Scope-188"><a href="#Scope-188"><span class="linenos">188</span></a> +</span><span id="Scope-189"><a href="#Scope-189"><span class="linenos">189</span></a> <span class="nd">@property</span> +</span><span id="Scope-190"><a href="#Scope-190"><span class="linenos">190</span></a> <span class="k">def</span> <span class="nf">derived_tables</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-191"><a href="#Scope-191"><span class="linenos">191</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-192"><a href="#Scope-192"><span class="linenos">192</span></a><span class="sd"> List of derived tables in this scope.</span> +</span><span id="Scope-193"><a href="#Scope-193"><span class="linenos">193</span></a> +</span><span id="Scope-194"><a href="#Scope-194"><span class="linenos">194</span></a><span class="sd"> For example:</span> +</span><span id="Scope-195"><a href="#Scope-195"><span class="linenos">195</span></a><span class="sd"> SELECT * FROM (SELECT ...) <- that's a derived table</span> +</span><span id="Scope-196"><a href="#Scope-196"><span class="linenos">196</span></a> +</span><span id="Scope-197"><a href="#Scope-197"><span class="linenos">197</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-198"><a href="#Scope-198"><span class="linenos">198</span></a><span class="sd"> list[exp.Subquery]: derived tables</span> +</span><span id="Scope-199"><a href="#Scope-199"><span class="linenos">199</span></a><span class="sd"> """</span> +</span><span id="Scope-200"><a href="#Scope-200"><span class="linenos">200</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="Scope-201"><a href="#Scope-201"><span class="linenos">201</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> +</span><span id="Scope-202"><a href="#Scope-202"><span class="linenos">202</span></a> +</span><span id="Scope-203"><a href="#Scope-203"><span class="linenos">203</span></a> <span class="nd">@property</span> +</span><span id="Scope-204"><a href="#Scope-204"><span class="linenos">204</span></a> <span class="k">def</span> <span class="nf">subqueries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-205"><a href="#Scope-205"><span class="linenos">205</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-206"><a href="#Scope-206"><span class="linenos">206</span></a><span class="sd"> List of subqueries in this scope.</span> +</span><span id="Scope-207"><a href="#Scope-207"><span class="linenos">207</span></a> +</span><span id="Scope-208"><a href="#Scope-208"><span class="linenos">208</span></a><span class="sd"> For example:</span> +</span><span id="Scope-209"><a href="#Scope-209"><span class="linenos">209</span></a><span class="sd"> SELECT * FROM x WHERE a IN (SELECT ...) <- that's a subquery</span> +</span><span id="Scope-210"><a href="#Scope-210"><span class="linenos">210</span></a> +</span><span id="Scope-211"><a href="#Scope-211"><span class="linenos">211</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-212"><a href="#Scope-212"><span class="linenos">212</span></a><span class="sd"> list[exp.Subqueryable]: subqueries</span> +</span><span id="Scope-213"><a href="#Scope-213"><span class="linenos">213</span></a><span class="sd"> """</span> +</span><span id="Scope-214"><a href="#Scope-214"><span class="linenos">214</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="Scope-215"><a href="#Scope-215"><span class="linenos">215</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> +</span><span id="Scope-216"><a href="#Scope-216"><span class="linenos">216</span></a> +</span><span id="Scope-217"><a href="#Scope-217"><span class="linenos">217</span></a> <span class="nd">@property</span> +</span><span id="Scope-218"><a href="#Scope-218"><span class="linenos">218</span></a> <span class="k">def</span> <span class="nf">columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-219"><a href="#Scope-219"><span class="linenos">219</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-220"><a href="#Scope-220"><span class="linenos">220</span></a><span class="sd"> List of columns in this scope.</span> +</span><span id="Scope-221"><a href="#Scope-221"><span class="linenos">221</span></a> +</span><span id="Scope-222"><a href="#Scope-222"><span class="linenos">222</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-223"><a href="#Scope-223"><span class="linenos">223</span></a><span class="sd"> list[exp.Column]: Column instances in this scope, plus any</span> +</span><span id="Scope-224"><a href="#Scope-224"><span class="linenos">224</span></a><span class="sd"> Columns that reference this scope from correlated subqueries.</span> +</span><span id="Scope-225"><a href="#Scope-225"><span class="linenos">225</span></a><span class="sd"> """</span> +</span><span id="Scope-226"><a href="#Scope-226"><span class="linenos">226</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="Scope-227"><a href="#Scope-227"><span class="linenos">227</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ensure_collected</span><span class="p">()</span> +</span><span id="Scope-228"><a href="#Scope-228"><span class="linenos">228</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> +</span><span id="Scope-229"><a href="#Scope-229"><span class="linenos">229</span></a> +</span><span id="Scope-230"><a href="#Scope-230"><span class="linenos">230</span></a> <span class="n">external_columns</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="Scope-231"><a href="#Scope-231"><span class="linenos">231</span></a> <span class="n">column</span> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span> +</span><span id="Scope-232"><a href="#Scope-232"><span class="linenos">232</span></a> <span class="p">]</span> +</span><span id="Scope-233"><a href="#Scope-233"><span class="linenos">233</span></a> +</span><span id="Scope-234"><a href="#Scope-234"><span class="linenos">234</span></a> <span class="n">named_selects</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">named_selects</span><span class="p">)</span> +</span><span id="Scope-235"><a href="#Scope-235"><span class="linenos">235</span></a> +</span><span id="Scope-236"><a href="#Scope-236"><span class="linenos">236</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-237"><a href="#Scope-237"><span class="linenos">237</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span> <span class="o">+</span> <span class="n">external_columns</span><span class="p">:</span> +</span><span id="Scope-238"><a href="#Scope-238"><span class="linenos">238</span></a> <span class="n">ancestor</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Qualify</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Order</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">)</span> +</span><span id="Scope-239"><a href="#Scope-239"><span class="linenos">239</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="Scope-240"><a href="#Scope-240"><span class="linenos">240</span></a> <span class="ow">not</span> <span class="n">ancestor</span> +</span><span id="Scope-241"><a href="#Scope-241"><span class="linenos">241</span></a> <span class="c1"># Window functions can have an ORDER BY clause</span> +</span><span id="Scope-242"><a href="#Scope-242"><span class="linenos">242</span></a> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ancestor</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">Select</span><span class="p">)</span> +</span><span id="Scope-243"><a href="#Scope-243"><span class="linenos">243</span></a> <span class="ow">or</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> +</span><span id="Scope-244"><a href="#Scope-244"><span class="linenos">244</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">column</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">named_selects</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ancestor</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Hint</span><span class="p">))</span> +</span><span id="Scope-245"><a href="#Scope-245"><span class="linenos">245</span></a> <span class="p">):</span> +</span><span id="Scope-246"><a href="#Scope-246"><span class="linenos">246</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> +</span><span id="Scope-247"><a href="#Scope-247"><span class="linenos">247</span></a> +</span><span id="Scope-248"><a href="#Scope-248"><span class="linenos">248</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> +</span><span id="Scope-249"><a href="#Scope-249"><span class="linenos">249</span></a> +</span><span id="Scope-250"><a href="#Scope-250"><span class="linenos">250</span></a> <span class="nd">@property</span> +</span><span id="Scope-251"><a href="#Scope-251"><span class="linenos">251</span></a> <span class="k">def</span> <span class="nf">selected_sources</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-252"><a href="#Scope-252"><span class="linenos">252</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-253"><a href="#Scope-253"><span class="linenos">253</span></a><span class="sd"> Mapping of nodes and sources that are actually selected from in this scope.</span> +</span><span id="Scope-254"><a href="#Scope-254"><span class="linenos">254</span></a> +</span><span id="Scope-255"><a href="#Scope-255"><span class="linenos">255</span></a><span class="sd"> That is, all tables in a schema are selectable at any point. But a</span> +</span><span id="Scope-256"><a href="#Scope-256"><span class="linenos">256</span></a><span class="sd"> table only becomes a selected source if it's included in a FROM or JOIN clause.</span> +</span><span id="Scope-257"><a href="#Scope-257"><span class="linenos">257</span></a> +</span><span id="Scope-258"><a href="#Scope-258"><span class="linenos">258</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-259"><a href="#Scope-259"><span class="linenos">259</span></a><span class="sd"> dict[str, (exp.Table|exp.Select, exp.Table|Scope)]: selected sources and nodes</span> +</span><span id="Scope-260"><a href="#Scope-260"><span class="linenos">260</span></a><span class="sd"> """</span> +</span><span id="Scope-261"><a href="#Scope-261"><span class="linenos">261</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="Scope-262"><a href="#Scope-262"><span class="linenos">262</span></a> <span class="n">referenced_names</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope-263"><a href="#Scope-263"><span class="linenos">263</span></a> +</span><span id="Scope-264"><a href="#Scope-264"><span class="linenos">264</span></a> <span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tables</span><span class="p">:</span> +</span><span id="Scope-265"><a href="#Scope-265"><span class="linenos">265</span></a> <span class="n">referenced_names</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">table</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">table</span><span class="p">))</span> +</span><span id="Scope-266"><a href="#Scope-266"><span class="linenos">266</span></a> <span class="k">for</span> <span class="n">derived_table</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">derived_tables</span><span class="p">:</span> +</span><span id="Scope-267"><a href="#Scope-267"><span class="linenos">267</span></a> <span class="n">referenced_names</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">derived_table</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">derived_table</span><span class="o">.</span><span class="n">unnest</span><span class="p">()))</span> +</span><span id="Scope-268"><a href="#Scope-268"><span class="linenos">268</span></a> +</span><span id="Scope-269"><a href="#Scope-269"><span class="linenos">269</span></a> <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="Scope-270"><a href="#Scope-270"><span class="linenos">270</span></a> +</span><span id="Scope-271"><a href="#Scope-271"><span class="linenos">271</span></a> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">referenced_names</span><span class="p">:</span> +</span><span id="Scope-272"><a href="#Scope-272"><span class="linenos">272</span></a> <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span> +</span><span id="Scope-273"><a href="#Scope-273"><span class="linenos">273</span></a> <span class="n">result</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">])</span> +</span><span id="Scope-274"><a href="#Scope-274"><span class="linenos">274</span></a> +</span><span id="Scope-275"><a href="#Scope-275"><span class="linenos">275</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="o">=</span> <span class="n">result</span> +</span><span id="Scope-276"><a href="#Scope-276"><span class="linenos">276</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> +</span><span id="Scope-277"><a href="#Scope-277"><span class="linenos">277</span></a> +</span><span id="Scope-278"><a href="#Scope-278"><span class="linenos">278</span></a> <span class="nd">@property</span> +</span><span id="Scope-279"><a href="#Scope-279"><span class="linenos">279</span></a> <span class="k">def</span> <span class="nf">cte_sources</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-280"><a href="#Scope-280"><span class="linenos">280</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-281"><a href="#Scope-281"><span class="linenos">281</span></a><span class="sd"> Sources that are CTEs.</span> +</span><span id="Scope-282"><a href="#Scope-282"><span class="linenos">282</span></a> +</span><span id="Scope-283"><a href="#Scope-283"><span class="linenos">283</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-284"><a href="#Scope-284"><span class="linenos">284</span></a><span class="sd"> dict[str, Scope]: Mapping of source alias to Scope</span> +</span><span id="Scope-285"><a href="#Scope-285"><span class="linenos">285</span></a><span class="sd"> """</span> +</span><span id="Scope-286"><a href="#Scope-286"><span class="linenos">286</span></a> <span class="k">return</span> <span class="p">{</span> +</span><span id="Scope-287"><a href="#Scope-287"><span class="linenos">287</span></a> <span class="n">alias</span><span class="p">:</span> <span class="n">scope</span> +</span><span id="Scope-288"><a href="#Scope-288"><span class="linenos">288</span></a> <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> +</span><span id="Scope-289"><a href="#Scope-289"><span class="linenos">289</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="n">Scope</span><span class="p">)</span> <span class="ow">and</span> <span class="n">scope</span><span class="o">.</span><span class="n">is_cte</span> +</span><span id="Scope-290"><a href="#Scope-290"><span class="linenos">290</span></a> <span class="p">}</span> +</span><span id="Scope-291"><a href="#Scope-291"><span class="linenos">291</span></a> +</span><span id="Scope-292"><a href="#Scope-292"><span class="linenos">292</span></a> <span class="nd">@property</span> +</span><span id="Scope-293"><a href="#Scope-293"><span class="linenos">293</span></a> <span class="k">def</span> <span class="nf">selects</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-294"><a href="#Scope-294"><span class="linenos">294</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-295"><a href="#Scope-295"><span class="linenos">295</span></a><span class="sd"> Select expressions of this scope.</span> +</span><span id="Scope-296"><a href="#Scope-296"><span class="linenos">296</span></a> +</span><span id="Scope-297"><a href="#Scope-297"><span class="linenos">297</span></a><span class="sd"> For example, for the following expression:</span> +</span><span id="Scope-298"><a href="#Scope-298"><span class="linenos">298</span></a><span class="sd"> SELECT 1 as a, 2 as b FROM x</span> +</span><span id="Scope-299"><a href="#Scope-299"><span class="linenos">299</span></a> +</span><span id="Scope-300"><a href="#Scope-300"><span class="linenos">300</span></a><span class="sd"> The outputs are the "1 as a" and "2 as b" expressions.</span> +</span><span id="Scope-301"><a href="#Scope-301"><span class="linenos">301</span></a> +</span><span id="Scope-302"><a href="#Scope-302"><span class="linenos">302</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-303"><a href="#Scope-303"><span class="linenos">303</span></a><span class="sd"> list[exp.Expression]: expressions</span> +</span><span id="Scope-304"><a href="#Scope-304"><span class="linenos">304</span></a><span class="sd"> """</span> +</span><span id="Scope-305"><a href="#Scope-305"><span class="linenos">305</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span> +</span><span id="Scope-306"><a href="#Scope-306"><span class="linenos">306</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</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">selects</span> +</span><span id="Scope-307"><a href="#Scope-307"><span class="linenos">307</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">selects</span> +</span><span id="Scope-308"><a href="#Scope-308"><span class="linenos">308</span></a> +</span><span id="Scope-309"><a href="#Scope-309"><span class="linenos">309</span></a> <span class="nd">@property</span> +</span><span id="Scope-310"><a href="#Scope-310"><span class="linenos">310</span></a> <span class="k">def</span> <span class="nf">external_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-311"><a href="#Scope-311"><span class="linenos">311</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-312"><a href="#Scope-312"><span class="linenos">312</span></a><span class="sd"> Columns that appear to reference sources in outer scopes.</span> +</span><span id="Scope-313"><a href="#Scope-313"><span class="linenos">313</span></a> +</span><span id="Scope-314"><a href="#Scope-314"><span class="linenos">314</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-315"><a href="#Scope-315"><span class="linenos">315</span></a><span class="sd"> list[exp.Column]: Column instances that don't reference</span> +</span><span id="Scope-316"><a href="#Scope-316"><span class="linenos">316</span></a><span class="sd"> sources in the current scope.</span> +</span><span id="Scope-317"><a href="#Scope-317"><span class="linenos">317</span></a><span class="sd"> """</span> +</span><span id="Scope-318"><a href="#Scope-318"><span class="linenos">318</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="Scope-319"><a href="#Scope-319"><span class="linenos">319</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="o">=</span> <span class="p">[</span> +</span><span id="Scope-320"><a href="#Scope-320"><span class="linenos">320</span></a> <span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_sources</span> +</span><span id="Scope-321"><a href="#Scope-321"><span class="linenos">321</span></a> <span class="p">]</span> +</span><span id="Scope-322"><a href="#Scope-322"><span class="linenos">322</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> +</span><span id="Scope-323"><a href="#Scope-323"><span class="linenos">323</span></a> +</span><span id="Scope-324"><a href="#Scope-324"><span class="linenos">324</span></a> <span class="nd">@property</span> +</span><span id="Scope-325"><a href="#Scope-325"><span class="linenos">325</span></a> <span class="k">def</span> <span class="nf">unqualified_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-326"><a href="#Scope-326"><span class="linenos">326</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-327"><a href="#Scope-327"><span class="linenos">327</span></a><span class="sd"> Unqualified columns in the current scope.</span> +</span><span id="Scope-328"><a href="#Scope-328"><span class="linenos">328</span></a> +</span><span id="Scope-329"><a href="#Scope-329"><span class="linenos">329</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-330"><a href="#Scope-330"><span class="linenos">330</span></a><span class="sd"> list[exp.Column]: Unqualified columns</span> +</span><span id="Scope-331"><a href="#Scope-331"><span class="linenos">331</span></a><span class="sd"> """</span> +</span><span id="Scope-332"><a href="#Scope-332"><span class="linenos">332</span></a> <span class="k">return</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">table</span><span class="p">]</span> +</span><span id="Scope-333"><a href="#Scope-333"><span class="linenos">333</span></a> +</span><span id="Scope-334"><a href="#Scope-334"><span class="linenos">334</span></a> <span class="nd">@property</span> +</span><span id="Scope-335"><a href="#Scope-335"><span class="linenos">335</span></a> <span class="k">def</span> <span class="nf">join_hints</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-336"><a href="#Scope-336"><span class="linenos">336</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-337"><a href="#Scope-337"><span class="linenos">337</span></a><span class="sd"> Hints that exist in the scope that reference tables</span> +</span><span id="Scope-338"><a href="#Scope-338"><span class="linenos">338</span></a> +</span><span id="Scope-339"><a href="#Scope-339"><span class="linenos">339</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-340"><a href="#Scope-340"><span class="linenos">340</span></a><span class="sd"> list[exp.JoinHint]: Join hints that are referenced within the scope</span> +</span><span id="Scope-341"><a href="#Scope-341"><span class="linenos">341</span></a><span class="sd"> """</span> +</span><span id="Scope-342"><a href="#Scope-342"><span class="linenos">342</span></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> +</span><span id="Scope-343"><a href="#Scope-343"><span class="linenos">343</span></a> <span class="k">return</span> <span class="p">[]</span> +</span><span id="Scope-344"><a href="#Scope-344"><span class="linenos">344</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> +</span><span id="Scope-345"><a href="#Scope-345"><span class="linenos">345</span></a> +</span><span id="Scope-346"><a href="#Scope-346"><span class="linenos">346</span></a> <span class="k">def</span> <span class="nf">source_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_name</span><span class="p">):</span> +</span><span id="Scope-347"><a href="#Scope-347"><span class="linenos">347</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-348"><a href="#Scope-348"><span class="linenos">348</span></a><span class="sd"> Get all columns in the current scope for a particular source.</span> +</span><span id="Scope-349"><a href="#Scope-349"><span class="linenos">349</span></a> +</span><span id="Scope-350"><a href="#Scope-350"><span class="linenos">350</span></a><span class="sd"> Args:</span> +</span><span id="Scope-351"><a href="#Scope-351"><span class="linenos">351</span></a><span class="sd"> source_name (str): Name of the source</span> +</span><span id="Scope-352"><a href="#Scope-352"><span class="linenos">352</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-353"><a href="#Scope-353"><span class="linenos">353</span></a><span class="sd"> list[exp.Column]: Column instances that reference `source_name`</span> +</span><span id="Scope-354"><a href="#Scope-354"><span class="linenos">354</span></a><span class="sd"> """</span> +</span><span id="Scope-355"><a href="#Scope-355"><span class="linenos">355</span></a> <span class="k">return</span> <span class="p">[</span><span class="n">column</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">source_name</span><span class="p">]</span> +</span><span id="Scope-356"><a href="#Scope-356"><span class="linenos">356</span></a> +</span><span id="Scope-357"><a href="#Scope-357"><span class="linenos">357</span></a> <span class="nd">@property</span> +</span><span id="Scope-358"><a href="#Scope-358"><span class="linenos">358</span></a> <span class="k">def</span> <span class="nf">is_subquery</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-359"><a href="#Scope-359"><span class="linenos">359</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a subquery"""</span> +</span><span id="Scope-360"><a href="#Scope-360"><span class="linenos">360</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">SUBQUERY</span> +</span><span id="Scope-361"><a href="#Scope-361"><span class="linenos">361</span></a> +</span><span id="Scope-362"><a href="#Scope-362"><span class="linenos">362</span></a> <span class="nd">@property</span> +</span><span id="Scope-363"><a href="#Scope-363"><span class="linenos">363</span></a> <span class="k">def</span> <span class="nf">is_derived_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-364"><a href="#Scope-364"><span class="linenos">364</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a derived table"""</span> +</span><span id="Scope-365"><a href="#Scope-365"><span class="linenos">365</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">DERIVED_TABLE</span> +</span><span id="Scope-366"><a href="#Scope-366"><span class="linenos">366</span></a> +</span><span id="Scope-367"><a href="#Scope-367"><span class="linenos">367</span></a> <span class="nd">@property</span> +</span><span id="Scope-368"><a href="#Scope-368"><span class="linenos">368</span></a> <span class="k">def</span> <span class="nf">is_union</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-369"><a href="#Scope-369"><span class="linenos">369</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a union"""</span> +</span><span id="Scope-370"><a href="#Scope-370"><span class="linenos">370</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">UNION</span> +</span><span id="Scope-371"><a href="#Scope-371"><span class="linenos">371</span></a> +</span><span id="Scope-372"><a href="#Scope-372"><span class="linenos">372</span></a> <span class="nd">@property</span> +</span><span id="Scope-373"><a href="#Scope-373"><span class="linenos">373</span></a> <span class="k">def</span> <span class="nf">is_cte</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-374"><a href="#Scope-374"><span class="linenos">374</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a common table expression"""</span> +</span><span id="Scope-375"><a href="#Scope-375"><span class="linenos">375</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">CTE</span> +</span><span id="Scope-376"><a href="#Scope-376"><span class="linenos">376</span></a> +</span><span id="Scope-377"><a href="#Scope-377"><span class="linenos">377</span></a> <span class="nd">@property</span> +</span><span id="Scope-378"><a href="#Scope-378"><span class="linenos">378</span></a> <span class="k">def</span> <span class="nf">is_root</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-379"><a href="#Scope-379"><span class="linenos">379</span></a><span class="w"> </span><span class="sd">"""Determine if this is the root scope"""</span> +</span><span id="Scope-380"><a href="#Scope-380"><span class="linenos">380</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">ROOT</span> +</span><span id="Scope-381"><a href="#Scope-381"><span class="linenos">381</span></a> +</span><span id="Scope-382"><a href="#Scope-382"><span class="linenos">382</span></a> <span class="nd">@property</span> +</span><span id="Scope-383"><a href="#Scope-383"><span class="linenos">383</span></a> <span class="k">def</span> <span class="nf">is_udtf</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-384"><a href="#Scope-384"><span class="linenos">384</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a UDTF (User Defined Table Function)"""</span> +</span><span id="Scope-385"><a href="#Scope-385"><span class="linenos">385</span></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">UDTF</span> +</span><span id="Scope-386"><a href="#Scope-386"><span class="linenos">386</span></a> +</span><span id="Scope-387"><a href="#Scope-387"><span class="linenos">387</span></a> <span class="nd">@property</span> +</span><span id="Scope-388"><a href="#Scope-388"><span class="linenos">388</span></a> <span class="k">def</span> <span class="nf">is_correlated_subquery</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-389"><a href="#Scope-389"><span class="linenos">389</span></a><span class="w"> </span><span class="sd">"""Determine if this scope is a correlated subquery"""</span> +</span><span id="Scope-390"><a href="#Scope-390"><span class="linenos">390</span></a> <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">is_subquery</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_columns</span><span class="p">)</span> +</span><span id="Scope-391"><a href="#Scope-391"><span class="linenos">391</span></a> +</span><span id="Scope-392"><a href="#Scope-392"><span class="linenos">392</span></a> <span class="k">def</span> <span class="nf">rename_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_name</span><span class="p">,</span> <span class="n">new_name</span><span class="p">):</span> +</span><span id="Scope-393"><a href="#Scope-393"><span class="linenos">393</span></a><span class="w"> </span><span class="sd">"""Rename a source in this scope"""</span> +</span><span id="Scope-394"><a href="#Scope-394"><span class="linenos">394</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">old_name</span> <span class="ow">or</span> <span class="s2">""</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="Scope-395"><a href="#Scope-395"><span class="linenos">395</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">new_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">columns</span> +</span><span id="Scope-396"><a href="#Scope-396"><span class="linenos">396</span></a> +</span><span id="Scope-397"><a href="#Scope-397"><span class="linenos">397</span></a> <span class="k">def</span> <span class="nf">add_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span><span class="p">):</span> +</span><span id="Scope-398"><a href="#Scope-398"><span class="linenos">398</span></a><span class="w"> </span><span class="sd">"""Add a source to this scope"""</span> +</span><span id="Scope-399"><a href="#Scope-399"><span class="linenos">399</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span> +</span><span id="Scope-400"><a href="#Scope-400"><span class="linenos">400</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="Scope-401"><a href="#Scope-401"><span class="linenos">401</span></a> +</span><span id="Scope-402"><a href="#Scope-402"><span class="linenos">402</span></a> <span class="k">def</span> <span class="nf">remove_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> +</span><span id="Scope-403"><a href="#Scope-403"><span class="linenos">403</span></a><span class="w"> </span><span class="sd">"""Remove a source from this scope"""</span> +</span><span id="Scope-404"><a href="#Scope-404"><span class="linenos">404</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Scope-405"><a href="#Scope-405"><span class="linenos">405</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span><span id="Scope-406"><a href="#Scope-406"><span class="linenos">406</span></a> +</span><span id="Scope-407"><a href="#Scope-407"><span class="linenos">407</span></a> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-408"><a href="#Scope-408"><span class="linenos">408</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"Scope<</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span><span class="si">}</span><span class="s2">>"</span> +</span><span id="Scope-409"><a href="#Scope-409"><span class="linenos">409</span></a> +</span><span id="Scope-410"><a href="#Scope-410"><span class="linenos">410</span></a> <span class="k">def</span> <span class="nf">traverse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-411"><a href="#Scope-411"><span class="linenos">411</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-412"><a href="#Scope-412"><span class="linenos">412</span></a><span class="sd"> Traverse the scope tree from this node.</span> +</span><span id="Scope-413"><a href="#Scope-413"><span class="linenos">413</span></a> +</span><span id="Scope-414"><a href="#Scope-414"><span class="linenos">414</span></a><span class="sd"> Yields:</span> +</span><span id="Scope-415"><a href="#Scope-415"><span class="linenos">415</span></a><span class="sd"> Scope: scope instances in depth-first-search post-order</span> +</span><span id="Scope-416"><a href="#Scope-416"><span class="linenos">416</span></a><span class="sd"> """</span> +</span><span id="Scope-417"><a href="#Scope-417"><span class="linenos">417</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> +</span><span id="Scope-418"><a href="#Scope-418"><span class="linenos">418</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> +</span><span id="Scope-419"><a href="#Scope-419"><span class="linenos">419</span></a> <span class="p">):</span> +</span><span id="Scope-420"><a href="#Scope-420"><span class="linenos">420</span></a> <span class="k">yield from</span> <span class="n">child_scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">()</span> +</span><span id="Scope-421"><a href="#Scope-421"><span class="linenos">421</span></a> <span class="k">yield</span> <span class="bp">self</span> +</span><span id="Scope-422"><a href="#Scope-422"><span class="linenos">422</span></a> +</span><span id="Scope-423"><a href="#Scope-423"><span class="linenos">423</span></a> <span class="k">def</span> <span class="nf">ref_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope-424"><a href="#Scope-424"><span class="linenos">424</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope-425"><a href="#Scope-425"><span class="linenos">425</span></a><span class="sd"> Count the number of times each scope in this tree is referenced.</span> +</span><span id="Scope-426"><a href="#Scope-426"><span class="linenos">426</span></a> +</span><span id="Scope-427"><a href="#Scope-427"><span class="linenos">427</span></a><span class="sd"> Returns:</span> +</span><span id="Scope-428"><a href="#Scope-428"><span class="linenos">428</span></a><span class="sd"> dict[int, int]: Mapping of Scope instance ID to reference count</span> +</span><span id="Scope-429"><a href="#Scope-429"><span class="linenos">429</span></a><span class="sd"> """</span> +</span><span id="Scope-430"><a href="#Scope-430"><span class="linenos">430</span></a> <span class="n">scope_ref_count</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span> +</span><span id="Scope-431"><a href="#Scope-431"><span class="linenos">431</span></a> +</span><span id="Scope-432"><a href="#Scope-432"><span class="linenos">432</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="Scope-433"><a href="#Scope-433"><span class="linenos">433</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="Scope-434"><a href="#Scope-434"><span class="linenos">434</span></a> <span class="n">scope_ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o">+=</span> <span class="mi">1</span> +</span><span id="Scope-435"><a href="#Scope-435"><span class="linenos">435</span></a> +</span><span id="Scope-436"><a href="#Scope-436"><span class="linenos">436</span></a> <span class="k">return</span> <span class="n">scope_ref_count</span> +</span></pre></div> + + + <div class="docstring"><p>Selection scope.</p> + +<h6 id="attributes">Attributes:</h6> + +<ul> +<li><strong>expression (exp.Select|exp.Union):</strong> Root expression of this scope</li> +<li><strong>sources (dict[str, exp.Table|Scope]):</strong> Mapping of source name to either +a Table expression or another Scope instance. For example: + SELECT * FROM x {"x": Table(this="x")} + SELECT * FROM x AS y {"y": Table(this="x")} + SELECT * FROM (SELECT ...) AS y {"y": Scope(...)}</li> +<li><strong>outer_column_list (list[str]):</strong> If this is a derived table or CTE, and the outer query +defines a column list of it's alias of this scope, this is that list of columns. +For example: + SELECT * FROM (SELECT ...) AS y(col1, col2) +The inner query would have <code>["col1", "col2"]</code> for its <code>outer_column_list</code></li> +<li><strong>parent (Scope):</strong> Parent scope</li> +<li><strong>scope_type (ScopeType):</strong> Type of this scope, relative to it's parent</li> +<li><strong>subquery_scopes (list[Scope]):</strong> List of all child scopes for subqueries</li> +<li>cte_scopes = (list[Scope]) List of all child scopes for CTEs</li> +<li>derived_table_scopes = (list[Scope]) List of all child scopes for derived_tables</li> +<li><strong>union_scopes (list[Scope, Scope]):</strong> If this Scope is for a Union expression, this will be +a list of the left and right child scopes.</li> +</ul> +</div> + + + <div id="Scope.__init__" class="classattr"> + <input id="Scope.__init__-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="name">Scope</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span>,</span><span class="param"> <span class="n">sources</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">outer_column_list</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span>,</span><span class="param"> <span class="n">scope_type</span><span class="o">=<</span><span class="n"><a href="#ScopeType.ROOT">ScopeType.ROOT</a></span><span class="p">:</span> <span class="mi">1</span><span class="o">></span></span>)</span> + + <label class="view-source-button" for="Scope.__init__-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.__init__"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.__init__-44"><a href="#Scope.__init__-44"><span class="linenos">44</span></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span> +</span><span id="Scope.__init__-45"><a href="#Scope.__init__-45"><span class="linenos">45</span></a> <span class="bp">self</span><span class="p">,</span> +</span><span id="Scope.__init__-46"><a href="#Scope.__init__-46"><span class="linenos">46</span></a> <span class="n">expression</span><span class="p">,</span> +</span><span id="Scope.__init__-47"><a href="#Scope.__init__-47"><span class="linenos">47</span></a> <span class="n">sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope.__init__-48"><a href="#Scope.__init__-48"><span class="linenos">48</span></a> <span class="n">outer_column_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope.__init__-49"><a href="#Scope.__init__-49"><span class="linenos">49</span></a> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> +</span><span id="Scope.__init__-50"><a href="#Scope.__init__-50"><span class="linenos">50</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">ScopeType</span><span class="o">.</span><span class="n">ROOT</span><span class="p">,</span> +</span><span id="Scope.__init__-51"><a href="#Scope.__init__-51"><span class="linenos">51</span></a> <span class="p">):</span> +</span><span id="Scope.__init__-52"><a href="#Scope.__init__-52"><span class="linenos">52</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span> +</span><span id="Scope.__init__-53"><a href="#Scope.__init__-53"><span class="linenos">53</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="n">sources</span> <span class="ow">or</span> <span class="p">{}</span> +</span><span id="Scope.__init__-54"><a href="#Scope.__init__-54"><span class="linenos">54</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">outer_column_list</span> <span class="o">=</span> <span class="n">outer_column_list</span> <span class="ow">or</span> <span class="p">[]</span> +</span><span id="Scope.__init__-55"><a href="#Scope.__init__-55"><span class="linenos">55</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span> +</span><span id="Scope.__init__-56"><a href="#Scope.__init__-56"><span class="linenos">56</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">=</span> <span class="n">scope_type</span> +</span><span id="Scope.__init__-57"><a href="#Scope.__init__-57"><span class="linenos">57</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope.__init__-58"><a href="#Scope.__init__-58"><span class="linenos">58</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope.__init__-59"><a href="#Scope.__init__-59"><span class="linenos">59</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope.__init__-60"><a href="#Scope.__init__-60"><span class="linenos">60</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="Scope.__init__-61"><a href="#Scope.__init__-61"><span class="linenos">61</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span></pre></div> + + + + + </div> + <div id="Scope.clear_cache" class="classattr"> + <input id="Scope.clear_cache-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">clear_cache</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.clear_cache-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.clear_cache"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.clear_cache-63"><a href="#Scope.clear_cache-63"><span class="linenos">63</span></a> <span class="k">def</span> <span class="nf">clear_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope.clear_cache-64"><a href="#Scope.clear_cache-64"><span class="linenos">64</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_collected</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="Scope.clear_cache-65"><a href="#Scope.clear_cache-65"><span class="linenos">65</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-66"><a href="#Scope.clear_cache-66"><span class="linenos">66</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_derived_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-67"><a href="#Scope.clear_cache-67"><span class="linenos">67</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_tables</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-68"><a href="#Scope.clear_cache-68"><span class="linenos">68</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_ctes</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-69"><a href="#Scope.clear_cache-69"><span class="linenos">69</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_subqueries</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-70"><a href="#Scope.clear_cache-70"><span class="linenos">70</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_selected_sources</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-71"><a href="#Scope.clear_cache-71"><span class="linenos">71</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-72"><a href="#Scope.clear_cache-72"><span class="linenos">72</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_external_columns</span> <span class="o">=</span> <span class="kc">None</span> +</span><span id="Scope.clear_cache-73"><a href="#Scope.clear_cache-73"><span class="linenos">73</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">_join_hints</span> <span class="o">=</span> <span class="kc">None</span> +</span></pre></div> + + + + + </div> + <div id="Scope.branch" class="classattr"> + <input id="Scope.branch-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">branch</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">scope_type</span>, </span><span class="param"><span class="n">chain_sources</span><span class="o">=</span><span class="kc">None</span>, </span><span class="param"><span class="o">**</span><span class="n">kwargs</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.branch-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.branch"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.branch-75"><a href="#Scope.branch-75"><span class="linenos">75</span></a> <span class="k">def</span> <span class="nf">branch</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">scope_type</span><span class="p">,</span> <span class="n">chain_sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> +</span><span id="Scope.branch-76"><a href="#Scope.branch-76"><span class="linenos">76</span></a><span class="w"> </span><span class="sd">"""Branch from the current scope to a new, inner scope"""</span> +</span><span id="Scope.branch-77"><a href="#Scope.branch-77"><span class="linenos">77</span></a> <span class="k">return</span> <span class="n">Scope</span><span class="p">(</span> +</span><span id="Scope.branch-78"><a href="#Scope.branch-78"><span class="linenos">78</span></a> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">unnest</span><span class="p">(),</span> +</span><span id="Scope.branch-79"><a href="#Scope.branch-79"><span class="linenos">79</span></a> <span class="n">sources</span><span class="o">=</span><span class="p">{</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">cte_sources</span><span class="p">,</span> <span class="o">**</span><span class="p">(</span><span class="n">chain_sources</span> <span class="ow">or</span> <span class="p">{})},</span> +</span><span id="Scope.branch-80"><a href="#Scope.branch-80"><span class="linenos">80</span></a> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> +</span><span id="Scope.branch-81"><a href="#Scope.branch-81"><span class="linenos">81</span></a> <span class="n">scope_type</span><span class="o">=</span><span class="n">scope_type</span><span class="p">,</span> +</span><span id="Scope.branch-82"><a href="#Scope.branch-82"><span class="linenos">82</span></a> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> +</span><span id="Scope.branch-83"><a href="#Scope.branch-83"><span class="linenos">83</span></a> <span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>Branch from the current scope to a new, inner scope</p> +</div> + + + </div> + <div id="Scope.walk" class="classattr"> + <input id="Scope.walk-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">walk</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">bfs</span><span class="o">=</span><span class="kc">True</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.walk-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.walk"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.walk-117"><a href="#Scope.walk-117"><span class="linenos">117</span></a> <span class="k">def</span> <span class="nf">walk</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope.walk-118"><a href="#Scope.walk-118"><span class="linenos">118</span></a> <span class="k">return</span> <span class="n">walk_in_scope</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">)</span> +</span></pre></div> + + + + + </div> + <div id="Scope.find" class="classattr"> + <input id="Scope.find-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">find</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="o">*</span><span class="n">expression_types</span>, </span><span class="param"><span class="n">bfs</span><span class="o">=</span><span class="kc">True</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.find-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.find"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.find-120"><a href="#Scope.find-120"><span class="linenos">120</span></a> <span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope.find-121"><a href="#Scope.find-121"><span class="linenos">121</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.find-122"><a href="#Scope.find-122"><span class="linenos">122</span></a><span class="sd"> Returns the first node in this scope which matches at least one of the specified types.</span> +</span><span id="Scope.find-123"><a href="#Scope.find-123"><span class="linenos">123</span></a> +</span><span id="Scope.find-124"><a href="#Scope.find-124"><span class="linenos">124</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="Scope.find-125"><a href="#Scope.find-125"><span class="linenos">125</span></a> +</span><span id="Scope.find-126"><a href="#Scope.find-126"><span class="linenos">126</span></a><span class="sd"> Args:</span> +</span><span id="Scope.find-127"><a href="#Scope.find-127"><span class="linenos">127</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="Scope.find-128"><a href="#Scope.find-128"><span class="linenos">128</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="Scope.find-129"><a href="#Scope.find-129"><span class="linenos">129</span></a> +</span><span id="Scope.find-130"><a href="#Scope.find-130"><span class="linenos">130</span></a><span class="sd"> Returns:</span> +</span><span id="Scope.find-131"><a href="#Scope.find-131"><span class="linenos">131</span></a><span class="sd"> exp.Expression: the node which matches the criteria or None if no node matching</span> +</span><span id="Scope.find-132"><a href="#Scope.find-132"><span class="linenos">132</span></a><span class="sd"> the criteria was found.</span> +</span><span id="Scope.find-133"><a href="#Scope.find-133"><span class="linenos">133</span></a><span class="sd"> """</span> +</span><span id="Scope.find-134"><a href="#Scope.find-134"><span class="linenos">134</span></a> <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span> +</span></pre></div> + + + <div class="docstring"><p>Returns the first node in this scope which matches at least one of the specified types.</p> + +<p>This does NOT traverse into subscopes.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression_types (type):</strong> the expression type(s) to match.</li> +<li><strong>bfs (bool):</strong> True to use breadth-first search, False to use depth-first.</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>exp.Expression: the node which matches the criteria or None if no node matching + the criteria was found.</p> +</blockquote> +</div> + + + </div> + <div id="Scope.find_all" class="classattr"> + <input id="Scope.find_all-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">find_all</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="o">*</span><span class="n">expression_types</span>, </span><span class="param"><span class="n">bfs</span><span class="o">=</span><span class="kc">True</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.find_all-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.find_all"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.find_all-136"><a href="#Scope.find_all-136"><span class="linenos">136</span></a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">expression_types</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="Scope.find_all-137"><a href="#Scope.find_all-137"><span class="linenos">137</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.find_all-138"><a href="#Scope.find_all-138"><span class="linenos">138</span></a><span class="sd"> Returns a generator object which visits all nodes in this scope and only yields those that</span> +</span><span id="Scope.find_all-139"><a href="#Scope.find_all-139"><span class="linenos">139</span></a><span class="sd"> match at least one of the specified expression types.</span> +</span><span id="Scope.find_all-140"><a href="#Scope.find_all-140"><span class="linenos">140</span></a> +</span><span id="Scope.find_all-141"><a href="#Scope.find_all-141"><span class="linenos">141</span></a><span class="sd"> This does NOT traverse into subscopes.</span> +</span><span id="Scope.find_all-142"><a href="#Scope.find_all-142"><span class="linenos">142</span></a> +</span><span id="Scope.find_all-143"><a href="#Scope.find_all-143"><span class="linenos">143</span></a><span class="sd"> Args:</span> +</span><span id="Scope.find_all-144"><a href="#Scope.find_all-144"><span class="linenos">144</span></a><span class="sd"> expression_types (type): the expression type(s) to match.</span> +</span><span id="Scope.find_all-145"><a href="#Scope.find_all-145"><span class="linenos">145</span></a><span class="sd"> bfs (bool): True to use breadth-first search, False to use depth-first.</span> +</span><span id="Scope.find_all-146"><a href="#Scope.find_all-146"><span class="linenos">146</span></a> +</span><span id="Scope.find_all-147"><a href="#Scope.find_all-147"><span class="linenos">147</span></a><span class="sd"> Yields:</span> +</span><span id="Scope.find_all-148"><a href="#Scope.find_all-148"><span class="linenos">148</span></a><span class="sd"> exp.Expression: nodes</span> +</span><span id="Scope.find_all-149"><a href="#Scope.find_all-149"><span class="linenos">149</span></a><span class="sd"> """</span> +</span><span id="Scope.find_all-150"><a href="#Scope.find_all-150"><span class="linenos">150</span></a> <span class="k">for</span> <span class="n">expression</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">):</span> +</span><span id="Scope.find_all-151"><a href="#Scope.find_all-151"><span class="linenos">151</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">expression_types</span><span class="p">):</span> +</span><span id="Scope.find_all-152"><a href="#Scope.find_all-152"><span class="linenos">152</span></a> <span class="k">yield</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Returns a generator object which visits all nodes in this scope and only yields those that +match at least one of the specified expression types.</p> + +<p>This does NOT traverse into subscopes.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression_types (type):</strong> the expression type(s) to match.</li> +<li><strong>bfs (bool):</strong> True to use breadth-first search, False to use depth-first.</li> +</ul> + +<h6 id="yields">Yields:</h6> + +<blockquote> + <p>exp.Expression: nodes</p> +</blockquote> +</div> + + + </div> + <div id="Scope.replace" class="classattr"> + <input id="Scope.replace-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">replace</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">old</span>, </span><span class="param"><span class="n">new</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.replace-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.replace"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.replace-154"><a href="#Scope.replace-154"><span class="linenos">154</span></a> <span class="k">def</span> <span class="nf">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span> +</span><span id="Scope.replace-155"><a href="#Scope.replace-155"><span class="linenos">155</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.replace-156"><a href="#Scope.replace-156"><span class="linenos">156</span></a><span class="sd"> Replace `old` with `new`.</span> +</span><span id="Scope.replace-157"><a href="#Scope.replace-157"><span class="linenos">157</span></a> +</span><span id="Scope.replace-158"><a href="#Scope.replace-158"><span class="linenos">158</span></a><span class="sd"> This can be used instead of `exp.Expression.replace` to ensure the `Scope` is kept up-to-date.</span> +</span><span id="Scope.replace-159"><a href="#Scope.replace-159"><span class="linenos">159</span></a> +</span><span id="Scope.replace-160"><a href="#Scope.replace-160"><span class="linenos">160</span></a><span class="sd"> Args:</span> +</span><span id="Scope.replace-161"><a href="#Scope.replace-161"><span class="linenos">161</span></a><span class="sd"> old (exp.Expression): old node</span> +</span><span id="Scope.replace-162"><a href="#Scope.replace-162"><span class="linenos">162</span></a><span class="sd"> new (exp.Expression): new node</span> +</span><span id="Scope.replace-163"><a href="#Scope.replace-163"><span class="linenos">163</span></a><span class="sd"> """</span> +</span><span id="Scope.replace-164"><a href="#Scope.replace-164"><span class="linenos">164</span></a> <span class="n">old</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">new</span><span class="p">)</span> +</span><span id="Scope.replace-165"><a href="#Scope.replace-165"><span class="linenos">165</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span></pre></div> + + + <div class="docstring"><p>Replace <code>old</code> with <code>new</code>.</p> + +<p>This can be used instead of <code>exp.Expression.replace</code> to ensure the <code><a href="#Scope">Scope</a></code> is kept up-to-date.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>old (exp.Expression):</strong> old node</li> +<li><strong>new (exp.Expression):</strong> new node</li> +</ul> +</div> + + + </div> + <div id="Scope.tables" class="classattr"> + <div class="attr variable"> + <span class="name">tables</span> + + + </div> + <a class="headerlink" href="#Scope.tables"></a> + + <div class="docstring"><p>List of tables in this scope.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Table]: tables</p> +</blockquote> +</div> + + + </div> + <div id="Scope.ctes" class="classattr"> + <div class="attr variable"> + <span class="name">ctes</span> + + + </div> + <a class="headerlink" href="#Scope.ctes"></a> + + <div class="docstring"><p>List of CTEs in this scope.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.CTE]: ctes</p> +</blockquote> +</div> + + + </div> + <div id="Scope.derived_tables" class="classattr"> + <div class="attr variable"> + <span class="name">derived_tables</span> + + + </div> + <a class="headerlink" href="#Scope.derived_tables"></a> + + <div class="docstring"><p>List of derived tables in this scope.</p> + +<h6 id="for-example">For example:</h6> + +<blockquote> + <p>SELECT * FROM (SELECT ...) <- that's a derived table</p> +</blockquote> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Subquery]: derived tables</p> +</blockquote> +</div> + + + </div> + <div id="Scope.subqueries" class="classattr"> + <div class="attr variable"> + <span class="name">subqueries</span> + + + </div> + <a class="headerlink" href="#Scope.subqueries"></a> + + <div class="docstring"><p>List of subqueries in this scope.</p> + +<h6 id="for-example">For example:</h6> + +<blockquote> + <p>SELECT * FROM x WHERE a IN (SELECT ...) <- that's a subquery</p> +</blockquote> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Subqueryable]: subqueries</p> +</blockquote> +</div> + + + </div> + <div id="Scope.columns" class="classattr"> + <div class="attr variable"> + <span class="name">columns</span> + + + </div> + <a class="headerlink" href="#Scope.columns"></a> + + <div class="docstring"><p>List of columns in this scope.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Column]: Column instances in this scope, plus any + Columns that reference this scope from correlated subqueries.</p> +</blockquote> +</div> + + + </div> + <div id="Scope.selected_sources" class="classattr"> + <div class="attr variable"> + <span class="name">selected_sources</span> + + + </div> + <a class="headerlink" href="#Scope.selected_sources"></a> + + <div class="docstring"><p>Mapping of nodes and sources that are actually selected from in this scope.</p> + +<p>That is, all tables in a schema are selectable at any point. But a +table only becomes a selected source if it's included in a FROM or JOIN clause.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>dict[str, (exp.Table|exp.Select, exp.Table|Scope)]: selected sources and nodes</p> +</blockquote> +</div> + + + </div> + <div id="Scope.cte_sources" class="classattr"> + <div class="attr variable"> + <span class="name">cte_sources</span> + + + </div> + <a class="headerlink" href="#Scope.cte_sources"></a> + + <div class="docstring"><p>Sources that are CTEs.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>dict[str, Scope]: Mapping of source alias to Scope</p> +</blockquote> +</div> + + + </div> + <div id="Scope.selects" class="classattr"> + <div class="attr variable"> + <span class="name">selects</span> + + + </div> + <a class="headerlink" href="#Scope.selects"></a> + + <div class="docstring"><p>Select expressions of this scope.</p> + +<p>For example, for the following expression: + SELECT 1 as a, 2 as b FROM x</p> + +<p>The outputs are the "1 as a" and "2 as b" expressions.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Expression]: expressions</p> +</blockquote> +</div> + + + </div> + <div id="Scope.external_columns" class="classattr"> + <div class="attr variable"> + <span class="name">external_columns</span> + + + </div> + <a class="headerlink" href="#Scope.external_columns"></a> + + <div class="docstring"><p>Columns that appear to reference sources in outer scopes.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Column]: Column instances that don't reference + sources in the current scope.</p> +</blockquote> +</div> + + + </div> + <div id="Scope.unqualified_columns" class="classattr"> + <div class="attr variable"> + <span class="name">unqualified_columns</span> + + + </div> + <a class="headerlink" href="#Scope.unqualified_columns"></a> + + <div class="docstring"><p>Unqualified columns in the current scope.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Column]: Unqualified columns</p> +</blockquote> +</div> + + + </div> + <div id="Scope.join_hints" class="classattr"> + <div class="attr variable"> + <span class="name">join_hints</span> + + + </div> + <a class="headerlink" href="#Scope.join_hints"></a> + + <div class="docstring"><p>Hints that exist in the scope that reference tables</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.JoinHint]: Join hints that are referenced within the scope</p> +</blockquote> +</div> + + + </div> + <div id="Scope.source_columns" class="classattr"> + <input id="Scope.source_columns-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">source_columns</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">source_name</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.source_columns-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.source_columns"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.source_columns-346"><a href="#Scope.source_columns-346"><span class="linenos">346</span></a> <span class="k">def</span> <span class="nf">source_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_name</span><span class="p">):</span> +</span><span id="Scope.source_columns-347"><a href="#Scope.source_columns-347"><span class="linenos">347</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.source_columns-348"><a href="#Scope.source_columns-348"><span class="linenos">348</span></a><span class="sd"> Get all columns in the current scope for a particular source.</span> +</span><span id="Scope.source_columns-349"><a href="#Scope.source_columns-349"><span class="linenos">349</span></a> +</span><span id="Scope.source_columns-350"><a href="#Scope.source_columns-350"><span class="linenos">350</span></a><span class="sd"> Args:</span> +</span><span id="Scope.source_columns-351"><a href="#Scope.source_columns-351"><span class="linenos">351</span></a><span class="sd"> source_name (str): Name of the source</span> +</span><span id="Scope.source_columns-352"><a href="#Scope.source_columns-352"><span class="linenos">352</span></a><span class="sd"> Returns:</span> +</span><span id="Scope.source_columns-353"><a href="#Scope.source_columns-353"><span class="linenos">353</span></a><span class="sd"> list[exp.Column]: Column instances that reference `source_name`</span> +</span><span id="Scope.source_columns-354"><a href="#Scope.source_columns-354"><span class="linenos">354</span></a><span class="sd"> """</span> +</span><span id="Scope.source_columns-355"><a href="#Scope.source_columns-355"><span class="linenos">355</span></a> <span class="k">return</span> <span class="p">[</span><span class="n">column</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="n">source_name</span><span class="p">]</span> +</span></pre></div> + + + <div class="docstring"><p>Get all columns in the current scope for a particular source.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>source_name (str):</strong> Name of the source</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[exp.Column]: Column instances that reference <code>source_name</code></p> +</blockquote> +</div> + + + </div> + <div id="Scope.is_subquery" class="classattr"> + <div class="attr variable"> + <span class="name">is_subquery</span> + + + </div> + <a class="headerlink" href="#Scope.is_subquery"></a> + + <div class="docstring"><p>Determine if this scope is a subquery</p> +</div> + + + </div> + <div id="Scope.is_derived_table" class="classattr"> + <div class="attr variable"> + <span class="name">is_derived_table</span> + + + </div> + <a class="headerlink" href="#Scope.is_derived_table"></a> + + <div class="docstring"><p>Determine if this scope is a derived table</p> +</div> + + + </div> + <div id="Scope.is_union" class="classattr"> + <div class="attr variable"> + <span class="name">is_union</span> + + + </div> + <a class="headerlink" href="#Scope.is_union"></a> + + <div class="docstring"><p>Determine if this scope is a union</p> +</div> + + + </div> + <div id="Scope.is_cte" class="classattr"> + <div class="attr variable"> + <span class="name">is_cte</span> + + + </div> + <a class="headerlink" href="#Scope.is_cte"></a> + + <div class="docstring"><p>Determine if this scope is a common table expression</p> +</div> + + + </div> + <div id="Scope.is_root" class="classattr"> + <div class="attr variable"> + <span class="name">is_root</span> + + + </div> + <a class="headerlink" href="#Scope.is_root"></a> + + <div class="docstring"><p>Determine if this is the root scope</p> +</div> + + + </div> + <div id="Scope.is_udtf" class="classattr"> + <div class="attr variable"> + <span class="name">is_udtf</span> + + + </div> + <a class="headerlink" href="#Scope.is_udtf"></a> + + <div class="docstring"><p>Determine if this scope is a UDTF (User Defined Table Function)</p> +</div> + + + </div> + <div id="Scope.is_correlated_subquery" class="classattr"> + <div class="attr variable"> + <span class="name">is_correlated_subquery</span> + + + </div> + <a class="headerlink" href="#Scope.is_correlated_subquery"></a> + + <div class="docstring"><p>Determine if this scope is a correlated subquery</p> +</div> + + + </div> + <div id="Scope.rename_source" class="classattr"> + <input id="Scope.rename_source-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">rename_source</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">old_name</span>, </span><span class="param"><span class="n">new_name</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.rename_source-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.rename_source"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.rename_source-392"><a href="#Scope.rename_source-392"><span class="linenos">392</span></a> <span class="k">def</span> <span class="nf">rename_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_name</span><span class="p">,</span> <span class="n">new_name</span><span class="p">):</span> +</span><span id="Scope.rename_source-393"><a href="#Scope.rename_source-393"><span class="linenos">393</span></a><span class="w"> </span><span class="sd">"""Rename a source in this scope"""</span> +</span><span id="Scope.rename_source-394"><a href="#Scope.rename_source-394"><span class="linenos">394</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">old_name</span> <span class="ow">or</span> <span class="s2">""</span><span class="p">,</span> <span class="p">[])</span> +</span><span id="Scope.rename_source-395"><a href="#Scope.rename_source-395"><span class="linenos">395</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">new_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">columns</span> +</span></pre></div> + + + <div class="docstring"><p>Rename a source in this scope</p> +</div> + + + </div> + <div id="Scope.add_source" class="classattr"> + <input id="Scope.add_source-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">add_source</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">name</span>, </span><span class="param"><span class="n">source</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.add_source-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.add_source"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.add_source-397"><a href="#Scope.add_source-397"><span class="linenos">397</span></a> <span class="k">def</span> <span class="nf">add_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">source</span><span class="p">):</span> +</span><span id="Scope.add_source-398"><a href="#Scope.add_source-398"><span class="linenos">398</span></a><span class="w"> </span><span class="sd">"""Add a source to this scope"""</span> +</span><span id="Scope.add_source-399"><a href="#Scope.add_source-399"><span class="linenos">399</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span> +</span><span id="Scope.add_source-400"><a href="#Scope.add_source-400"><span class="linenos">400</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span></pre></div> + + + <div class="docstring"><p>Add a source to this scope</p> +</div> + + + </div> + <div id="Scope.remove_source" class="classattr"> + <input id="Scope.remove_source-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">remove_source</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">name</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.remove_source-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.remove_source"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.remove_source-402"><a href="#Scope.remove_source-402"><span class="linenos">402</span></a> <span class="k">def</span> <span class="nf">remove_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> +</span><span id="Scope.remove_source-403"><a href="#Scope.remove_source-403"><span class="linenos">403</span></a><span class="w"> </span><span class="sd">"""Remove a source from this scope"""</span> +</span><span id="Scope.remove_source-404"><a href="#Scope.remove_source-404"><span class="linenos">404</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="Scope.remove_source-405"><a href="#Scope.remove_source-405"><span class="linenos">405</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span> +</span></pre></div> + + + <div class="docstring"><p>Remove a source from this scope</p> +</div> + + + </div> + <div id="Scope.traverse" class="classattr"> + <input id="Scope.traverse-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">traverse</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.traverse-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.traverse"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.traverse-410"><a href="#Scope.traverse-410"><span class="linenos">410</span></a> <span class="k">def</span> <span class="nf">traverse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope.traverse-411"><a href="#Scope.traverse-411"><span class="linenos">411</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.traverse-412"><a href="#Scope.traverse-412"><span class="linenos">412</span></a><span class="sd"> Traverse the scope tree from this node.</span> +</span><span id="Scope.traverse-413"><a href="#Scope.traverse-413"><span class="linenos">413</span></a> +</span><span id="Scope.traverse-414"><a href="#Scope.traverse-414"><span class="linenos">414</span></a><span class="sd"> Yields:</span> +</span><span id="Scope.traverse-415"><a href="#Scope.traverse-415"><span class="linenos">415</span></a><span class="sd"> Scope: scope instances in depth-first-search post-order</span> +</span><span id="Scope.traverse-416"><a href="#Scope.traverse-416"><span class="linenos">416</span></a><span class="sd"> """</span> +</span><span id="Scope.traverse-417"><a href="#Scope.traverse-417"><span class="linenos">417</span></a> <span class="k">for</span> <span class="n">child_scope</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span> +</span><span id="Scope.traverse-418"><a href="#Scope.traverse-418"><span class="linenos">418</span></a> <span class="bp">self</span><span class="o">.</span><span class="n">cte_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">union_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">derived_table_scopes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">subquery_scopes</span> +</span><span id="Scope.traverse-419"><a href="#Scope.traverse-419"><span class="linenos">419</span></a> <span class="p">):</span> +</span><span id="Scope.traverse-420"><a href="#Scope.traverse-420"><span class="linenos">420</span></a> <span class="k">yield from</span> <span class="n">child_scope</span><span class="o">.</span><span class="n">traverse</span><span class="p">()</span> +</span><span id="Scope.traverse-421"><a href="#Scope.traverse-421"><span class="linenos">421</span></a> <span class="k">yield</span> <span class="bp">self</span> +</span></pre></div> + + + <div class="docstring"><p>Traverse the scope tree from this node.</p> + +<h6 id="yields">Yields:</h6> + +<blockquote> + <p>Scope: scope instances in depth-first-search post-order</p> +</blockquote> +</div> + + + </div> + <div id="Scope.ref_count" class="classattr"> + <input id="Scope.ref_count-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">ref_count</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="Scope.ref_count-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#Scope.ref_count"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="Scope.ref_count-423"><a href="#Scope.ref_count-423"><span class="linenos">423</span></a> <span class="k">def</span> <span class="nf">ref_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> +</span><span id="Scope.ref_count-424"><a href="#Scope.ref_count-424"><span class="linenos">424</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="Scope.ref_count-425"><a href="#Scope.ref_count-425"><span class="linenos">425</span></a><span class="sd"> Count the number of times each scope in this tree is referenced.</span> +</span><span id="Scope.ref_count-426"><a href="#Scope.ref_count-426"><span class="linenos">426</span></a> +</span><span id="Scope.ref_count-427"><a href="#Scope.ref_count-427"><span class="linenos">427</span></a><span class="sd"> Returns:</span> +</span><span id="Scope.ref_count-428"><a href="#Scope.ref_count-428"><span class="linenos">428</span></a><span class="sd"> dict[int, int]: Mapping of Scope instance ID to reference count</span> +</span><span id="Scope.ref_count-429"><a href="#Scope.ref_count-429"><span class="linenos">429</span></a><span class="sd"> """</span> +</span><span id="Scope.ref_count-430"><a href="#Scope.ref_count-430"><span class="linenos">430</span></a> <span class="n">scope_ref_count</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span> +</span><span id="Scope.ref_count-431"><a href="#Scope.ref_count-431"><span class="linenos">431</span></a> +</span><span id="Scope.ref_count-432"><a href="#Scope.ref_count-432"><span class="linenos">432</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">traverse</span><span class="p">():</span> +</span><span id="Scope.ref_count-433"><a href="#Scope.ref_count-433"><span class="linenos">433</span></a> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">scope</span><span class="o">.</span><span class="n">selected_sources</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="Scope.ref_count-434"><a href="#Scope.ref_count-434"><span class="linenos">434</span></a> <span class="n">scope_ref_count</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">source</span><span class="p">)]</span> <span class="o">+=</span> <span class="mi">1</span> +</span><span id="Scope.ref_count-435"><a href="#Scope.ref_count-435"><span class="linenos">435</span></a> +</span><span id="Scope.ref_count-436"><a href="#Scope.ref_count-436"><span class="linenos">436</span></a> <span class="k">return</span> <span class="n">scope_ref_count</span> +</span></pre></div> + + + <div class="docstring"><p>Count the number of times each scope in this tree is referenced.</p> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>dict[int, int]: Mapping of Scope instance ID to reference count</p> +</blockquote> +</div> + + + </div> + </section> + <section id="traverse_scope"> + <input id="traverse_scope-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">traverse_scope</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="traverse_scope-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#traverse_scope"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="traverse_scope-439"><a href="#traverse_scope-439"><span class="linenos">439</span></a><span class="k">def</span> <span class="nf">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="traverse_scope-440"><a href="#traverse_scope-440"><span class="linenos">440</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="traverse_scope-441"><a href="#traverse_scope-441"><span class="linenos">441</span></a><span class="sd"> Traverse an expression by it's "scopes".</span> +</span><span id="traverse_scope-442"><a href="#traverse_scope-442"><span class="linenos">442</span></a> +</span><span id="traverse_scope-443"><a href="#traverse_scope-443"><span class="linenos">443</span></a><span class="sd"> "Scope" represents the current context of a Select statement.</span> +</span><span id="traverse_scope-444"><a href="#traverse_scope-444"><span class="linenos">444</span></a> +</span><span id="traverse_scope-445"><a href="#traverse_scope-445"><span class="linenos">445</span></a><span class="sd"> This is helpful for optimizing queries, where we need more information than</span> +</span><span id="traverse_scope-446"><a href="#traverse_scope-446"><span class="linenos">446</span></a><span class="sd"> the expression tree itself. For example, we might care about the source</span> +</span><span id="traverse_scope-447"><a href="#traverse_scope-447"><span class="linenos">447</span></a><span class="sd"> names within a subquery. Returns a list because a generator could result in</span> +</span><span id="traverse_scope-448"><a href="#traverse_scope-448"><span class="linenos">448</span></a><span class="sd"> incomplete properties which is confusing.</span> +</span><span id="traverse_scope-449"><a href="#traverse_scope-449"><span class="linenos">449</span></a> +</span><span id="traverse_scope-450"><a href="#traverse_scope-450"><span class="linenos">450</span></a><span class="sd"> Examples:</span> +</span><span id="traverse_scope-451"><a href="#traverse_scope-451"><span class="linenos">451</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="traverse_scope-452"><a href="#traverse_scope-452"><span class="linenos">452</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT a FROM (SELECT a FROM x) AS y")</span> +</span><span id="traverse_scope-453"><a href="#traverse_scope-453"><span class="linenos">453</span></a><span class="sd"> >>> scopes = traverse_scope(expression)</span> +</span><span id="traverse_scope-454"><a href="#traverse_scope-454"><span class="linenos">454</span></a><span class="sd"> >>> scopes[0].expression.sql(), list(scopes[0].sources)</span> +</span><span id="traverse_scope-455"><a href="#traverse_scope-455"><span class="linenos">455</span></a><span class="sd"> ('SELECT a FROM x', ['x'])</span> +</span><span id="traverse_scope-456"><a href="#traverse_scope-456"><span class="linenos">456</span></a><span class="sd"> >>> scopes[1].expression.sql(), list(scopes[1].sources)</span> +</span><span id="traverse_scope-457"><a href="#traverse_scope-457"><span class="linenos">457</span></a><span class="sd"> ('SELECT a FROM (SELECT a FROM x) AS y', ['y'])</span> +</span><span id="traverse_scope-458"><a href="#traverse_scope-458"><span class="linenos">458</span></a> +</span><span id="traverse_scope-459"><a href="#traverse_scope-459"><span class="linenos">459</span></a><span class="sd"> Args:</span> +</span><span id="traverse_scope-460"><a href="#traverse_scope-460"><span class="linenos">460</span></a><span class="sd"> expression (exp.Expression): expression to traverse</span> +</span><span id="traverse_scope-461"><a href="#traverse_scope-461"><span class="linenos">461</span></a><span class="sd"> Returns:</span> +</span><span id="traverse_scope-462"><a href="#traverse_scope-462"><span class="linenos">462</span></a><span class="sd"> list[Scope]: scope instances</span> +</span><span id="traverse_scope-463"><a href="#traverse_scope-463"><span class="linenos">463</span></a><span class="sd"> """</span> +</span><span id="traverse_scope-464"><a href="#traverse_scope-464"><span class="linenos">464</span></a> <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">_traverse_scope</span><span class="p">(</span><span class="n">Scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)))</span> +</span></pre></div> + + + <div class="docstring"><p>Traverse an expression by it's "scopes".</p> + +<p>"Scope" represents the current context of a Select statement.</p> + +<p>This is helpful for optimizing queries, where we need more information than +the expression tree itself. For example, we might care about the source +names within a subquery. Returns a list because a generator could result in +incomplete properties which is confusing.</p> + +<h6 id="examples">Examples:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT a FROM (SELECT a FROM x) AS y"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">scopes</span> <span class="o">=</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">scopes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">sql</span><span class="p">(),</span> <span class="nb">list</span><span class="p">(</span><span class="n">scopes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">sources</span><span class="p">)</span> +<span class="go">('SELECT a FROM x', ['x'])</span> +<span class="gp">>>> </span><span class="n">scopes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">expression</span><span class="o">.</span><span class="n">sql</span><span class="p">(),</span> <span class="nb">list</span><span class="p">(</span><span class="n">scopes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">sources</span><span class="p">)</span> +<span class="go">('SELECT a FROM (SELECT a FROM x) AS y', ['y'])</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (exp.Expression):</strong> expression to traverse</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>list[Scope]: scope instances</p> +</blockquote> +</div> + + + </section> + <section id="build_scope"> + <input id="build_scope-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">build_scope</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="build_scope-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#build_scope"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="build_scope-467"><a href="#build_scope-467"><span class="linenos">467</span></a><span class="k">def</span> <span class="nf">build_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="build_scope-468"><a href="#build_scope-468"><span class="linenos">468</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="build_scope-469"><a href="#build_scope-469"><span class="linenos">469</span></a><span class="sd"> Build a scope tree.</span> +</span><span id="build_scope-470"><a href="#build_scope-470"><span class="linenos">470</span></a> +</span><span id="build_scope-471"><a href="#build_scope-471"><span class="linenos">471</span></a><span class="sd"> Args:</span> +</span><span id="build_scope-472"><a href="#build_scope-472"><span class="linenos">472</span></a><span class="sd"> expression (exp.Expression): expression to build the scope tree for</span> +</span><span id="build_scope-473"><a href="#build_scope-473"><span class="linenos">473</span></a><span class="sd"> Returns:</span> +</span><span id="build_scope-474"><a href="#build_scope-474"><span class="linenos">474</span></a><span class="sd"> Scope: root scope</span> +</span><span id="build_scope-475"><a href="#build_scope-475"><span class="linenos">475</span></a><span class="sd"> """</span> +</span><span id="build_scope-476"><a href="#build_scope-476"><span class="linenos">476</span></a> <span class="k">return</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> +</span></pre></div> + + + <div class="docstring"><p>Build a scope tree.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (exp.Expression):</strong> expression to build the scope tree for</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>Scope: root scope</p> +</blockquote> +</div> + + + </section> + <section id="walk_in_scope"> + <input id="walk_in_scope-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">walk_in_scope</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">bfs</span><span class="o">=</span><span class="kc">True</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="walk_in_scope-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#walk_in_scope"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="walk_in_scope-604"><a href="#walk_in_scope-604"><span class="linenos">604</span></a><span class="k">def</span> <span class="nf">walk_in_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">bfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="walk_in_scope-605"><a href="#walk_in_scope-605"><span class="linenos">605</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="walk_in_scope-606"><a href="#walk_in_scope-606"><span class="linenos">606</span></a><span class="sd"> Returns a generator object which visits all nodes in the syntrax tree, stopping at</span> +</span><span id="walk_in_scope-607"><a href="#walk_in_scope-607"><span class="linenos">607</span></a><span class="sd"> nodes that start child scopes.</span> +</span><span id="walk_in_scope-608"><a href="#walk_in_scope-608"><span class="linenos">608</span></a> +</span><span id="walk_in_scope-609"><a href="#walk_in_scope-609"><span class="linenos">609</span></a><span class="sd"> Args:</span> +</span><span id="walk_in_scope-610"><a href="#walk_in_scope-610"><span class="linenos">610</span></a><span class="sd"> expression (exp.Expression):</span> +</span><span id="walk_in_scope-611"><a href="#walk_in_scope-611"><span class="linenos">611</span></a><span class="sd"> bfs (bool): if set to True the BFS traversal order will be applied,</span> +</span><span id="walk_in_scope-612"><a href="#walk_in_scope-612"><span class="linenos">612</span></a><span class="sd"> otherwise the DFS traversal will be used instead.</span> +</span><span id="walk_in_scope-613"><a href="#walk_in_scope-613"><span class="linenos">613</span></a> +</span><span id="walk_in_scope-614"><a href="#walk_in_scope-614"><span class="linenos">614</span></a><span class="sd"> Yields:</span> +</span><span id="walk_in_scope-615"><a href="#walk_in_scope-615"><span class="linenos">615</span></a><span class="sd"> tuple[exp.Expression, Optional[exp.Expression], str]: node, parent, arg key</span> +</span><span id="walk_in_scope-616"><a href="#walk_in_scope-616"><span class="linenos">616</span></a><span class="sd"> """</span> +</span><span id="walk_in_scope-617"><a href="#walk_in_scope-617"><span class="linenos">617</span></a> <span class="c1"># We'll use this variable to pass state into the dfs generator.</span> +</span><span id="walk_in_scope-618"><a href="#walk_in_scope-618"><span class="linenos">618</span></a> <span class="c1"># Whenever we set it to True, we exclude a subtree from traversal.</span> +</span><span id="walk_in_scope-619"><a href="#walk_in_scope-619"><span class="linenos">619</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="walk_in_scope-620"><a href="#walk_in_scope-620"><span class="linenos">620</span></a> +</span><span id="walk_in_scope-621"><a href="#walk_in_scope-621"><span class="linenos">621</span></a> <span class="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">bfs</span><span class="o">=</span><span class="n">bfs</span><span class="p">,</span> <span class="n">prune</span><span class="o">=</span><span class="k">lambda</span> <span class="o">*</span><span class="n">_</span><span class="p">:</span> <span class="n">prune</span><span class="p">):</span> +</span><span id="walk_in_scope-622"><a href="#walk_in_scope-622"><span class="linenos">622</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="walk_in_scope-623"><a href="#walk_in_scope-623"><span class="linenos">623</span></a> +</span><span id="walk_in_scope-624"><a href="#walk_in_scope-624"><span class="linenos">624</span></a> <span class="k">yield</span> <span class="n">node</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">key</span> +</span><span id="walk_in_scope-625"><a href="#walk_in_scope-625"><span class="linenos">625</span></a> +</span><span id="walk_in_scope-626"><a href="#walk_in_scope-626"><span class="linenos">626</span></a> <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="n">expression</span><span class="p">:</span> +</span><span id="walk_in_scope-627"><a href="#walk_in_scope-627"><span class="linenos">627</span></a> <span class="k">continue</span> +</span><span id="walk_in_scope-628"><a href="#walk_in_scope-628"><span class="linenos">628</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">CTE</span><span class="p">):</span> +</span><span id="walk_in_scope-629"><a href="#walk_in_scope-629"><span class="linenos">629</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="walk_in_scope-630"><a href="#walk_in_scope-630"><span class="linenos">630</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">From</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">)):</span> +</span><span id="walk_in_scope-631"><a href="#walk_in_scope-631"><span class="linenos">631</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="walk_in_scope-632"><a href="#walk_in_scope-632"><span class="linenos">632</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subqueryable</span><span class="p">):</span> +</span><span id="walk_in_scope-633"><a href="#walk_in_scope-633"><span class="linenos">633</span></a> <span class="n">prune</span> <span class="o">=</span> <span class="kc">True</span> +</span></pre></div> + + + <div class="docstring"><p>Returns a generator object which visits all nodes in the syntrax tree, stopping at +nodes that start child scopes.</p> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (exp.Expression):</strong> </li> +<li><strong>bfs (bool):</strong> if set to True the BFS traversal order will be applied, +otherwise the DFS traversal will be used instead.</li> +</ul> + +<h6 id="yields">Yields:</h6> + +<blockquote> + <p>tuple[exp.Expression, Optional[exp.Expression], str]: node, parent, arg key</p> +</blockquote> +</div> + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/simplify.html b/docs/sqlglot/optimizer/simplify.html new file mode 100644 index 0000000..cc83534 --- /dev/null +++ b/docs/sqlglot/optimizer/simplify.html @@ -0,0 +1,1428 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.simplify API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#simplify">simplify</a> + </li> + <li> + <a class="function" href="#rewrite_between">rewrite_between</a> + </li> + <li> + <a class="function" href="#simplify_not">simplify_not</a> + </li> + <li> + <a class="function" href="#flatten">flatten</a> + </li> + <li> + <a class="function" href="#simplify_connectors">simplify_connectors</a> + </li> + <li> + <a class="function" href="#remove_compliments">remove_compliments</a> + </li> + <li> + <a class="function" href="#uniq_sort">uniq_sort</a> + </li> + <li> + <a class="function" href="#absorb_and_eliminate">absorb_and_eliminate</a> + </li> + <li> + <a class="function" href="#simplify_literals">simplify_literals</a> + </li> + <li> + <a class="function" href="#simplify_parens">simplify_parens</a> + </li> + <li> + <a class="function" href="#remove_where_true">remove_where_true</a> + </li> + <li> + <a class="function" href="#always_true">always_true</a> + </li> + <li> + <a class="function" href="#is_complement">is_complement</a> + </li> + <li> + <a class="function" href="#eval_boolean">eval_boolean</a> + </li> + <li> + <a class="function" href="#extract_date">extract_date</a> + </li> + <li> + <a class="function" href="#extract_interval">extract_interval</a> + </li> + <li> + <a class="function" href="#date_literal">date_literal</a> + </li> + <li> + <a class="function" href="#boolean_literal">boolean_literal</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/simplify.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.simplify </h1> + + + <input id="mod-simplify-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-simplify-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">datetime</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a><span class="kn">import</span> <span class="nn">functools</span> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="n">Decimal</span> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="kn">from</span> <span class="nn">sqlglot.expressions</span> <span class="kn">import</span> <span class="n">FALSE</span><span class="p">,</span> <span class="n">NULL</span><span class="p">,</span> <span class="n">TRUE</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="kn">from</span> <span class="nn">sqlglot.generator</span> <span class="kn">import</span> <span class="n">Generator</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a><span class="kn">from</span> <span class="nn">sqlglot.helper</span> <span class="kn">import</span> <span class="n">first</span><span class="p">,</span> <span class="n">while_changing</span> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="n">GENERATOR</span> <span class="o">=</span> <span class="n">Generator</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">identify</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="k">def</span> <span class="nf">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> Rewrite sqlglot AST to simplify expressions.</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a><span class="sd"> Example:</span> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> >>> expression = sqlglot.parse_one("TRUE AND TRUE")</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> >>> simplify(expression).sql()</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> 'TRUE'</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a><span class="sd"> Args:</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a><span class="sd"> expression (sqlglot.Expression): expression to simplify</span> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a><span class="sd"> Returns:</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a><span class="sd"> sqlglot.Expression: simplified expression</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a><span class="sd"> """</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="k">def</span> <span class="nf">_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">expression</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">rewrite_between</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">uniq_sort</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">absorb_and_eliminate</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-36"><a href="#L-36"><span class="linenos"> 36</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">_simplify</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="kc">False</span><span class="p">))</span> +</span><span id="L-37"><a href="#L-37"><span class="linenos"> 37</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_not</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-38"><a href="#L-38"><span class="linenos"> 38</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">flatten</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-39"><a href="#L-39"><span class="linenos"> 39</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_connectors</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">remove_compliments</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="n">node</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="L-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_literals</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_parens</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="k">if</span> <span class="n">root</span><span class="p">:</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="L-46"><a href="#L-46"><span class="linenos"> 46</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-47"><a href="#L-47"><span class="linenos"> 47</span></a> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">while_changing</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify</span><span class="p">)</span> +</span><span id="L-49"><a href="#L-49"><span class="linenos"> 49</span></a> <span class="n">remove_where_true</span><span class="p">(</span><span class="n">expression</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">expression</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><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a><span class="k">def</span> <span class="nf">rewrite_between</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a><span class="w"> </span><span class="sd">"""Rewrite x between y and z to x >= y AND x <= z.</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a><span class="sd"> This is done because comparison simplification is only done on lt/lte/gt/gte.</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a><span class="sd"> """</span> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">):</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"low"</span><span class="p">]),</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"high"</span><span class="p">]),</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="p">)</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a><span class="k">def</span> <span class="nf">simplify_not</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a><span class="sd"> Demorgan's Law</span> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a><span class="sd"> NOT (x OR y) -> NOT x AND NOT y</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a><span class="sd"> NOT (x AND y) -> NOT x OR NOT y</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a><span class="sd"> """</span> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">):</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">condition</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span> +</span><span id="L-77"><a href="#L-77"><span class="linenos"> 77</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">):</span> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">left</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">):</span> +</span><span id="L-80"><a href="#L-80"><span class="linenos"> 80</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">left</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> +</span><span id="L-81"><a href="#L-81"><span class="linenos"> 81</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">):</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">):</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</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">FALSE</span><span class="p">:</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> <span class="c1"># double negation</span> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> <span class="c1"># NOT NOT x -> x</span> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a><span class="k">def</span> <span class="nf">flatten</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-95"><a href="#L-95"><span class="linenos"> 95</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-96"><a href="#L-96"><span class="linenos"> 96</span></a><span class="sd"> A AND (B AND C) -> A AND B AND C</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a><span class="sd"> A OR (B OR C) -> A OR B OR C</span> +</span><span id="L-98"><a href="#L-98"><span class="linenos"> 98</span></a><span class="sd"> """</span> +</span><span id="L-99"><a href="#L-99"><span class="linenos"> 99</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</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">node</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="n">child</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">child</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="n">node</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">child</span><span class="p">)</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> +</span><span id="L-107"><a href="#L-107"><span class="linenos">107</span></a><span class="k">def</span> <span class="nf">simplify_connectors</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-108"><a href="#L-108"><span class="linenos">108</span></a> <span class="k">def</span> <span class="nf">_simplify_connectors</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">right</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="n">left</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">):</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="n">FALSE</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="k">if</span> <span class="n">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> <span class="ow">and</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</span><span class="p">):</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">):</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> <span class="k">return</span> <span class="n">right</span> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</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="n">left</span> +</span><span id="L-123"><a href="#L-123"><span class="linenos">123</span></a> <span class="k">return</span> <span class="n">_simplify_comparison</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span> +</span><span id="L-124"><a href="#L-124"><span class="linenos">124</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">):</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> <span class="ow">or</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</span><span class="p">):</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-127"><a href="#L-127"><span class="linenos">127</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">:</span> +</span><span id="L-128"><a href="#L-128"><span class="linenos">128</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">NULL</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">NULL</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">)</span> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="p">):</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</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="n">right</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="k">if</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">:</span> +</span><span id="L-138"><a href="#L-138"><span class="linenos">138</span></a> <span class="k">return</span> <span class="n">left</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="k">return</span> <span class="n">_simplify_comparison</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">,</span> <span class="n">or_</span><span class="o">=</span><span class="kc">True</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="kc">None</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_connectors</span><span class="p">)</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a><span class="n">LT_LTE</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">)</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a><span class="n">GT_GTE</span> <span class="o">=</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">)</span> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a><span class="n">COMPARISONS</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="o">*</span><span class="n">LT_LTE</span><span class="p">,</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="o">*</span><span class="n">GT_GTE</span><span class="p">,</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">,</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a><span class="p">)</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a><span class="n">INVERSE_COMPARISONS</span> <span class="o">=</span> <span class="p">{</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">,</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">,</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">,</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a><span class="p">}</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a><span class="k">def</span> <span class="nf">_simplify_comparison</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">,</span> <span class="n">or_</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">right</span><span class="p">,</span> <span class="n">COMPARISONS</span><span class="p">):</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="n">ll</span><span class="p">,</span> <span class="n">lr</span> <span class="o">=</span> <span class="n">left</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="n">rl</span><span class="p">,</span> <span class="n">rr</span> <span class="o">=</span> <span class="n">right</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="n">largs</span> <span class="o">=</span> <span class="p">{</span><span class="n">ll</span><span class="p">,</span> <span class="n">lr</span><span class="p">}</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">rargs</span> <span class="o">=</span> <span class="p">{</span><span class="n">rl</span><span class="p">,</span> <span class="n">rr</span><span class="p">}</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="n">matching</span> <span class="o">=</span> <span class="n">largs</span> <span class="o">&</span> <span class="n">rargs</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="n">columns</span> <span class="o">=</span> <span class="p">{</span><span class="n">m</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">matching</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Column</span><span class="p">)}</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="k">if</span> <span class="n">matching</span> <span class="ow">and</span> <span class="n">columns</span><span class="p">:</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="n">l</span> <span class="o">=</span> <span class="n">first</span><span class="p">(</span><span class="n">largs</span> <span class="o">-</span> <span class="n">columns</span><span class="p">)</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="n">r</span> <span class="o">=</span> <span class="n">first</span><span class="p">(</span><span class="n">rargs</span> <span class="o">-</span> <span class="n">columns</span><span class="p">)</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="c1"># make sure the comparison is always of the form x > 1 instead of 1 < x</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="k">if</span> <span class="n">left</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_COMPARISONS</span> <span class="ow">and</span> <span class="n">l</span> <span class="o">==</span> <span class="n">ll</span><span class="p">:</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">left</span> <span class="o">=</span> <span class="n">INVERSE_COMPARISONS</span><span class="p">[</span><span class="n">left</span><span class="o">.</span><span class="vm">__class__</span><span class="p">](</span><span class="n">this</span><span class="o">=</span><span class="n">lr</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">ll</span><span class="p">)</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="k">if</span> <span class="n">right</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="n">INVERSE_COMPARISONS</span> <span class="ow">and</span> <span class="n">r</span> <span class="o">==</span> <span class="n">rl</span><span class="p">:</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="n">right</span> <span class="o">=</span> <span class="n">INVERSE_COMPARISONS</span><span class="p">[</span><span class="n">right</span><span class="o">.</span><span class="vm">__class__</span><span class="p">](</span><span class="n">this</span><span class="o">=</span><span class="n">rr</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">rl</span><span class="p">)</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="n">is_number</span> <span class="ow">and</span> <span class="n">r</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="n">l</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="n">r</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="k">elif</span> <span class="n">l</span><span class="o">.</span><span class="n">is_string</span> <span class="ow">and</span> <span class="n">r</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a> <span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a> <span class="k">for</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">av</span><span class="p">),</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">bv</span><span class="p">)</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(((</span><span class="n">left</span><span class="p">,</span> <span class="n">l</span><span class="p">),</span> <span class="p">(</span><span class="n">right</span><span class="p">,</span> <span class="n">r</span><span class="p">))):</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">LT_LTE</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">LT_LTE</span><span class="p">):</span> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a> <span class="k">return</span> <span class="n">left</span> <span class="k">if</span> <span class="p">(</span><span class="n">av</span> <span class="o">></span> <span class="n">bv</span> <span class="k">if</span> <span class="n">or_</span> <span class="k">else</span> <span class="n">av</span> <span class="o"><=</span> <span class="n">bv</span><span class="p">)</span> <span class="k">else</span> <span class="n">right</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">GT_GTE</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">GT_GTE</span><span class="p">):</span> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="k">return</span> <span class="n">left</span> <span class="k">if</span> <span class="p">(</span><span class="n">av</span> <span class="o"><</span> <span class="n">bv</span> <span class="k">if</span> <span class="n">or_</span> <span class="k">else</span> <span class="n">av</span> <span class="o">>=</span> <span class="n">bv</span><span class="p">)</span> <span class="k">else</span> <span class="n">right</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> <span class="c1"># we can't ever shortcut to true because the column could be null</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">GT_GTE</span><span class="p">):</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">or_</span> <span class="ow">and</span> <span class="n">av</span> <span class="o"><=</span> <span class="n">bv</span><span class="p">:</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">LT_LTE</span><span class="p">):</span> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">or_</span> <span class="ow">and</span> <span class="n">av</span> <span class="o">>=</span> <span class="n">bv</span><span class="p">:</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">):</span> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">):</span> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">av</span> <span class="o">>=</span> <span class="n">bv</span> <span class="k">else</span> <span class="n">a</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">):</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">av</span> <span class="o">></span> <span class="n">bv</span> <span class="k">else</span> <span class="n">a</span> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">):</span> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">av</span> <span class="o"><=</span> <span class="n">bv</span> <span class="k">else</span> <span class="n">a</span> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">):</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">av</span> <span class="o"><</span> <span class="n">bv</span> <span class="k">else</span> <span class="n">a</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">):</span> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">av</span> <span class="o">==</span> <span class="n">bv</span> <span class="k">else</span> <span class="n">a</span> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a><span class="k">def</span> <span class="nf">remove_compliments</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a><span class="sd"> Removing compliments.</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a><span class="sd"> A AND NOT A -> FALSE</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a><span class="sd"> A OR NOT A -> TRUE</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a><span class="sd"> """</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="n">compliment</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="mi">2</span><span class="p">):</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> <span class="k">if</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="k">return</span> <span class="n">compliment</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a><span class="k">def</span> <span class="nf">uniq_sort</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a><span class="sd"> Uniq and sort a connector.</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a><span class="sd"> C AND A AND B AND B -> A AND B AND C</span> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a><span class="sd"> """</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> <span class="n">result_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a> <span class="n">flattened</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> <span class="n">deduped</span> <span class="o">=</span> <span class="p">{</span><span class="n">GENERATOR</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">e</span><span class="p">):</span> <span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">flattened</span><span class="p">}</span> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> <span class="n">arr</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">deduped</span><span class="o">.</span><span class="n">items</span><span class="p">())</span> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a> <span class="c1"># check if the operands are already sorted, if not sort them</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> <span class="c1"># A AND C AND B -> A AND B AND C</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a> <span class="k">if</span> <span class="n">sql</span> <span class="o"><</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]:</span> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">result_func</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">deduped</span><span class="p">[</span><span class="n">sql</span><span class="p">]</span> <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">deduped</span><span class="p">)))</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> <span class="k">break</span> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> <span class="c1"># we didn't have to sort but maybe we need to dedup</span> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">deduped</span><span class="p">)</span> <span class="o"><</span> <span class="nb">len</span><span class="p">(</span><span class="n">flattened</span><span class="p">):</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">result_func</span><span class="p">(</span><span class="o">*</span><span class="n">deduped</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a><span class="k">def</span> <span class="nf">absorb_and_eliminate</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a><span class="sd"> absorption:</span> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a><span class="sd"> A AND (A OR B) -> A</span> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a><span class="sd"> A OR (A AND B) -> A</span> +</span><span id="L-270"><a href="#L-270"><span class="linenos">270</span></a><span class="sd"> A AND (NOT A OR B) -> A AND B</span> +</span><span id="L-271"><a href="#L-271"><span class="linenos">271</span></a><span class="sd"> A OR (NOT A AND B) -> A OR B</span> +</span><span id="L-272"><a href="#L-272"><span class="linenos">272</span></a><span class="sd"> elimination:</span> +</span><span id="L-273"><a href="#L-273"><span class="linenos">273</span></a><span class="sd"> (A AND B) OR (A AND NOT B) -> A</span> +</span><span id="L-274"><a href="#L-274"><span class="linenos">274</span></a><span class="sd"> (A OR B) AND (A OR NOT B) -> A</span> +</span><span id="L-275"><a href="#L-275"><span class="linenos">275</span></a><span class="sd"> """</span> +</span><span id="L-276"><a href="#L-276"><span class="linenos">276</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="L-277"><a href="#L-277"><span class="linenos">277</span></a> <span class="n">kind</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> +</span><span id="L-278"><a href="#L-278"><span class="linenos">278</span></a> +</span><span id="L-279"><a href="#L-279"><span class="linenos">279</span></a> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="mi">2</span><span class="p">):</span> +</span><span id="L-280"><a href="#L-280"><span class="linenos">280</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">kind</span><span class="p">):</span> +</span><span id="L-281"><a href="#L-281"><span class="linenos">281</span></a> <span class="n">aa</span><span class="p">,</span> <span class="n">ab</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="L-282"><a href="#L-282"><span class="linenos">282</span></a> +</span><span id="L-283"><a href="#L-283"><span class="linenos">283</span></a> <span class="c1"># absorb</span> +</span><span id="L-284"><a href="#L-284"><span class="linenos">284</span></a> <span class="k">if</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">aa</span><span class="p">):</span> +</span><span id="L-285"><a href="#L-285"><span class="linenos">285</span></a> <span class="n">aa</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">())</span> +</span><span id="L-286"><a href="#L-286"><span class="linenos">286</span></a> <span class="k">elif</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">ab</span><span class="p">):</span> +</span><span id="L-287"><a href="#L-287"><span class="linenos">287</span></a> <span class="n">ab</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">())</span> +</span><span id="L-288"><a href="#L-288"><span class="linenos">288</span></a> <span class="k">elif</span> <span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">kind</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span><span class="n">b</span><span class="p">})</span> <span class="o"><</span> <span class="nb">set</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">flatten</span><span class="p">()):</span> +</span><span id="L-289"><a href="#L-289"><span class="linenos">289</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-290"><a href="#L-290"><span class="linenos">290</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">kind</span><span class="p">):</span> +</span><span id="L-291"><a href="#L-291"><span class="linenos">291</span></a> <span class="c1"># eliminate</span> +</span><span id="L-292"><a href="#L-292"><span class="linenos">292</span></a> <span class="n">rhs</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="L-293"><a href="#L-293"><span class="linenos">293</span></a> <span class="n">ba</span><span class="p">,</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">rhs</span> +</span><span id="L-294"><a href="#L-294"><span class="linenos">294</span></a> +</span><span id="L-295"><a href="#L-295"><span class="linenos">295</span></a> <span class="k">if</span> <span class="n">aa</span> <span class="ow">in</span> <span class="n">rhs</span> <span class="ow">and</span> <span class="p">(</span><span class="n">is_complement</span><span class="p">(</span><span class="n">ab</span><span class="p">,</span> <span class="n">ba</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">ab</span><span class="p">,</span> <span class="n">bb</span><span class="p">)):</span> +</span><span id="L-296"><a href="#L-296"><span class="linenos">296</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">aa</span><span class="p">)</span> +</span><span id="L-297"><a href="#L-297"><span class="linenos">297</span></a> <span class="n">b</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">aa</span><span class="p">)</span> +</span><span id="L-298"><a href="#L-298"><span class="linenos">298</span></a> <span class="k">elif</span> <span class="n">ab</span> <span class="ow">in</span> <span class="n">rhs</span> <span class="ow">and</span> <span class="p">(</span><span class="n">is_complement</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span> <span class="n">ba</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span> <span class="n">bb</span><span class="p">)):</span> +</span><span id="L-299"><a href="#L-299"><span class="linenos">299</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">ab</span><span class="p">)</span> +</span><span id="L-300"><a href="#L-300"><span class="linenos">300</span></a> <span class="n">b</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">ab</span><span class="p">)</span> +</span><span id="L-301"><a href="#L-301"><span class="linenos">301</span></a> +</span><span id="L-302"><a href="#L-302"><span class="linenos">302</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-303"><a href="#L-303"><span class="linenos">303</span></a> +</span><span id="L-304"><a href="#L-304"><span class="linenos">304</span></a> +</span><span id="L-305"><a href="#L-305"><span class="linenos">305</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-306"><a href="#L-306"><span class="linenos">306</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="L-307"><a href="#L-307"><span class="linenos">307</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">)</span> +</span><span id="L-308"><a href="#L-308"><span class="linenos">308</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> +</span><span id="L-309"><a href="#L-309"><span class="linenos">309</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-310"><a href="#L-310"><span class="linenos">310</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-311"><a href="#L-311"><span class="linenos">311</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="L-312"><a href="#L-312"><span class="linenos">312</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> +</span><span id="L-313"><a href="#L-313"><span class="linenos">313</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> +</span><span id="L-314"><a href="#L-314"><span class="linenos">314</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-315"><a href="#L-315"><span class="linenos">315</span></a> +</span><span id="L-316"><a href="#L-316"><span class="linenos">316</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-317"><a href="#L-317"><span class="linenos">317</span></a> +</span><span id="L-318"><a href="#L-318"><span class="linenos">318</span></a> +</span><span id="L-319"><a href="#L-319"><span class="linenos">319</span></a><span class="k">def</span> <span class="nf">_simplify_binary</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-320"><a href="#L-320"><span class="linenos">320</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">):</span> +</span><span id="L-321"><a href="#L-321"><span class="linenos">321</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="L-322"><a href="#L-322"><span class="linenos">322</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-323"><a href="#L-323"><span class="linenos">323</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">True</span> +</span><span id="L-324"><a href="#L-324"><span class="linenos">324</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-325"><a href="#L-325"><span class="linenos">325</span></a> <span class="n">c</span> <span class="o">=</span> <span class="n">b</span> +</span><span id="L-326"><a href="#L-326"><span class="linenos">326</span></a> <span class="n">not_</span> <span class="o">=</span> <span class="kc">False</span> +</span><span id="L-327"><a href="#L-327"><span class="linenos">327</span></a> +</span><span id="L-328"><a href="#L-328"><span class="linenos">328</span></a> <span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">:</span> +</span><span id="L-329"><a href="#L-329"><span class="linenos">329</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">):</span> +</span><span id="L-330"><a href="#L-330"><span class="linenos">330</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-331"><a href="#L-331"><span class="linenos">331</span></a> <span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">:</span> +</span><span id="L-332"><a href="#L-332"><span class="linenos">332</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">not_</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="L-333"><a href="#L-333"><span class="linenos">333</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">NullSafeEQ</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NullSafeNEQ</span><span class="p">)):</span> +</span><span id="L-334"><a href="#L-334"><span class="linenos">334</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-335"><a href="#L-335"><span class="linenos">335</span></a> <span class="k">elif</span> <span class="n">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-336"><a href="#L-336"><span class="linenos">336</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-337"><a href="#L-337"><span class="linenos">337</span></a> +</span><span id="L-338"><a href="#L-338"><span class="linenos">338</span></a> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_number</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="L-339"><a href="#L-339"><span class="linenos">339</span></a> <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-340"><a href="#L-340"><span class="linenos">340</span></a> <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">is_int</span> <span class="k">else</span> <span class="n">Decimal</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-341"><a href="#L-341"><span class="linenos">341</span></a> +</span><span id="L-342"><a href="#L-342"><span class="linenos">342</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-343"><a href="#L-343"><span class="linenos">343</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-344"><a href="#L-344"><span class="linenos">344</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> +</span><span id="L-345"><a href="#L-345"><span class="linenos">345</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-346"><a href="#L-346"><span class="linenos">346</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Mul</span><span class="p">):</span> +</span><span id="L-347"><a href="#L-347"><span class="linenos">347</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-348"><a href="#L-348"><span class="linenos">348</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Div</span><span class="p">):</span> +</span><span id="L-349"><a href="#L-349"><span class="linenos">349</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span> +</span><span id="L-350"><a href="#L-350"><span class="linenos">350</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">//</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-351"><a href="#L-351"><span class="linenos">351</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">a</span> <span class="o">/</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-352"><a href="#L-352"><span class="linenos">352</span></a> +</span><span id="L-353"><a href="#L-353"><span class="linenos">353</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-354"><a href="#L-354"><span class="linenos">354</span></a> +</span><span id="L-355"><a href="#L-355"><span class="linenos">355</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> +</span><span id="L-356"><a href="#L-356"><span class="linenos">356</span></a> <span class="k">return</span> <span class="n">boolean</span> +</span><span id="L-357"><a href="#L-357"><span class="linenos">357</span></a> <span class="k">elif</span> <span class="n">a</span><span class="o">.</span><span class="n">is_string</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">is_string</span><span class="p">:</span> +</span><span id="L-358"><a href="#L-358"><span class="linenos">358</span></a> <span class="n">boolean</span> <span class="o">=</span> <span class="n">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-359"><a href="#L-359"><span class="linenos">359</span></a> +</span><span id="L-360"><a href="#L-360"><span class="linenos">360</span></a> <span class="k">if</span> <span class="n">boolean</span><span class="p">:</span> +</span><span id="L-361"><a href="#L-361"><span class="linenos">361</span></a> <span class="k">return</span> <span class="n">boolean</span> +</span><span id="L-362"><a href="#L-362"><span class="linenos">362</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">):</span> +</span><span id="L-363"><a href="#L-363"><span class="linenos">363</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-364"><a href="#L-364"><span class="linenos">364</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span><span class="p">:</span> +</span><span id="L-365"><a href="#L-365"><span class="linenos">365</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-366"><a href="#L-366"><span class="linenos">366</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-367"><a href="#L-367"><span class="linenos">367</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Sub</span><span class="p">):</span> +</span><span id="L-368"><a href="#L-368"><span class="linenos">368</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-369"><a href="#L-369"><span class="linenos">369</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Interval</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Cast</span><span class="p">):</span> +</span><span id="L-370"><a href="#L-370"><span class="linenos">370</span></a> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">extract_interval</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">extract_date</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-371"><a href="#L-371"><span class="linenos">371</span></a> <span class="c1"># you cannot subtract a date from an interval</span> +</span><span id="L-372"><a href="#L-372"><span class="linenos">372</span></a> <span class="k">if</span> <span class="n">a</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Add</span><span class="p">):</span> +</span><span id="L-373"><a href="#L-373"><span class="linenos">373</span></a> <span class="k">return</span> <span class="n">date_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-374"><a href="#L-374"><span class="linenos">374</span></a> +</span><span id="L-375"><a href="#L-375"><span class="linenos">375</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-376"><a href="#L-376"><span class="linenos">376</span></a> +</span><span id="L-377"><a href="#L-377"><span class="linenos">377</span></a> +</span><span id="L-378"><a href="#L-378"><span class="linenos">378</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-379"><a href="#L-379"><span class="linenos">379</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="L-380"><a href="#L-380"><span class="linenos">380</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> +</span><span id="L-381"><a href="#L-381"><span class="linenos">381</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> +</span><span id="L-382"><a href="#L-382"><span class="linenos">382</span></a> <span class="ow">and</span> <span class="p">(</span> +</span><span id="L-383"><a href="#L-383"><span class="linenos">383</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> +</span><span id="L-384"><a href="#L-384"><span class="linenos">384</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Like</span><span class="p">))</span> +</span><span id="L-385"><a href="#L-385"><span class="linenos">385</span></a> <span class="ow">or</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">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="L-386"><a href="#L-386"><span class="linenos">386</span></a> <span class="p">)</span> +</span><span id="L-387"><a href="#L-387"><span class="linenos">387</span></a> <span class="p">):</span> +</span><span id="L-388"><a href="#L-388"><span class="linenos">388</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-389"><a href="#L-389"><span class="linenos">389</span></a> <span class="k">return</span> <span class="n">expression</span> +</span><span id="L-390"><a href="#L-390"><span class="linenos">390</span></a> +</span><span id="L-391"><a href="#L-391"><span class="linenos">391</span></a> +</span><span id="L-392"><a href="#L-392"><span class="linenos">392</span></a><span class="k">def</span> <span class="nf">remove_where_true</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-393"><a href="#L-393"><span class="linenos">393</span></a> <span class="k">for</span> <span class="n">where</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">):</span> +</span><span id="L-394"><a href="#L-394"><span class="linenos">394</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">):</span> +</span><span id="L-395"><a href="#L-395"><span class="linenos">395</span></a> <span class="n">where</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"where"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-396"><a href="#L-396"><span class="linenos">396</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="L-397"><a href="#L-397"><span class="linenos">397</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)):</span> +</span><span id="L-398"><a href="#L-398"><span class="linenos">398</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"kind"</span><span class="p">,</span> <span class="s2">"CROSS"</span><span class="p">)</span> +</span><span id="L-399"><a href="#L-399"><span class="linenos">399</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"on"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="L-400"><a href="#L-400"><span class="linenos">400</span></a> +</span><span id="L-401"><a href="#L-401"><span class="linenos">401</span></a> +</span><span id="L-402"><a href="#L-402"><span class="linenos">402</span></a><span class="k">def</span> <span class="nf">always_true</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-403"><a href="#L-403"><span class="linenos">403</span></a> <span class="k">return</span> <span class="n">expression</span> <span class="o">==</span> <span class="n">TRUE</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">)</span> +</span><span id="L-404"><a href="#L-404"><span class="linenos">404</span></a> +</span><span id="L-405"><a href="#L-405"><span class="linenos">405</span></a> +</span><span id="L-406"><a href="#L-406"><span class="linenos">406</span></a><span class="k">def</span> <span class="nf">is_complement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-407"><a href="#L-407"><span class="linenos">407</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">a</span> +</span><span id="L-408"><a href="#L-408"><span class="linenos">408</span></a> +</span><span id="L-409"><a href="#L-409"><span class="linenos">409</span></a> +</span><span id="L-410"><a href="#L-410"><span class="linenos">410</span></a><span class="k">def</span> <span class="nf">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="L-411"><a href="#L-411"><span class="linenos">411</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">)):</span> +</span><span id="L-412"><a href="#L-412"><span class="linenos">412</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-413"><a href="#L-413"><span class="linenos">413</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">):</span> +</span><span id="L-414"><a href="#L-414"><span class="linenos">414</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">!=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-415"><a href="#L-415"><span class="linenos">415</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">):</span> +</span><span id="L-416"><a href="#L-416"><span class="linenos">416</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">></span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-417"><a href="#L-417"><span class="linenos">417</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">):</span> +</span><span id="L-418"><a href="#L-418"><span class="linenos">418</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">>=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-419"><a href="#L-419"><span class="linenos">419</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">):</span> +</span><span id="L-420"><a href="#L-420"><span class="linenos">420</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o"><</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-421"><a href="#L-421"><span class="linenos">421</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">):</span> +</span><span id="L-422"><a href="#L-422"><span class="linenos">422</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o"><=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-423"><a href="#L-423"><span class="linenos">423</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-424"><a href="#L-424"><span class="linenos">424</span></a> +</span><span id="L-425"><a href="#L-425"><span class="linenos">425</span></a> +</span><span id="L-426"><a href="#L-426"><span class="linenos">426</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">):</span> +</span><span id="L-427"><a href="#L-427"><span class="linenos">427</span></a> <span class="c1"># The "fromisoformat" conversion could fail if the cast is used on an identifier,</span> +</span><span id="L-428"><a href="#L-428"><span class="linenos">428</span></a> <span class="c1"># so in that case we can't extract the date.</span> +</span><span id="L-429"><a href="#L-429"><span class="linenos">429</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-430"><a href="#L-430"><span class="linenos">430</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">:</span> +</span><span id="L-431"><a href="#L-431"><span class="linenos">431</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-432"><a href="#L-432"><span class="linenos">432</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> +</span><span id="L-433"><a href="#L-433"><span class="linenos">433</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-434"><a href="#L-434"><span class="linenos">434</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> +</span><span id="L-435"><a href="#L-435"><span class="linenos">435</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-436"><a href="#L-436"><span class="linenos">436</span></a> +</span><span id="L-437"><a href="#L-437"><span class="linenos">437</span></a> +</span><span id="L-438"><a href="#L-438"><span class="linenos">438</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">interval</span><span class="p">):</span> +</span><span id="L-439"><a href="#L-439"><span class="linenos">439</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="L-440"><a href="#L-440"><span class="linenos">440</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> <span class="c1"># type: ignore</span> +</span><span id="L-441"><a href="#L-441"><span class="linenos">441</span></a> <span class="k">except</span> <span class="ne">ModuleNotFoundError</span><span class="p">:</span> +</span><span id="L-442"><a href="#L-442"><span class="linenos">442</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-443"><a href="#L-443"><span class="linenos">443</span></a> +</span><span id="L-444"><a href="#L-444"><span class="linenos">444</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">interval</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="L-445"><a href="#L-445"><span class="linenos">445</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">interval</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> +</span><span id="L-446"><a href="#L-446"><span class="linenos">446</span></a> +</span><span id="L-447"><a href="#L-447"><span class="linenos">447</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="L-448"><a href="#L-448"><span class="linenos">448</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="L-449"><a href="#L-449"><span class="linenos">449</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="L-450"><a href="#L-450"><span class="linenos">450</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="L-451"><a href="#L-451"><span class="linenos">451</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="L-452"><a href="#L-452"><span class="linenos">452</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="L-453"><a href="#L-453"><span class="linenos">453</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="L-454"><a href="#L-454"><span class="linenos">454</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="L-455"><a href="#L-455"><span class="linenos">455</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="L-456"><a href="#L-456"><span class="linenos">456</span></a> +</span><span id="L-457"><a href="#L-457"><span class="linenos">457</span></a> +</span><span id="L-458"><a href="#L-458"><span class="linenos">458</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> +</span><span id="L-459"><a href="#L-459"><span class="linenos">459</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> +</span><span id="L-460"><a href="#L-460"><span class="linenos">460</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> +</span><span id="L-461"><a href="#L-461"><span class="linenos">461</span></a> <span class="s2">"DATETIME"</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> <span class="k">else</span> <span class="s2">"DATE"</span><span class="p">,</span> +</span><span id="L-462"><a href="#L-462"><span class="linenos">462</span></a> <span class="p">)</span> +</span><span id="L-463"><a href="#L-463"><span class="linenos">463</span></a> +</span><span id="L-464"><a href="#L-464"><span class="linenos">464</span></a> +</span><span id="L-465"><a href="#L-465"><span class="linenos">465</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="L-466"><a href="#L-466"><span class="linenos">466</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="L-467"><a href="#L-467"><span class="linenos">467</span></a> +</span><span id="L-468"><a href="#L-468"><span class="linenos">468</span></a> +</span><span id="L-469"><a href="#L-469"><span class="linenos">469</span></a><span class="k">def</span> <span class="nf">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">simplifier</span><span class="p">):</span> +</span><span id="L-470"><a href="#L-470"><span class="linenos">470</span></a> <span class="n">operands</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-471"><a href="#L-471"><span class="linenos">471</span></a> <span class="n">queue</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">unnest</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span> +</span><span id="L-472"><a href="#L-472"><span class="linenos">472</span></a> <span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">queue</span><span class="p">)</span> +</span><span id="L-473"><a href="#L-473"><span class="linenos">473</span></a> +</span><span id="L-474"><a href="#L-474"><span class="linenos">474</span></a> <span class="k">while</span> <span class="n">queue</span><span class="p">:</span> +</span><span id="L-475"><a href="#L-475"><span class="linenos">475</span></a> <span class="n">a</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> +</span><span id="L-476"><a href="#L-476"><span class="linenos">476</span></a> +</span><span id="L-477"><a href="#L-477"><span class="linenos">477</span></a> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">queue</span><span class="p">:</span> +</span><span id="L-478"><a href="#L-478"><span class="linenos">478</span></a> <span class="n">result</span> <span class="o">=</span> <span class="n">simplifier</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> +</span><span id="L-479"><a href="#L-479"><span class="linenos">479</span></a> +</span><span id="L-480"><a href="#L-480"><span class="linenos">480</span></a> <span class="k">if</span> <span class="n">result</span><span class="p">:</span> +</span><span id="L-481"><a href="#L-481"><span class="linenos">481</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> +</span><span id="L-482"><a href="#L-482"><span class="linenos">482</span></a> <span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> +</span><span id="L-483"><a href="#L-483"><span class="linenos">483</span></a> <span class="k">break</span> +</span><span id="L-484"><a href="#L-484"><span class="linenos">484</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-485"><a href="#L-485"><span class="linenos">485</span></a> <span class="n">operands</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> +</span><span id="L-486"><a href="#L-486"><span class="linenos">486</span></a> +</span><span id="L-487"><a href="#L-487"><span class="linenos">487</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">operands</span><span class="p">)</span> <span class="o"><</span> <span class="n">size</span><span class="p">:</span> +</span><span id="L-488"><a href="#L-488"><span class="linenos">488</span></a> <span class="k">return</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">expression</span><span class="o">=</span><span class="n">b</span><span class="p">),</span> <span class="n">operands</span><span class="p">)</span> +</span><span id="L-489"><a href="#L-489"><span class="linenos">489</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + </section> + <section id="simplify"> + <input id="simplify-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">simplify</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="simplify-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#simplify"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify-16"><a href="#simplify-16"><span class="linenos">16</span></a><span class="k">def</span> <span class="nf">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify-17"><a href="#simplify-17"><span class="linenos">17</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="simplify-18"><a href="#simplify-18"><span class="linenos">18</span></a><span class="sd"> Rewrite sqlglot AST to simplify expressions.</span> +</span><span id="simplify-19"><a href="#simplify-19"><span class="linenos">19</span></a> +</span><span id="simplify-20"><a href="#simplify-20"><span class="linenos">20</span></a><span class="sd"> Example:</span> +</span><span id="simplify-21"><a href="#simplify-21"><span class="linenos">21</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="simplify-22"><a href="#simplify-22"><span class="linenos">22</span></a><span class="sd"> >>> expression = sqlglot.parse_one("TRUE AND TRUE")</span> +</span><span id="simplify-23"><a href="#simplify-23"><span class="linenos">23</span></a><span class="sd"> >>> simplify(expression).sql()</span> +</span><span id="simplify-24"><a href="#simplify-24"><span class="linenos">24</span></a><span class="sd"> 'TRUE'</span> +</span><span id="simplify-25"><a href="#simplify-25"><span class="linenos">25</span></a> +</span><span id="simplify-26"><a href="#simplify-26"><span class="linenos">26</span></a><span class="sd"> Args:</span> +</span><span id="simplify-27"><a href="#simplify-27"><span class="linenos">27</span></a><span class="sd"> expression (sqlglot.Expression): expression to simplify</span> +</span><span id="simplify-28"><a href="#simplify-28"><span class="linenos">28</span></a><span class="sd"> Returns:</span> +</span><span id="simplify-29"><a href="#simplify-29"><span class="linenos">29</span></a><span class="sd"> sqlglot.Expression: simplified expression</span> +</span><span id="simplify-30"><a href="#simplify-30"><span class="linenos">30</span></a><span class="sd"> """</span> +</span><span id="simplify-31"><a href="#simplify-31"><span class="linenos">31</span></a> +</span><span id="simplify-32"><a href="#simplify-32"><span class="linenos">32</span></a> <span class="k">def</span> <span class="nf">_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span> +</span><span id="simplify-33"><a href="#simplify-33"><span class="linenos">33</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">expression</span> +</span><span id="simplify-34"><a href="#simplify-34"><span class="linenos">34</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">rewrite_between</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-35"><a href="#simplify-35"><span class="linenos">35</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">uniq_sort</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-36"><a href="#simplify-36"><span class="linenos">36</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">absorb_and_eliminate</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-37"><a href="#simplify-37"><span class="linenos">37</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">replace_children</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">_simplify</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="kc">False</span><span class="p">))</span> +</span><span id="simplify-38"><a href="#simplify-38"><span class="linenos">38</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_not</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-39"><a href="#simplify-39"><span class="linenos">39</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">flatten</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-40"><a href="#simplify-40"><span class="linenos">40</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_connectors</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-41"><a href="#simplify-41"><span class="linenos">41</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">remove_compliments</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-42"><a href="#simplify-42"><span class="linenos">42</span></a> <span class="n">node</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">parent</span> +</span><span id="simplify-43"><a href="#simplify-43"><span class="linenos">43</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_literals</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-44"><a href="#simplify-44"><span class="linenos">44</span></a> <span class="n">node</span> <span class="o">=</span> <span class="n">simplify_parens</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-45"><a href="#simplify-45"><span class="linenos">45</span></a> <span class="k">if</span> <span class="n">root</span><span class="p">:</span> +</span><span id="simplify-46"><a href="#simplify-46"><span class="linenos">46</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> +</span><span id="simplify-47"><a href="#simplify-47"><span class="linenos">47</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="simplify-48"><a href="#simplify-48"><span class="linenos">48</span></a> +</span><span id="simplify-49"><a href="#simplify-49"><span class="linenos">49</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">while_changing</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify</span><span class="p">)</span> +</span><span id="simplify-50"><a href="#simplify-50"><span class="linenos">50</span></a> <span class="n">remove_where_true</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span> +</span><span id="simplify-51"><a href="#simplify-51"><span class="linenos">51</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to simplify expressions.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"TRUE AND TRUE"</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'TRUE'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to simplify</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: simplified expression</p> +</blockquote> +</div> + + + </section> + <section id="rewrite_between"> + <input id="rewrite_between-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">rewrite_between</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="n">expression</span><span class="p">:</span> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span></span><span class="return-annotation">) -> <span class="n"><a href="../expressions.html#Expression">sqlglot.expressions.Expression</a></span>:</span></span> + + <label class="view-source-button" for="rewrite_between-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#rewrite_between"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="rewrite_between-54"><a href="#rewrite_between-54"><span class="linenos">54</span></a><span class="k">def</span> <span class="nf">rewrite_between</span><span class="p">(</span><span class="n">expression</span><span class="p">:</span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="o">-></span> <span class="n">exp</span><span class="o">.</span><span class="n">Expression</span><span class="p">:</span> +</span><span id="rewrite_between-55"><a href="#rewrite_between-55"><span class="linenos">55</span></a><span class="w"> </span><span class="sd">"""Rewrite x between y and z to x >= y AND x <= z.</span> +</span><span id="rewrite_between-56"><a href="#rewrite_between-56"><span class="linenos">56</span></a> +</span><span id="rewrite_between-57"><a href="#rewrite_between-57"><span class="linenos">57</span></a><span class="sd"> This is done because comparison simplification is only done on lt/lte/gt/gte.</span> +</span><span id="rewrite_between-58"><a href="#rewrite_between-58"><span class="linenos">58</span></a><span class="sd"> """</span> +</span><span id="rewrite_between-59"><a href="#rewrite_between-59"><span class="linenos">59</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Between</span><span class="p">):</span> +</span><span id="rewrite_between-60"><a href="#rewrite_between-60"><span class="linenos">60</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span> +</span><span id="rewrite_between-61"><a href="#rewrite_between-61"><span class="linenos">61</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"low"</span><span class="p">]),</span> +</span><span id="rewrite_between-62"><a href="#rewrite_between-62"><span class="linenos">62</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">expression</span><span class="o">=</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"high"</span><span class="p">]),</span> +</span><span id="rewrite_between-63"><a href="#rewrite_between-63"><span class="linenos">63</span></a> <span class="p">)</span> +</span><span id="rewrite_between-64"><a href="#rewrite_between-64"><span class="linenos">64</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite x between y and z to x >= y AND x <= z.</p> + +<p>This is done because comparison simplification is only done on lt/lte/gt/gte.</p> +</div> + + + </section> + <section id="simplify_not"> + <input id="simplify_not-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">simplify_not</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="simplify_not-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#simplify_not"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_not-67"><a href="#simplify_not-67"><span class="linenos">67</span></a><span class="k">def</span> <span class="nf">simplify_not</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_not-68"><a href="#simplify_not-68"><span class="linenos">68</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="simplify_not-69"><a href="#simplify_not-69"><span class="linenos">69</span></a><span class="sd"> Demorgan's Law</span> +</span><span id="simplify_not-70"><a href="#simplify_not-70"><span class="linenos">70</span></a><span class="sd"> NOT (x OR y) -> NOT x AND NOT y</span> +</span><span id="simplify_not-71"><a href="#simplify_not-71"><span class="linenos">71</span></a><span class="sd"> NOT (x AND y) -> NOT x OR NOT y</span> +</span><span id="simplify_not-72"><a href="#simplify_not-72"><span class="linenos">72</span></a><span class="sd"> """</span> +</span><span id="simplify_not-73"><a href="#simplify_not-73"><span class="linenos">73</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="simplify_not-74"><a href="#simplify_not-74"><span class="linenos">74</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">):</span> +</span><span id="simplify_not-75"><a href="#simplify_not-75"><span class="linenos">75</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="simplify_not-76"><a href="#simplify_not-76"><span class="linenos">76</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">):</span> +</span><span id="simplify_not-77"><a href="#simplify_not-77"><span class="linenos">77</span></a> <span class="n">condition</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span> +</span><span id="simplify_not-78"><a href="#simplify_not-78"><span class="linenos">78</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">):</span> +</span><span id="simplify_not-79"><a href="#simplify_not-79"><span class="linenos">79</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">left</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> +</span><span id="simplify_not-80"><a href="#simplify_not-80"><span class="linenos">80</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">):</span> +</span><span id="simplify_not-81"><a href="#simplify_not-81"><span class="linenos">81</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">left</span><span class="p">),</span> <span class="n">exp</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="n">condition</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> +</span><span id="simplify_not-82"><a href="#simplify_not-82"><span class="linenos">82</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Null</span><span class="p">):</span> +</span><span id="simplify_not-83"><a href="#simplify_not-83"><span class="linenos">83</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="simplify_not-84"><a href="#simplify_not-84"><span class="linenos">84</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">):</span> +</span><span id="simplify_not-85"><a href="#simplify_not-85"><span class="linenos">85</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="simplify_not-86"><a href="#simplify_not-86"><span class="linenos">86</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">FALSE</span><span class="p">:</span> +</span><span id="simplify_not-87"><a href="#simplify_not-87"><span class="linenos">87</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="simplify_not-88"><a href="#simplify_not-88"><span class="linenos">88</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">):</span> +</span><span id="simplify_not-89"><a href="#simplify_not-89"><span class="linenos">89</span></a> <span class="c1"># double negation</span> +</span><span id="simplify_not-90"><a href="#simplify_not-90"><span class="linenos">90</span></a> <span class="c1"># NOT NOT x -> x</span> +</span><span id="simplify_not-91"><a href="#simplify_not-91"><span class="linenos">91</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_not-92"><a href="#simplify_not-92"><span class="linenos">92</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Demorgan's Law +NOT (x OR y) -> NOT x AND NOT y +NOT (x AND y) -> NOT x OR NOT y</p> +</div> + + + </section> + <section id="flatten"> + <input id="flatten-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">flatten</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="flatten-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#flatten"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="flatten-95"><a href="#flatten-95"><span class="linenos"> 95</span></a><span class="k">def</span> <span class="nf">flatten</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="flatten-96"><a href="#flatten-96"><span class="linenos"> 96</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="flatten-97"><a href="#flatten-97"><span class="linenos"> 97</span></a><span class="sd"> A AND (B AND C) -> A AND B AND C</span> +</span><span id="flatten-98"><a href="#flatten-98"><span class="linenos"> 98</span></a><span class="sd"> A OR (B OR C) -> A OR B OR C</span> +</span><span id="flatten-99"><a href="#flatten-99"><span class="linenos"> 99</span></a><span class="sd"> """</span> +</span><span id="flatten-100"><a href="#flatten-100"><span class="linenos">100</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="flatten-101"><a href="#flatten-101"><span class="linenos">101</span></a> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> +</span><span id="flatten-102"><a href="#flatten-102"><span class="linenos">102</span></a> <span class="n">child</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">unnest</span><span class="p">()</span> +</span><span id="flatten-103"><a href="#flatten-103"><span class="linenos">103</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">child</span><span class="p">,</span> <span class="n">expression</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span> +</span><span id="flatten-104"><a href="#flatten-104"><span class="linenos">104</span></a> <span class="n">node</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">child</span><span class="p">)</span> +</span><span id="flatten-105"><a href="#flatten-105"><span class="linenos">105</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>A AND (B AND C) -> A AND B AND C +A OR (B OR C) -> A OR B OR C</p> +</div> + + + </section> + <section id="simplify_connectors"> + <input id="simplify_connectors-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">simplify_connectors</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="simplify_connectors-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#simplify_connectors"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_connectors-108"><a href="#simplify_connectors-108"><span class="linenos">108</span></a><span class="k">def</span> <span class="nf">simplify_connectors</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_connectors-109"><a href="#simplify_connectors-109"><span class="linenos">109</span></a> <span class="k">def</span> <span class="nf">_simplify_connectors</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-110"><a href="#simplify_connectors-110"><span class="linenos">110</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="simplify_connectors-111"><a href="#simplify_connectors-111"><span class="linenos">111</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">right</span><span class="p">:</span> +</span><span id="simplify_connectors-112"><a href="#simplify_connectors-112"><span class="linenos">112</span></a> <span class="k">return</span> <span class="n">left</span> +</span><span id="simplify_connectors-113"><a href="#simplify_connectors-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">):</span> +</span><span id="simplify_connectors-114"><a href="#simplify_connectors-114"><span class="linenos">114</span></a> <span class="k">if</span> <span class="n">FALSE</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-115"><a href="#simplify_connectors-115"><span class="linenos">115</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="simplify_connectors-116"><a href="#simplify_connectors-116"><span class="linenos">116</span></a> <span class="k">if</span> <span class="n">NULL</span> <span class="ow">in</span> <span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-117"><a href="#simplify_connectors-117"><span class="linenos">117</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="simplify_connectors-118"><a href="#simplify_connectors-118"><span class="linenos">118</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> <span class="ow">and</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-119"><a href="#simplify_connectors-119"><span class="linenos">119</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="simplify_connectors-120"><a href="#simplify_connectors-120"><span class="linenos">120</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">):</span> +</span><span id="simplify_connectors-121"><a href="#simplify_connectors-121"><span class="linenos">121</span></a> <span class="k">return</span> <span class="n">right</span> +</span><span id="simplify_connectors-122"><a href="#simplify_connectors-122"><span class="linenos">122</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-123"><a href="#simplify_connectors-123"><span class="linenos">123</span></a> <span class="k">return</span> <span class="n">left</span> +</span><span id="simplify_connectors-124"><a href="#simplify_connectors-124"><span class="linenos">124</span></a> <span class="k">return</span> <span class="n">_simplify_comparison</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span> +</span><span id="simplify_connectors-125"><a href="#simplify_connectors-125"><span class="linenos">125</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">):</span> +</span><span id="simplify_connectors-126"><a href="#simplify_connectors-126"><span class="linenos">126</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">left</span><span class="p">)</span> <span class="ow">or</span> <span class="n">always_true</span><span class="p">(</span><span class="n">right</span><span class="p">):</span> +</span><span id="simplify_connectors-127"><a href="#simplify_connectors-127"><span class="linenos">127</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="simplify_connectors-128"><a href="#simplify_connectors-128"><span class="linenos">128</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">:</span> +</span><span id="simplify_connectors-129"><a href="#simplify_connectors-129"><span class="linenos">129</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span><span id="simplify_connectors-130"><a href="#simplify_connectors-130"><span class="linenos">130</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="simplify_connectors-131"><a href="#simplify_connectors-131"><span class="linenos">131</span></a> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">NULL</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> +</span><span id="simplify_connectors-132"><a href="#simplify_connectors-132"><span class="linenos">132</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">NULL</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">)</span> +</span><span id="simplify_connectors-133"><a href="#simplify_connectors-133"><span class="linenos">133</span></a> <span class="ow">or</span> <span class="p">(</span><span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</span> <span class="ow">and</span> <span class="n">right</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> +</span><span id="simplify_connectors-134"><a href="#simplify_connectors-134"><span class="linenos">134</span></a> <span class="p">):</span> +</span><span id="simplify_connectors-135"><a href="#simplify_connectors-135"><span class="linenos">135</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="simplify_connectors-136"><a href="#simplify_connectors-136"><span class="linenos">136</span></a> <span class="k">if</span> <span class="n">left</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">:</span> +</span><span id="simplify_connectors-137"><a href="#simplify_connectors-137"><span class="linenos">137</span></a> <span class="k">return</span> <span class="n">right</span> +</span><span id="simplify_connectors-138"><a href="#simplify_connectors-138"><span class="linenos">138</span></a> <span class="k">if</span> <span class="n">right</span> <span class="o">==</span> <span class="n">FALSE</span><span class="p">:</span> +</span><span id="simplify_connectors-139"><a href="#simplify_connectors-139"><span class="linenos">139</span></a> <span class="k">return</span> <span class="n">left</span> +</span><span id="simplify_connectors-140"><a href="#simplify_connectors-140"><span class="linenos">140</span></a> <span class="k">return</span> <span class="n">_simplify_comparison</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">,</span> <span class="n">or_</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span><span id="simplify_connectors-141"><a href="#simplify_connectors-141"><span class="linenos">141</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="simplify_connectors-142"><a href="#simplify_connectors-142"><span class="linenos">142</span></a> +</span><span id="simplify_connectors-143"><a href="#simplify_connectors-143"><span class="linenos">143</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_connectors</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="remove_compliments"> + <input id="remove_compliments-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">remove_compliments</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="remove_compliments-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#remove_compliments"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="remove_compliments-224"><a href="#remove_compliments-224"><span class="linenos">224</span></a><span class="k">def</span> <span class="nf">remove_compliments</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="remove_compliments-225"><a href="#remove_compliments-225"><span class="linenos">225</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="remove_compliments-226"><a href="#remove_compliments-226"><span class="linenos">226</span></a><span class="sd"> Removing compliments.</span> +</span><span id="remove_compliments-227"><a href="#remove_compliments-227"><span class="linenos">227</span></a> +</span><span id="remove_compliments-228"><a href="#remove_compliments-228"><span class="linenos">228</span></a><span class="sd"> A AND NOT A -> FALSE</span> +</span><span id="remove_compliments-229"><a href="#remove_compliments-229"><span class="linenos">229</span></a><span class="sd"> A OR NOT A -> TRUE</span> +</span><span id="remove_compliments-230"><a href="#remove_compliments-230"><span class="linenos">230</span></a><span class="sd"> """</span> +</span><span id="remove_compliments-231"><a href="#remove_compliments-231"><span class="linenos">231</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="remove_compliments-232"><a href="#remove_compliments-232"><span class="linenos">232</span></a> <span class="n">compliment</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> +</span><span id="remove_compliments-233"><a href="#remove_compliments-233"><span class="linenos">233</span></a> +</span><span id="remove_compliments-234"><a href="#remove_compliments-234"><span class="linenos">234</span></a> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="mi">2</span><span class="p">):</span> +</span><span id="remove_compliments-235"><a href="#remove_compliments-235"><span class="linenos">235</span></a> <span class="k">if</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="remove_compliments-236"><a href="#remove_compliments-236"><span class="linenos">236</span></a> <span class="k">return</span> <span class="n">compliment</span> +</span><span id="remove_compliments-237"><a href="#remove_compliments-237"><span class="linenos">237</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Removing compliments.</p> + +<p>A AND NOT A -> FALSE +A OR NOT A -> TRUE</p> +</div> + + + </section> + <section id="uniq_sort"> + <input id="uniq_sort-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">uniq_sort</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="uniq_sort-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#uniq_sort"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="uniq_sort-240"><a href="#uniq_sort-240"><span class="linenos">240</span></a><span class="k">def</span> <span class="nf">uniq_sort</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="uniq_sort-241"><a href="#uniq_sort-241"><span class="linenos">241</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="uniq_sort-242"><a href="#uniq_sort-242"><span class="linenos">242</span></a><span class="sd"> Uniq and sort a connector.</span> +</span><span id="uniq_sort-243"><a href="#uniq_sort-243"><span class="linenos">243</span></a> +</span><span id="uniq_sort-244"><a href="#uniq_sort-244"><span class="linenos">244</span></a><span class="sd"> C AND A AND B AND B -> A AND B AND C</span> +</span><span id="uniq_sort-245"><a href="#uniq_sort-245"><span class="linenos">245</span></a><span class="sd"> """</span> +</span><span id="uniq_sort-246"><a href="#uniq_sort-246"><span class="linenos">246</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="uniq_sort-247"><a href="#uniq_sort-247"><span class="linenos">247</span></a> <span class="n">result_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">and_</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">or_</span> +</span><span id="uniq_sort-248"><a href="#uniq_sort-248"><span class="linenos">248</span></a> <span class="n">flattened</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span> +</span><span id="uniq_sort-249"><a href="#uniq_sort-249"><span class="linenos">249</span></a> <span class="n">deduped</span> <span class="o">=</span> <span class="p">{</span><span class="n">GENERATOR</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">e</span><span class="p">):</span> <span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">flattened</span><span class="p">}</span> +</span><span id="uniq_sort-250"><a href="#uniq_sort-250"><span class="linenos">250</span></a> <span class="n">arr</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">deduped</span><span class="o">.</span><span class="n">items</span><span class="p">())</span> +</span><span id="uniq_sort-251"><a href="#uniq_sort-251"><span class="linenos">251</span></a> +</span><span id="uniq_sort-252"><a href="#uniq_sort-252"><span class="linenos">252</span></a> <span class="c1"># check if the operands are already sorted, if not sort them</span> +</span><span id="uniq_sort-253"><a href="#uniq_sort-253"><span class="linenos">253</span></a> <span class="c1"># A AND C AND B -> A AND B AND C</span> +</span><span id="uniq_sort-254"><a href="#uniq_sort-254"><span class="linenos">254</span></a> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span> +</span><span id="uniq_sort-255"><a href="#uniq_sort-255"><span class="linenos">255</span></a> <span class="k">if</span> <span class="n">sql</span> <span class="o"><</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]:</span> +</span><span id="uniq_sort-256"><a href="#uniq_sort-256"><span class="linenos">256</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">result_func</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">deduped</span><span class="p">[</span><span class="n">sql</span><span class="p">]</span> <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">deduped</span><span class="p">)))</span> +</span><span id="uniq_sort-257"><a href="#uniq_sort-257"><span class="linenos">257</span></a> <span class="k">break</span> +</span><span id="uniq_sort-258"><a href="#uniq_sort-258"><span class="linenos">258</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="uniq_sort-259"><a href="#uniq_sort-259"><span class="linenos">259</span></a> <span class="c1"># we didn't have to sort but maybe we need to dedup</span> +</span><span id="uniq_sort-260"><a href="#uniq_sort-260"><span class="linenos">260</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">deduped</span><span class="p">)</span> <span class="o"><</span> <span class="nb">len</span><span class="p">(</span><span class="n">flattened</span><span class="p">):</span> +</span><span id="uniq_sort-261"><a href="#uniq_sort-261"><span class="linenos">261</span></a> <span class="n">expression</span> <span class="o">=</span> <span class="n">result_func</span><span class="p">(</span><span class="o">*</span><span class="n">deduped</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> +</span><span id="uniq_sort-262"><a href="#uniq_sort-262"><span class="linenos">262</span></a> +</span><span id="uniq_sort-263"><a href="#uniq_sort-263"><span class="linenos">263</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Uniq and sort a connector.</p> + +<p>C AND A AND B AND B -> A AND B AND C</p> +</div> + + + </section> + <section id="absorb_and_eliminate"> + <input id="absorb_and_eliminate-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">absorb_and_eliminate</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="absorb_and_eliminate-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#absorb_and_eliminate"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="absorb_and_eliminate-266"><a href="#absorb_and_eliminate-266"><span class="linenos">266</span></a><span class="k">def</span> <span class="nf">absorb_and_eliminate</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-267"><a href="#absorb_and_eliminate-267"><span class="linenos">267</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="absorb_and_eliminate-268"><a href="#absorb_and_eliminate-268"><span class="linenos">268</span></a><span class="sd"> absorption:</span> +</span><span id="absorb_and_eliminate-269"><a href="#absorb_and_eliminate-269"><span class="linenos">269</span></a><span class="sd"> A AND (A OR B) -> A</span> +</span><span id="absorb_and_eliminate-270"><a href="#absorb_and_eliminate-270"><span class="linenos">270</span></a><span class="sd"> A OR (A AND B) -> A</span> +</span><span id="absorb_and_eliminate-271"><a href="#absorb_and_eliminate-271"><span class="linenos">271</span></a><span class="sd"> A AND (NOT A OR B) -> A AND B</span> +</span><span id="absorb_and_eliminate-272"><a href="#absorb_and_eliminate-272"><span class="linenos">272</span></a><span class="sd"> A OR (NOT A AND B) -> A OR B</span> +</span><span id="absorb_and_eliminate-273"><a href="#absorb_and_eliminate-273"><span class="linenos">273</span></a><span class="sd"> elimination:</span> +</span><span id="absorb_and_eliminate-274"><a href="#absorb_and_eliminate-274"><span class="linenos">274</span></a><span class="sd"> (A AND B) OR (A AND NOT B) -> A</span> +</span><span id="absorb_and_eliminate-275"><a href="#absorb_and_eliminate-275"><span class="linenos">275</span></a><span class="sd"> (A OR B) AND (A OR NOT B) -> A</span> +</span><span id="absorb_and_eliminate-276"><a href="#absorb_and_eliminate-276"><span class="linenos">276</span></a><span class="sd"> """</span> +</span><span id="absorb_and_eliminate-277"><a href="#absorb_and_eliminate-277"><span class="linenos">277</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Connector</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-278"><a href="#absorb_and_eliminate-278"><span class="linenos">278</span></a> <span class="n">kind</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Or</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span><span class="p">)</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> +</span><span id="absorb_and_eliminate-279"><a href="#absorb_and_eliminate-279"><span class="linenos">279</span></a> +</span><span id="absorb_and_eliminate-280"><a href="#absorb_and_eliminate-280"><span class="linenos">280</span></a> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="mi">2</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-281"><a href="#absorb_and_eliminate-281"><span class="linenos">281</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">kind</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-282"><a href="#absorb_and_eliminate-282"><span class="linenos">282</span></a> <span class="n">aa</span><span class="p">,</span> <span class="n">ab</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="absorb_and_eliminate-283"><a href="#absorb_and_eliminate-283"><span class="linenos">283</span></a> +</span><span id="absorb_and_eliminate-284"><a href="#absorb_and_eliminate-284"><span class="linenos">284</span></a> <span class="c1"># absorb</span> +</span><span id="absorb_and_eliminate-285"><a href="#absorb_and_eliminate-285"><span class="linenos">285</span></a> <span class="k">if</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">aa</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-286"><a href="#absorb_and_eliminate-286"><span class="linenos">286</span></a> <span class="n">aa</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">())</span> +</span><span id="absorb_and_eliminate-287"><a href="#absorb_and_eliminate-287"><span class="linenos">287</span></a> <span class="k">elif</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">ab</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-288"><a href="#absorb_and_eliminate-288"><span class="linenos">288</span></a> <span class="n">ab</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">())</span> +</span><span id="absorb_and_eliminate-289"><a href="#absorb_and_eliminate-289"><span class="linenos">289</span></a> <span class="k">elif</span> <span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">kind</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span><span class="n">b</span><span class="p">})</span> <span class="o"><</span> <span class="nb">set</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">flatten</span><span class="p">()):</span> +</span><span id="absorb_and_eliminate-290"><a href="#absorb_and_eliminate-290"><span class="linenos">290</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">And</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="absorb_and_eliminate-291"><a href="#absorb_and_eliminate-291"><span class="linenos">291</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">kind</span><span class="p">):</span> +</span><span id="absorb_and_eliminate-292"><a href="#absorb_and_eliminate-292"><span class="linenos">292</span></a> <span class="c1"># eliminate</span> +</span><span id="absorb_and_eliminate-293"><a href="#absorb_and_eliminate-293"><span class="linenos">293</span></a> <span class="n">rhs</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">unnest_operands</span><span class="p">()</span> +</span><span id="absorb_and_eliminate-294"><a href="#absorb_and_eliminate-294"><span class="linenos">294</span></a> <span class="n">ba</span><span class="p">,</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">rhs</span> +</span><span id="absorb_and_eliminate-295"><a href="#absorb_and_eliminate-295"><span class="linenos">295</span></a> +</span><span id="absorb_and_eliminate-296"><a href="#absorb_and_eliminate-296"><span class="linenos">296</span></a> <span class="k">if</span> <span class="n">aa</span> <span class="ow">in</span> <span class="n">rhs</span> <span class="ow">and</span> <span class="p">(</span><span class="n">is_complement</span><span class="p">(</span><span class="n">ab</span><span class="p">,</span> <span class="n">ba</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">ab</span><span class="p">,</span> <span class="n">bb</span><span class="p">)):</span> +</span><span id="absorb_and_eliminate-297"><a href="#absorb_and_eliminate-297"><span class="linenos">297</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">aa</span><span class="p">)</span> +</span><span id="absorb_and_eliminate-298"><a href="#absorb_and_eliminate-298"><span class="linenos">298</span></a> <span class="n">b</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">aa</span><span class="p">)</span> +</span><span id="absorb_and_eliminate-299"><a href="#absorb_and_eliminate-299"><span class="linenos">299</span></a> <span class="k">elif</span> <span class="n">ab</span> <span class="ow">in</span> <span class="n">rhs</span> <span class="ow">and</span> <span class="p">(</span><span class="n">is_complement</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span> <span class="n">ba</span><span class="p">)</span> <span class="ow">or</span> <span class="n">is_complement</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span> <span class="n">bb</span><span class="p">)):</span> +</span><span id="absorb_and_eliminate-300"><a href="#absorb_and_eliminate-300"><span class="linenos">300</span></a> <span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">ab</span><span class="p">)</span> +</span><span id="absorb_and_eliminate-301"><a href="#absorb_and_eliminate-301"><span class="linenos">301</span></a> <span class="n">b</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">ab</span><span class="p">)</span> +</span><span id="absorb_and_eliminate-302"><a href="#absorb_and_eliminate-302"><span class="linenos">302</span></a> +</span><span id="absorb_and_eliminate-303"><a href="#absorb_and_eliminate-303"><span class="linenos">303</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>absorption: + A AND (A OR B) -> A + A OR (A AND B) -> A + A AND (NOT A OR B) -> A AND B + A OR (NOT A AND B) -> A OR B +elimination: + (A AND B) OR (A AND NOT B) -> A + (A OR B) AND (A OR NOT B) -> A</p> +</div> + + + </section> + <section id="simplify_literals"> + <input id="simplify_literals-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">simplify_literals</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="simplify_literals-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#simplify_literals"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_literals-306"><a href="#simplify_literals-306"><span class="linenos">306</span></a><span class="k">def</span> <span class="nf">simplify_literals</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_literals-307"><a href="#simplify_literals-307"><span class="linenos">307</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="simplify_literals-308"><a href="#simplify_literals-308"><span class="linenos">308</span></a> <span class="k">return</span> <span class="n">_flat_simplify</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">_simplify_binary</span><span class="p">)</span> +</span><span id="simplify_literals-309"><a href="#simplify_literals-309"><span class="linenos">309</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Neg</span><span class="p">):</span> +</span><span id="simplify_literals-310"><a href="#simplify_literals-310"><span class="linenos">310</span></a> <span class="n">this</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_literals-311"><a href="#simplify_literals-311"><span class="linenos">311</span></a> <span class="k">if</span> <span class="n">this</span><span class="o">.</span><span class="n">is_number</span><span class="p">:</span> +</span><span id="simplify_literals-312"><a href="#simplify_literals-312"><span class="linenos">312</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">this</span><span class="o">.</span><span class="n">name</span> +</span><span id="simplify_literals-313"><a href="#simplify_literals-313"><span class="linenos">313</span></a> <span class="k">if</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"-"</span><span class="p">:</span> +</span><span id="simplify_literals-314"><a href="#simplify_literals-314"><span class="linenos">314</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> +</span><span id="simplify_literals-315"><a href="#simplify_literals-315"><span class="linenos">315</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="simplify_literals-316"><a href="#simplify_literals-316"><span class="linenos">316</span></a> +</span><span id="simplify_literals-317"><a href="#simplify_literals-317"><span class="linenos">317</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + <section id="simplify_parens"> + <input id="simplify_parens-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">simplify_parens</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="simplify_parens-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#simplify_parens"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="simplify_parens-379"><a href="#simplify_parens-379"><span class="linenos">379</span></a><span class="k">def</span> <span class="nf">simplify_parens</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="simplify_parens-380"><a href="#simplify_parens-380"><span class="linenos">380</span></a> <span class="k">if</span> <span class="p">(</span> +</span><span id="simplify_parens-381"><a href="#simplify_parens-381"><span class="linenos">381</span></a> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Paren</span><span class="p">)</span> +</span><span id="simplify_parens-382"><a href="#simplify_parens-382"><span class="linenos">382</span></a> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Select</span><span class="p">)</span> +</span><span id="simplify_parens-383"><a href="#simplify_parens-383"><span class="linenos">383</span></a> <span class="ow">and</span> <span class="p">(</span> +</span><span id="simplify_parens-384"><a href="#simplify_parens-384"><span class="linenos">384</span></a> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">))</span> +</span><span id="simplify_parens-385"><a href="#simplify_parens-385"><span class="linenos">385</span></a> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Like</span><span class="p">))</span> +</span><span id="simplify_parens-386"><a href="#simplify_parens-386"><span class="linenos">386</span></a> <span class="ow">or</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">this</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">)</span> +</span><span id="simplify_parens-387"><a href="#simplify_parens-387"><span class="linenos">387</span></a> <span class="p">)</span> +</span><span id="simplify_parens-388"><a href="#simplify_parens-388"><span class="linenos">388</span></a> <span class="p">):</span> +</span><span id="simplify_parens-389"><a href="#simplify_parens-389"><span class="linenos">389</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="simplify_parens-390"><a href="#simplify_parens-390"><span class="linenos">390</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + + + </section> + <section id="remove_where_true"> + <input id="remove_where_true-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">remove_where_true</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="remove_where_true-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#remove_where_true"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="remove_where_true-393"><a href="#remove_where_true-393"><span class="linenos">393</span></a><span class="k">def</span> <span class="nf">remove_where_true</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="remove_where_true-394"><a href="#remove_where_true-394"><span class="linenos">394</span></a> <span class="k">for</span> <span class="n">where</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">):</span> +</span><span id="remove_where_true-395"><a href="#remove_where_true-395"><span class="linenos">395</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">where</span><span class="o">.</span><span class="n">this</span><span class="p">):</span> +</span><span id="remove_where_true-396"><a href="#remove_where_true-396"><span class="linenos">396</span></a> <span class="n">where</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"where"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span><span id="remove_where_true-397"><a href="#remove_where_true-397"><span class="linenos">397</span></a> <span class="k">for</span> <span class="n">join</span> <span class="ow">in</span> <span class="n">expression</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Join</span><span class="p">):</span> +</span><span id="remove_where_true-398"><a href="#remove_where_true-398"><span class="linenos">398</span></a> <span class="k">if</span> <span class="n">always_true</span><span class="p">(</span><span class="n">join</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">"on"</span><span class="p">)):</span> +</span><span id="remove_where_true-399"><a href="#remove_where_true-399"><span class="linenos">399</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"kind"</span><span class="p">,</span> <span class="s2">"CROSS"</span><span class="p">)</span> +</span><span id="remove_where_true-400"><a href="#remove_where_true-400"><span class="linenos">400</span></a> <span class="n">join</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"on"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="always_true"> + <input id="always_true-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">always_true</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="always_true-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#always_true"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="always_true-403"><a href="#always_true-403"><span class="linenos">403</span></a><span class="k">def</span> <span class="nf">always_true</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="always_true-404"><a href="#always_true-404"><span class="linenos">404</span></a> <span class="k">return</span> <span class="n">expression</span> <span class="o">==</span> <span class="n">TRUE</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="is_complement"> + <input id="is_complement-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">is_complement</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">a</span>, </span><span class="param"><span class="n">b</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="is_complement-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#is_complement"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="is_complement-407"><a href="#is_complement-407"><span class="linenos">407</span></a><span class="k">def</span> <span class="nf">is_complement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="is_complement-408"><a href="#is_complement-408"><span class="linenos">408</span></a> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Not</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">a</span> +</span></pre></div> + + + + + </section> + <section id="eval_boolean"> + <input id="eval_boolean-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">eval_boolean</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span>, </span><span class="param"><span class="n">a</span>, </span><span class="param"><span class="n">b</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="eval_boolean-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#eval_boolean"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="eval_boolean-411"><a href="#eval_boolean-411"><span class="linenos">411</span></a><span class="k">def</span> <span class="nf">eval_boolean</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> +</span><span id="eval_boolean-412"><a href="#eval_boolean-412"><span class="linenos">412</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Is</span><span class="p">)):</span> +</span><span id="eval_boolean-413"><a href="#eval_boolean-413"><span class="linenos">413</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-414"><a href="#eval_boolean-414"><span class="linenos">414</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">NEQ</span><span class="p">):</span> +</span><span id="eval_boolean-415"><a href="#eval_boolean-415"><span class="linenos">415</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">!=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-416"><a href="#eval_boolean-416"><span class="linenos">416</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GT</span><span class="p">):</span> +</span><span id="eval_boolean-417"><a href="#eval_boolean-417"><span class="linenos">417</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">></span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-418"><a href="#eval_boolean-418"><span class="linenos">418</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">GTE</span><span class="p">):</span> +</span><span id="eval_boolean-419"><a href="#eval_boolean-419"><span class="linenos">419</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o">>=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-420"><a href="#eval_boolean-420"><span class="linenos">420</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LT</span><span class="p">):</span> +</span><span id="eval_boolean-421"><a href="#eval_boolean-421"><span class="linenos">421</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o"><</span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-422"><a href="#eval_boolean-422"><span class="linenos">422</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">LTE</span><span class="p">):</span> +</span><span id="eval_boolean-423"><a href="#eval_boolean-423"><span class="linenos">423</span></a> <span class="k">return</span> <span class="n">boolean_literal</span><span class="p">(</span><span class="n">a</span> <span class="o"><=</span> <span class="n">b</span><span class="p">)</span> +</span><span id="eval_boolean-424"><a href="#eval_boolean-424"><span class="linenos">424</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> + <section id="extract_date"> + <input id="extract_date-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">extract_date</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">cast</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="extract_date-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#extract_date"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="extract_date-427"><a href="#extract_date-427"><span class="linenos">427</span></a><span class="k">def</span> <span class="nf">extract_date</span><span class="p">(</span><span class="n">cast</span><span class="p">):</span> +</span><span id="extract_date-428"><a href="#extract_date-428"><span class="linenos">428</span></a> <span class="c1"># The "fromisoformat" conversion could fail if the cast is used on an identifier,</span> +</span><span id="extract_date-429"><a href="#extract_date-429"><span class="linenos">429</span></a> <span class="c1"># so in that case we can't extract the date.</span> +</span><span id="extract_date-430"><a href="#extract_date-430"><span class="linenos">430</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="extract_date-431"><a href="#extract_date-431"><span class="linenos">431</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATE</span><span class="p">:</span> +</span><span id="extract_date-432"><a href="#extract_date-432"><span class="linenos">432</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="extract_date-433"><a href="#extract_date-433"><span class="linenos">433</span></a> <span class="k">if</span> <span class="n">cast</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"to"</span><span class="p">]</span><span class="o">.</span><span class="n">this</span> <span class="o">==</span> <span class="n">exp</span><span class="o">.</span><span class="n">DataType</span><span class="o">.</span><span class="n">Type</span><span class="o">.</span><span class="n">DATETIME</span><span class="p">:</span> +</span><span id="extract_date-434"><a href="#extract_date-434"><span class="linenos">434</span></a> <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">cast</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="extract_date-435"><a href="#extract_date-435"><span class="linenos">435</span></a> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> +</span><span id="extract_date-436"><a href="#extract_date-436"><span class="linenos">436</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> + <section id="extract_interval"> + <input id="extract_interval-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">extract_interval</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">interval</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="extract_interval-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#extract_interval"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="extract_interval-439"><a href="#extract_interval-439"><span class="linenos">439</span></a><span class="k">def</span> <span class="nf">extract_interval</span><span class="p">(</span><span class="n">interval</span><span class="p">):</span> +</span><span id="extract_interval-440"><a href="#extract_interval-440"><span class="linenos">440</span></a> <span class="k">try</span><span class="p">:</span> +</span><span id="extract_interval-441"><a href="#extract_interval-441"><span class="linenos">441</span></a> <span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="kn">import</span> <span class="n">relativedelta</span> <span class="c1"># type: ignore</span> +</span><span id="extract_interval-442"><a href="#extract_interval-442"><span class="linenos">442</span></a> <span class="k">except</span> <span class="ne">ModuleNotFoundError</span><span class="p">:</span> +</span><span id="extract_interval-443"><a href="#extract_interval-443"><span class="linenos">443</span></a> <span class="k">return</span> <span class="kc">None</span> +</span><span id="extract_interval-444"><a href="#extract_interval-444"><span class="linenos">444</span></a> +</span><span id="extract_interval-445"><a href="#extract_interval-445"><span class="linenos">445</span></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">interval</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> +</span><span id="extract_interval-446"><a href="#extract_interval-446"><span class="linenos">446</span></a> <span class="n">unit</span> <span class="o">=</span> <span class="n">interval</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">"unit"</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> +</span><span id="extract_interval-447"><a href="#extract_interval-447"><span class="linenos">447</span></a> +</span><span id="extract_interval-448"><a href="#extract_interval-448"><span class="linenos">448</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"year"</span><span class="p">:</span> +</span><span id="extract_interval-449"><a href="#extract_interval-449"><span class="linenos">449</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">years</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="extract_interval-450"><a href="#extract_interval-450"><span class="linenos">450</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"month"</span><span class="p">:</span> +</span><span id="extract_interval-451"><a href="#extract_interval-451"><span class="linenos">451</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="extract_interval-452"><a href="#extract_interval-452"><span class="linenos">452</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"week"</span><span class="p">:</span> +</span><span id="extract_interval-453"><a href="#extract_interval-453"><span class="linenos">453</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="extract_interval-454"><a href="#extract_interval-454"><span class="linenos">454</span></a> <span class="k">if</span> <span class="n">unit</span> <span class="o">==</span> <span class="s2">"day"</span><span class="p">:</span> +</span><span id="extract_interval-455"><a href="#extract_interval-455"><span class="linenos">455</span></a> <span class="k">return</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> +</span><span id="extract_interval-456"><a href="#extract_interval-456"><span class="linenos">456</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + + + </section> + <section id="date_literal"> + <input id="date_literal-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">date_literal</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">date</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="date_literal-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#date_literal"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="date_literal-459"><a href="#date_literal-459"><span class="linenos">459</span></a><span class="k">def</span> <span class="nf">date_literal</span><span class="p">(</span><span class="n">date</span><span class="p">):</span> +</span><span id="date_literal-460"><a href="#date_literal-460"><span class="linenos">460</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span> +</span><span id="date_literal-461"><a href="#date_literal-461"><span class="linenos">461</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> +</span><span id="date_literal-462"><a href="#date_literal-462"><span class="linenos">462</span></a> <span class="s2">"DATETIME"</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> <span class="k">else</span> <span class="s2">"DATE"</span><span class="p">,</span> +</span><span id="date_literal-463"><a href="#date_literal-463"><span class="linenos">463</span></a> <span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="boolean_literal"> + <input id="boolean_literal-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">boolean_literal</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">condition</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="boolean_literal-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#boolean_literal"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="boolean_literal-466"><a href="#boolean_literal-466"><span class="linenos">466</span></a><span class="k">def</span> <span class="nf">boolean_literal</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span> +</span><span id="boolean_literal-467"><a href="#boolean_literal-467"><span class="linenos">467</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">()</span> <span class="k">if</span> <span class="n">condition</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">false</span><span class="p">()</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file diff --git a/docs/sqlglot/optimizer/unnest_subqueries.html b/docs/sqlglot/optimizer/unnest_subqueries.html new file mode 100644 index 0000000..0102e43 --- /dev/null +++ b/docs/sqlglot/optimizer/unnest_subqueries.html @@ -0,0 +1,835 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="generator" content="pdoc 12.3.1"/> + <title>sqlglot.optimizer.unnest_subqueries API documentation</title> + + <style>/*! * Bootstrap Reboot v5.0.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors * Copyright 2011-2021 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Forked from Normalize.css, licensed MIT (https://github.com/necolas/normalize.css/blob/master/LICENSE.md) */*,::after,::before{box-sizing:border-box}@media (prefers-reduced-motion:no-preference){:root{scroll-behavior:smooth}}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#fff;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}hr{margin:1rem 0;color:inherit;background-color:currentColor;border:0;opacity:.25}hr:not([size]){height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem;font-weight:500;line-height:1.2}h1{font-size:calc(1.375rem + 1.5vw)}@media (min-width:1200px){h1{font-size:2.5rem}}h2{font-size:calc(1.325rem + .9vw)}@media (min-width:1200px){h2{font-size:2rem}}h3{font-size:calc(1.3rem + .6vw)}@media (min-width:1200px){h3{font-size:1.75rem}}h4{font-size:calc(1.275rem + .3vw)}@media (min-width:1200px){h4{font-size:1.5rem}}h5{font-size:1.25rem}h6{font-size:1rem}p{margin-top:0;margin-bottom:1rem}abbr[data-bs-original-title],abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}ol,ul{padding-left:2rem}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:.875em}mark{padding:.2em;background-color:#fcf8e3}sub,sup{position:relative;font-size:.75em;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#0d6efd;text-decoration:underline}a:hover{color:#0a58ca}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em;direction:ltr;unicode-bidi:bidi-override}pre{display:block;margin-top:0;margin-bottom:1rem;overflow:auto;font-size:.875em}pre code{font-size:inherit;color:inherit;word-break:normal}code{font-size:.875em;color:#d63384;word-wrap:break-word}a>code{color:inherit}kbd{padding:.2rem .4rem;font-size:.875em;color:#fff;background-color:#212529;border-radius:.2rem}kbd kbd{padding:0;font-size:1em;font-weight:700}figure{margin:0 0 1rem}img,svg{vertical-align:middle}table{caption-side:bottom;border-collapse:collapse}caption{padding-top:.5rem;padding-bottom:.5rem;color:#6c757d;text-align:left}th{text-align:inherit;text-align:-webkit-match-parent}tbody,td,tfoot,th,thead,tr{border-color:inherit;border-style:solid;border-width:0}label{display:inline-block}button{border-radius:0}button:focus:not(:focus-visible){outline:0}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,select{text-transform:none}[role=button]{cursor:pointer}select{word-wrap:normal}select:disabled{opacity:1}[list]::-webkit-calendar-picker-indicator{display:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}::-moz-focus-inner{padding:0;border-style:none}textarea{resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{float:left;width:100%;padding:0;margin-bottom:.5rem;font-size:calc(1.275rem + .3vw);line-height:inherit}@media (min-width:1200px){legend{font-size:1.5rem}}legend+*{clear:left}::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-fields-wrapper,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-text,::-webkit-datetime-edit-year-field{padding:0}::-webkit-inner-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:textfield}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-color-swatch-wrapper{padding:0}::file-selector-button{font:inherit}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}iframe{border:0}summary{display:list-item;cursor:pointer}progress{vertical-align:baseline}[hidden]{display:none!important}</style> + <style>/*! syntax-highlighting.css */pre{line-height:125%;}span.linenos{color:inherit; background-color:transparent; padding-left:5px; padding-right:20px;}.pdoc-code .hll{background-color:#ffffcc}.pdoc-code{background:#f8f8f8;}.pdoc-code .c{color:#3D7B7B; font-style:italic}.pdoc-code .err{border:1px solid #FF0000}.pdoc-code .k{color:#008000; font-weight:bold}.pdoc-code .o{color:#666666}.pdoc-code .ch{color:#3D7B7B; font-style:italic}.pdoc-code .cm{color:#3D7B7B; font-style:italic}.pdoc-code .cp{color:#9C6500}.pdoc-code .cpf{color:#3D7B7B; font-style:italic}.pdoc-code .c1{color:#3D7B7B; font-style:italic}.pdoc-code .cs{color:#3D7B7B; font-style:italic}.pdoc-code .gd{color:#A00000}.pdoc-code .ge{font-style:italic}.pdoc-code .gr{color:#E40000}.pdoc-code .gh{color:#000080; font-weight:bold}.pdoc-code .gi{color:#008400}.pdoc-code .go{color:#717171}.pdoc-code .gp{color:#000080; font-weight:bold}.pdoc-code .gs{font-weight:bold}.pdoc-code .gu{color:#800080; font-weight:bold}.pdoc-code .gt{color:#0044DD}.pdoc-code .kc{color:#008000; font-weight:bold}.pdoc-code .kd{color:#008000; font-weight:bold}.pdoc-code .kn{color:#008000; font-weight:bold}.pdoc-code .kp{color:#008000}.pdoc-code .kr{color:#008000; font-weight:bold}.pdoc-code .kt{color:#B00040}.pdoc-code .m{color:#666666}.pdoc-code .s{color:#BA2121}.pdoc-code .na{color:#687822}.pdoc-code .nb{color:#008000}.pdoc-code .nc{color:#0000FF; font-weight:bold}.pdoc-code .no{color:#880000}.pdoc-code .nd{color:#AA22FF}.pdoc-code .ni{color:#717171; font-weight:bold}.pdoc-code .ne{color:#CB3F38; font-weight:bold}.pdoc-code .nf{color:#0000FF}.pdoc-code .nl{color:#767600}.pdoc-code .nn{color:#0000FF; font-weight:bold}.pdoc-code .nt{color:#008000; font-weight:bold}.pdoc-code .nv{color:#19177C}.pdoc-code .ow{color:#AA22FF; font-weight:bold}.pdoc-code .w{color:#bbbbbb}.pdoc-code .mb{color:#666666}.pdoc-code .mf{color:#666666}.pdoc-code .mh{color:#666666}.pdoc-code .mi{color:#666666}.pdoc-code .mo{color:#666666}.pdoc-code .sa{color:#BA2121}.pdoc-code .sb{color:#BA2121}.pdoc-code .sc{color:#BA2121}.pdoc-code .dl{color:#BA2121}.pdoc-code .sd{color:#BA2121; font-style:italic}.pdoc-code .s2{color:#BA2121}.pdoc-code .se{color:#AA5D1F; font-weight:bold}.pdoc-code .sh{color:#BA2121}.pdoc-code .si{color:#A45A77; font-weight:bold}.pdoc-code .sx{color:#008000}.pdoc-code .sr{color:#A45A77}.pdoc-code .s1{color:#BA2121}.pdoc-code .ss{color:#19177C}.pdoc-code .bp{color:#008000}.pdoc-code .fm{color:#0000FF}.pdoc-code .vc{color:#19177C}.pdoc-code .vg{color:#19177C}.pdoc-code .vi{color:#19177C}.pdoc-code .vm{color:#19177C}.pdoc-code .il{color:#666666}</style> + <style>/*! theme.css */:root{--pdoc-background:#fff;}.pdoc{--text:#212529;--muted:#6c757d;--link:#3660a5;--link-hover:#1659c5;--code:#f8f8f8;--active:#fff598;--accent:#eee;--accent2:#c1c1c1;--nav-hover:rgba(255, 255, 255, 0.5);--name:#0066BB;--def:#008800;--annotation:#007020;}</style> + <style>/*! layout.css */html, body{width:100%;height:100%;}html, main{scroll-behavior:smooth;}body{background-color:var(--pdoc-background);}@media (max-width:769px){#navtoggle{cursor:pointer;position:absolute;width:50px;height:40px;top:1rem;right:1rem;border-color:var(--text);color:var(--text);display:flex;opacity:0.8;}#navtoggle:hover{opacity:1;}#togglestate + div{display:none;}#togglestate:checked + div{display:inherit;}main, header{padding:2rem 3vw;}header + main{margin-top:-3rem;}.git-button{display:none !important;}nav input[type="search"]{max-width:77%;}nav input[type="search"]:first-child{margin-top:-6px;}nav input[type="search"]:valid ~ *{display:none !important;}}@media (min-width:770px){:root{--sidebar-width:clamp(12.5rem, 28vw, 22rem);}nav{position:fixed;overflow:auto;height:100vh;width:var(--sidebar-width);}main, header{padding:3rem 2rem 3rem calc(var(--sidebar-width) + 3rem);width:calc(54rem + var(--sidebar-width));max-width:100%;}header + main{margin-top:-4rem;}#navtoggle{display:none;}}#togglestate{position:absolute;height:0;opacity:0;}nav.pdoc{--pad:clamp(0.5rem, 2vw, 1.75rem);--indent:1.5rem;background-color:var(--accent);border-right:1px solid var(--accent2);box-shadow:0 0 20px rgba(50, 50, 50, .2) inset;padding:0 0 0 var(--pad);overflow-wrap:anywhere;scrollbar-width:thin; scrollbar-color:var(--accent2) transparent }nav.pdoc::-webkit-scrollbar{width:.4rem; }nav.pdoc::-webkit-scrollbar-thumb{background-color:var(--accent2); }nav.pdoc > div{padding:var(--pad) 0;}nav.pdoc .module-list-button{display:inline-flex;align-items:center;color:var(--text);border-color:var(--muted);margin-bottom:1rem;}nav.pdoc .module-list-button:hover{border-color:var(--text);}nav.pdoc input[type=search]{display:block;outline-offset:0;width:calc(100% - var(--pad));}nav.pdoc .logo{max-width:calc(100% - var(--pad));max-height:35vh;display:block;margin:0 auto 1rem;transform:translate(calc(-.5 * var(--pad)), 0);}nav.pdoc ul{list-style:none;padding-left:0;}nav.pdoc > div > ul{margin-left:calc(0px - var(--pad));}nav.pdoc li a{padding:.2rem 0 .2rem calc(var(--pad) + var(--indent));}nav.pdoc > div > ul > li > a{padding-left:var(--pad);}nav.pdoc li{transition:all 100ms;}nav.pdoc li:hover{background-color:var(--nav-hover);}nav.pdoc a, nav.pdoc a:hover{color:var(--text);}nav.pdoc a{display:block;}nav.pdoc > h2:first-of-type{margin-top:1.5rem;}nav.pdoc .class:before{content:"class ";color:var(--muted);}nav.pdoc .function:after{content:"()";color:var(--muted);}nav.pdoc footer:before{content:"";display:block;width:calc(100% - var(--pad));border-top:solid var(--accent2) 1px;margin-top:1.5rem;padding-top:.5rem;}nav.pdoc footer{font-size:small;}</style> + <style>/*! content.css */.pdoc{color:var(--text);box-sizing:border-box;line-height:1.5;background:none;}.pdoc .pdoc-button{display:inline-block;border:solid black 1px;border-radius:2px;font-size:.75rem;padding:calc(0.5em - 1px) 1em;transition:100ms all;}.pdoc .pdoc-alert{padding:1rem 1rem 1rem calc(1.5rem + 24px);border:1px solid transparent;border-radius:.25rem;background-repeat:no-repeat;background-position:1rem center;margin-bottom:1rem;}.pdoc .pdoc-alert > *:last-child{margin-bottom:0;}.pdoc .pdoc-alert-note {color:#084298;background-color:#cfe2ff;border-color:#b6d4fe;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23084298%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2016A8%208%200%201%200%208%200a8%208%200%200%200%200%2016zm.93-9.412-1%204.705c-.07.34.029.533.304.533.194%200%20.487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703%200-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381%202.29-.287zM8%205.5a1%201%200%201%201%200-2%201%201%200%200%201%200%202z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-warning{color:#664d03;background-color:#fff3cd;border-color:#ffecb5;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23664d03%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8.982%201.566a1.13%201.13%200%200%200-1.96%200L.165%2013.233c-.457.778.091%201.767.98%201.767h13.713c.889%200%201.438-.99.98-1.767L8.982%201.566zM8%205c.535%200%20.954.462.9.995l-.35%203.507a.552.552%200%200%201-1.1%200L7.1%205.995A.905.905%200%200%201%208%205zm.002%206a1%201%200%201%201%200%202%201%201%200%200%201%200-2z%22/%3E%3C/svg%3E");}.pdoc .pdoc-alert-danger{color:#842029;background-color:#f8d7da;border-color:#f5c2c7;background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2224%22%20height%3D%2224%22%20fill%3D%22%23842029%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M5.52.359A.5.5%200%200%201%206%200h4a.5.5%200%200%201%20.474.658L8.694%206H12.5a.5.5%200%200%201%20.395.807l-7%209a.5.5%200%200%201-.873-.454L6.823%209.5H3.5a.5.5%200%200%201-.48-.641l2.5-8.5z%22/%3E%3C/svg%3E");}.pdoc .visually-hidden{position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;white-space:nowrap !important;border:0 !important;}.pdoc h1, .pdoc h2, .pdoc h3{font-weight:300;margin:.3em 0;padding:.2em 0;}.pdoc > section:not(.module-info) h1{font-size:1.5rem;font-weight:500;}.pdoc > section:not(.module-info) h2{font-size:1.4rem;font-weight:500;}.pdoc > section:not(.module-info) h3{font-size:1.3rem;font-weight:500;}.pdoc > section:not(.module-info) h4{font-size:1.2rem;}.pdoc > section:not(.module-info) h5{font-size:1.1rem;}.pdoc a{text-decoration:none;color:var(--link);}.pdoc a:hover{color:var(--link-hover);}.pdoc blockquote{margin-left:2rem;}.pdoc pre{border-top:1px solid var(--accent2);border-bottom:1px solid var(--accent2);margin-top:0;margin-bottom:1em;padding:.5rem 0 .5rem .5rem;overflow-x:auto;background-color:var(--code);}.pdoc code{color:var(--text);padding:.2em .4em;margin:0;font-size:85%;background-color:var(--code);border-radius:6px;}.pdoc a > code{color:inherit;}.pdoc pre > code{display:inline-block;font-size:inherit;background:none;border:none;padding:0;}.pdoc > section:not(.module-info){margin-bottom:1.5rem;}.pdoc .modulename{margin-top:0;font-weight:bold;}.pdoc .modulename a{color:var(--link);transition:100ms all;}.pdoc .git-button{float:right;border:solid var(--link) 1px;}.pdoc .git-button:hover{background-color:var(--link);color:var(--pdoc-background);}.view-source-toggle-state,.view-source-toggle-state ~ .pdoc-code{display:none;}.view-source-toggle-state:checked ~ .pdoc-code{display:block;}.view-source-button{display:inline-block;float:right;font-size:.75rem;line-height:1.5rem;color:var(--muted);padding:0 .4rem 0 1.3rem;cursor:pointer;text-indent:-2px;}.view-source-button > span{visibility:hidden;}.module-info .view-source-button{float:none;display:flex;justify-content:flex-end;margin:-1.2rem .4rem -.2rem 0;}.view-source-button::before{position:absolute;content:"View Source";display:list-item;list-style-type:disclosure-closed;}.view-source-toggle-state:checked ~ .attr .view-source-button::before,.view-source-toggle-state:checked ~ .view-source-button::before{list-style-type:disclosure-open;}.pdoc .docstring{margin-bottom:1.5rem;}.pdoc section:not(.module-info) .docstring{margin-left:clamp(0rem, 5vw - 2rem, 1rem);}.pdoc .docstring .pdoc-code{margin-left:1em;margin-right:1em;}.pdoc h1:target,.pdoc h2:target,.pdoc h3:target,.pdoc h4:target,.pdoc h5:target,.pdoc h6:target,.pdoc .pdoc-code > pre > span:target{background-color:var(--active);box-shadow:-1rem 0 0 0 var(--active);}.pdoc .pdoc-code > pre > span:target{display:block;}.pdoc div:target > .attr,.pdoc section:target > .attr,.pdoc dd:target > a{background-color:var(--active);}.pdoc *{scroll-margin:2rem;}.pdoc .pdoc-code .linenos{user-select:none;}.pdoc .attr:hover{filter:contrast(0.95);}.pdoc section, .pdoc .classattr{position:relative;}.pdoc .headerlink{--width:clamp(1rem, 3vw, 2rem);position:absolute;top:0;left:calc(0rem - var(--width));transition:all 100ms ease-in-out;opacity:0;}.pdoc .headerlink::before{content:"#";display:block;text-align:center;width:var(--width);height:2.3rem;line-height:2.3rem;font-size:1.5rem;}.pdoc .attr:hover ~ .headerlink,.pdoc *:target > .headerlink,.pdoc .headerlink:hover{opacity:1;}.pdoc .attr{display:block;margin:.5rem 0 .5rem;padding:.4rem .4rem .4rem 1rem;background-color:var(--accent);overflow-x:auto;}.pdoc .classattr{margin-left:2rem;}.pdoc .name{color:var(--name);font-weight:bold;}.pdoc .def{color:var(--def);font-weight:bold;}.pdoc .signature{background-color:transparent;}.pdoc .param, .pdoc .return-annotation{white-space:pre;}.pdoc .signature.multiline .param{display:block;}.pdoc .signature.condensed .param{display:inline-block;}.pdoc .annotation{color:var(--annotation);}.pdoc .inherited{margin-left:2rem;}.pdoc .inherited dt{font-weight:700;}.pdoc .inherited dt, .pdoc .inherited dd{display:inline;margin-left:0;margin-bottom:.5rem;}.pdoc .inherited dd:not(:last-child):after{content:", ";}.pdoc .inherited .class:before{content:"class ";}.pdoc .inherited .function a:after{content:"()";}.pdoc .search-result .docstring{overflow:auto;max-height:25vh;}.pdoc .search-result.focused > .attr{background-color:var(--active);}.pdoc .attribution{margin-top:2rem;display:block;opacity:0.5;transition:all 200ms;filter:grayscale(100%);}.pdoc .attribution:hover{opacity:1;filter:grayscale(0%);}.pdoc .attribution img{margin-left:5px;height:35px;vertical-align:middle;width:70px;transition:all 200ms;}.pdoc table{display:block;width:max-content;max-width:100%;overflow:auto;margin-bottom:1rem;}.pdoc table th{font-weight:600;}.pdoc table th, .pdoc table td{padding:6px 13px;border:1px solid var(--accent2);}</style> + <style>/*! custom.css */</style></head> +<body> + <nav class="pdoc"> + <label id="navtoggle" for="togglestate" class="pdoc-button"><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke-linecap='round' stroke="currentColor" stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg></label> + <input id="togglestate" type="checkbox" aria-hidden="true" tabindex="-1"> + <div> <a class="pdoc-button module-list-button" href="../optimizer.html"> +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-in-left" viewBox="0 0 16 16"> + <path fill-rule="evenodd" d="M10 3.5a.5.5 0 0 0-.5-.5h-8a.5.5 0 0 0-.5.5v9a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-2a.5.5 0 0 1 1 0v2A1.5 1.5 0 0 1 9.5 14h-8A1.5 1.5 0 0 1 0 12.5v-9A1.5 1.5 0 0 1 1.5 2h8A1.5 1.5 0 0 1 11 3.5v2a.5.5 0 0 1-1 0v-2z"/> + <path fill-rule="evenodd" d="M4.146 8.354a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H14.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3z"/> +</svg> sqlglot.optimizer</a> + + + <input type="search" placeholder="Search..." role="searchbox" aria-label="search" + pattern=".+" required> + + + + <h2>API Documentation</h2> + <ul class="memberlist"> + <li> + <a class="function" href="#unnest_subqueries">unnest_subqueries</a> + </li> + <li> + <a class="function" href="#unnest">unnest</a> + </li> + <li> + <a class="function" href="#decorrelate">decorrelate</a> + </li> + </ul> + + + <footer>Copyright (c) 2023 Toby Mao</footer> + + <a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank"> + built with <span class="visually-hidden">pdoc</span><img + alt="pdoc logo" + src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20role%3D%22img%22%20aria-label%3D%22pdoc%20logo%22%20width%3D%22300%22%20height%3D%22150%22%20viewBox%3D%22-1%200%2060%2030%22%3E%3Ctitle%3Epdoc%3C/title%3E%3Cpath%20d%3D%22M29.621%2021.293c-.011-.273-.214-.475-.511-.481a.5.5%200%200%200-.489.503l-.044%201.393c-.097.551-.695%201.215-1.566%201.704-.577.428-1.306.486-2.193.182-1.426-.617-2.467-1.654-3.304-2.487l-.173-.172a3.43%203.43%200%200%200-.365-.306.49.49%200%200%200-.286-.196c-1.718-1.06-4.931-1.47-7.353.191l-.219.15c-1.707%201.187-3.413%202.131-4.328%201.03-.02-.027-.49-.685-.141-1.763.233-.721.546-2.408.772-4.076.042-.09.067-.187.046-.288.166-1.347.277-2.625.241-3.351%201.378-1.008%202.271-2.586%202.271-4.362%200-.976-.272-1.935-.788-2.774-.057-.094-.122-.18-.184-.268.033-.167.052-.339.052-.516%200-1.477-1.202-2.679-2.679-2.679-.791%200-1.496.352-1.987.9a6.3%206.3%200%200%200-1.001.029c-.492-.564-1.207-.929-2.012-.929-1.477%200-2.679%201.202-2.679%202.679A2.65%202.65%200%200%200%20.97%206.554c-.383.747-.595%201.572-.595%202.41%200%202.311%201.507%204.29%203.635%205.107-.037.699-.147%202.27-.423%203.294l-.137.461c-.622%202.042-2.515%208.257%201.727%2010.643%201.614.908%203.06%201.248%204.317%201.248%202.665%200%204.492-1.524%205.322-2.401%201.476-1.559%202.886-1.854%206.491.82%201.877%201.393%203.514%201.753%204.861%201.068%202.223-1.713%202.811-3.867%203.399-6.374.077-.846.056-1.469.054-1.537zm-4.835%204.313c-.054.305-.156.586-.242.629-.034-.007-.131-.022-.307-.157-.145-.111-.314-.478-.456-.908.221.121.432.25.675.355.115.039.219.051.33.081zm-2.251-1.238c-.05.33-.158.648-.252.694-.022.001-.125-.018-.307-.157-.217-.166-.488-.906-.639-1.573.358.344.754.693%201.198%201.036zm-3.887-2.337c-.006-.116-.018-.231-.041-.342.635.145%201.189.368%201.599.625.097.231.166.481.174.642-.03.049-.055.101-.067.158-.046.013-.128.026-.298.004-.278-.037-.901-.57-1.367-1.087zm-1.127-.497c.116.306.176.625.12.71-.019.014-.117.045-.345.016-.206-.027-.604-.332-.986-.695.41-.051.816-.056%201.211-.031zm-4.535%201.535c.209.22.379.47.358.598-.006.041-.088.138-.351.234-.144.055-.539-.063-.979-.259a11.66%2011.66%200%200%200%20.972-.573zm.983-.664c.359-.237.738-.418%201.126-.554.25.237.479.548.457.694-.006.042-.087.138-.351.235-.174.064-.694-.105-1.232-.375zm-3.381%201.794c-.022.145-.061.29-.149.401-.133.166-.358.248-.69.251h-.002c-.133%200-.306-.26-.45-.621.417.091.854.07%201.291-.031zm-2.066-8.077a4.78%204.78%200%200%201-.775-.584c.172-.115.505-.254.88-.378l-.105.962zm-.331%202.302a10.32%2010.32%200%200%201-.828-.502c.202-.143.576-.328.984-.49l-.156.992zm-.45%202.157l-.701-.403c.214-.115.536-.249.891-.376a11.57%2011.57%200%200%201-.19.779zm-.181%201.716c.064.398.194.702.298.893-.194-.051-.435-.162-.736-.398.061-.119.224-.3.438-.495zM8.87%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zm-.735-.389a1.15%201.15%200%200%200-.314.783%201.16%201.16%200%200%200%201.162%201.162c.457%200%20.842-.27%201.032-.653.026.117.042.238.042.362a1.68%201.68%200%200%201-1.679%201.679%201.68%201.68%200%200%201-1.679-1.679c0-.843.626-1.535%201.436-1.654zM5.059%205.406A1.68%201.68%200%200%201%203.38%207.085a1.68%201.68%200%200%201-1.679-1.679c0-.037.009-.072.011-.109.21.3.541.508.935.508a1.16%201.16%200%200%200%201.162-1.162%201.14%201.14%200%200%200-.474-.912c.015%200%20.03-.005.045-.005.926.001%201.679.754%201.679%201.68zM3.198%204.141c0%20.152-.123.276-.276.276s-.275-.124-.275-.276.123-.276.276-.276.275.124.275.276zM1.375%208.964c0-.52.103-1.035.288-1.52.466.394%201.06.64%201.717.64%201.144%200%202.116-.725%202.499-1.738.383%201.012%201.355%201.738%202.499%201.738.867%200%201.631-.421%202.121-1.062.307.605.478%201.267.478%201.942%200%202.486-2.153%204.51-4.801%204.51s-4.801-2.023-4.801-4.51zm24.342%2019.349c-.985.498-2.267.168-3.813-.979-3.073-2.281-5.453-3.199-7.813-.705-1.315%201.391-4.163%203.365-8.423.97-3.174-1.786-2.239-6.266-1.261-9.479l.146-.492c.276-1.02.395-2.457.444-3.268a6.11%206.11%200%200%200%201.18.115%206.01%206.01%200%200%200%202.536-.562l-.006.175c-.802.215-1.848.612-2.021%201.25-.079.295.021.601.274.837.219.203.415.364.598.501-.667.304-1.243.698-1.311%201.179-.02.144-.022.507.393.787.213.144.395.26.564.365-1.285.521-1.361.96-1.381%201.126-.018.142-.011.496.427.746l.854.489c-.473.389-.971.914-.999%201.429-.018.278.095.532.316.713.675.556%201.231.721%201.653.721.059%200%20.104-.014.158-.02.207.707.641%201.64%201.513%201.64h.013c.8-.008%201.236-.345%201.462-.626.173-.216.268-.457.325-.692.424.195.93.374%201.372.374.151%200%20.294-.021.423-.068.732-.27.944-.704.993-1.021.009-.061.003-.119.002-.179.266.086.538.147.789.147.15%200%20.294-.021.423-.069.542-.2.797-.489.914-.754.237.147.478.258.704.288.106.014.205.021.296.021.356%200%20.595-.101.767-.229.438.435%201.094.992%201.656%201.067.106.014.205.021.296.021a1.56%201.56%200%200%200%20.323-.035c.17.575.453%201.289.866%201.605.358.273.665.362.914.362a.99.99%200%200%200%20.421-.093%201.03%201.03%200%200%200%20.245-.164c.168.428.39.846.68%201.068.358.273.665.362.913.362a.99.99%200%200%200%20.421-.093c.317-.148.512-.448.639-.762.251.157.495.257.726.257.127%200%20.25-.024.37-.071.427-.17.706-.617.841-1.314.022-.015.047-.022.068-.038.067-.051.133-.104.196-.159-.443%201.486-1.107%202.761-2.086%203.257zM8.66%209.925a.5.5%200%201%200-1%200c0%20.653-.818%201.205-1.787%201.205s-1.787-.552-1.787-1.205a.5.5%200%201%200-1%200c0%201.216%201.25%202.205%202.787%202.205s2.787-.989%202.787-2.205zm4.4%2015.965l-.208.097c-2.661%201.258-4.708%201.436-6.086.527-1.542-1.017-1.88-3.19-1.844-4.198a.4.4%200%200%200-.385-.414c-.242-.029-.406.164-.414.385-.046%201.249.367%203.686%202.202%204.896.708.467%201.547.7%202.51.7%201.248%200%202.706-.392%204.362-1.174l.185-.086a.4.4%200%200%200%20.205-.527c-.089-.204-.326-.291-.527-.206zM9.547%202.292c.093.077.205.114.317.114a.5.5%200%200%200%20.318-.886L8.817.397a.5.5%200%200%200-.703.068.5.5%200%200%200%20.069.703l1.364%201.124zm-7.661-.065c.086%200%20.173-.022.253-.068l1.523-.893a.5.5%200%200%200-.506-.863l-1.523.892a.5.5%200%200%200-.179.685c.094.158.261.247.432.247z%22%20transform%3D%22matrix%28-1%200%200%201%2058%200%29%22%20fill%3D%22%233bb300%22/%3E%3Cpath%20d%3D%22M.3%2021.86V10.18q0-.46.02-.68.04-.22.18-.5.28-.54%201.34-.54%201.06%200%201.42.28.38.26.44.78.76-1.04%202.38-1.04%201.64%200%203.1%201.54%201.46%201.54%201.46%203.58%200%202.04-1.46%203.58-1.44%201.54-3.08%201.54-1.64%200-2.38-.92v4.04q0%20.46-.04.68-.02.22-.18.5-.14.3-.5.42-.36.12-.98.12-.62%200-1-.12-.36-.12-.52-.4-.14-.28-.18-.5-.02-.22-.02-.68zm3.96-9.42q-.46.54-.46%201.18%200%20.64.46%201.18.48.52%201.2.52.74%200%201.24-.52.52-.52.52-1.18%200-.66-.48-1.18-.48-.54-1.26-.54-.76%200-1.22.54zm14.741-8.36q.16-.3.54-.42.38-.12%201-.12.64%200%201.02.12.38.12.52.42.16.3.18.54.04.22.04.68v11.94q0%20.46-.04.7-.02.22-.18.5-.3.54-1.7.54-1.38%200-1.54-.98-.84.96-2.34.96-1.8%200-3.28-1.56-1.48-1.58-1.48-3.66%200-2.1%201.48-3.68%201.5-1.58%203.28-1.58%201.48%200%202.3%201v-4.2q0-.46.02-.68.04-.24.18-.52zm-3.24%2010.86q.52.54%201.26.54.74%200%201.22-.54.5-.54.5-1.18%200-.66-.48-1.22-.46-.56-1.26-.56-.8%200-1.28.56-.48.54-.48%201.2%200%20.66.52%201.2zm7.833-1.2q0-2.4%201.68-3.96%201.68-1.56%203.84-1.56%202.16%200%203.82%201.56%201.66%201.54%201.66%203.94%200%201.66-.86%202.96-.86%201.28-2.1%201.9-1.22.6-2.54.6-1.32%200-2.56-.64-1.24-.66-2.1-1.92-.84-1.28-.84-2.88zm4.18%201.44q.64.48%201.3.48.66%200%201.32-.5.66-.5.66-1.48%200-.98-.62-1.46-.62-.48-1.34-.48-.72%200-1.34.5-.62.5-.62%201.48%200%20.96.64%201.46zm11.412-1.44q0%20.84.56%201.32.56.46%201.18.46.64%200%201.18-.36.56-.38.9-.38.6%200%201.46%201.06.46.58.46%201.04%200%20.76-1.1%201.42-1.14.8-2.8.8-1.86%200-3.58-1.34-.82-.64-1.34-1.7-.52-1.08-.52-2.36%200-1.3.52-2.34.52-1.06%201.34-1.7%201.66-1.32%203.54-1.32.76%200%201.48.22.72.2%201.06.4l.32.2q.36.24.56.38.52.4.52.92%200%20.5-.42%201.14-.72%201.1-1.38%201.1-.38%200-1.08-.44-.36-.34-1.04-.34-.66%200-1.24.48-.58.48-.58%201.34z%22%20fill%3D%22green%22/%3E%3C/svg%3E"/> + </a> +</div> + </nav> + <main class="pdoc"> + <section class="module-info"> + <a class="pdoc-button git-button" href="https://github.com/tobymao/sqlglot/tree/main/sqlglot/optimizer/unnest_subqueries.py">Edit on GitHub</a> + <h1 class="modulename"> +<a href="./../../sqlglot.html">sqlglot</a><wbr>.<a href="./../optimizer.html">optimizer</a><wbr>.unnest_subqueries </h1> + + + <input id="mod-unnest_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> + + <label class="view-source-button" for="mod-unnest_subqueries-view-source"><span>View Source</span></label> + + <div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">itertools</span> +</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a> +</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">from</span> <span class="nn">sqlglot</span> <span class="kn">import</span> <span class="n">exp</span> +</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="kn">from</span> <span class="nn">sqlglot.optimizer.scope</span> <span class="kn">import</span> <span class="n">ScopeType</span><span class="p">,</span> <span class="n">traverse_scope</span> +</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a> +</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a> +</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="k">def</span> <span class="nf">unnest_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Rewrite sqlglot AST to convert some predicates with subqueries into joins.</span> +</span><span id="L-10"><a href="#L-10"><span class="linenos"> 10</span></a> +</span><span id="L-11"><a href="#L-11"><span class="linenos"> 11</span></a><span class="sd"> Convert scalar subqueries into cross joins.</span> +</span><span id="L-12"><a href="#L-12"><span class="linenos"> 12</span></a><span class="sd"> Convert correlated or vectorized subqueries into a group by so it is not a many to many left join.</span> +</span><span id="L-13"><a href="#L-13"><span class="linenos"> 13</span></a> +</span><span id="L-14"><a href="#L-14"><span class="linenos"> 14</span></a><span class="sd"> Example:</span> +</span><span id="L-15"><a href="#L-15"><span class="linenos"> 15</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="L-16"><a href="#L-16"><span class="linenos"> 16</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT * FROM x AS x WHERE (SELECT y.a AS a FROM y AS y WHERE x.a = y.a) = 1 ")</span> +</span><span id="L-17"><a href="#L-17"><span class="linenos"> 17</span></a><span class="sd"> >>> unnest_subqueries(expression).sql()</span> +</span><span id="L-18"><a href="#L-18"><span class="linenos"> 18</span></a><span class="sd"> 'SELECT * FROM x AS x LEFT JOIN (SELECT y.a AS a FROM y AS y WHERE TRUE GROUP BY y.a) AS _u_0 ON x.a = _u_0.a WHERE _u_0.a = 1'</span> +</span><span id="L-19"><a href="#L-19"><span class="linenos"> 19</span></a> +</span><span id="L-20"><a href="#L-20"><span class="linenos"> 20</span></a><span class="sd"> Args:</span> +</span><span id="L-21"><a href="#L-21"><span class="linenos"> 21</span></a><span class="sd"> expression (sqlglot.Expression): expression to unnest</span> +</span><span id="L-22"><a href="#L-22"><span class="linenos"> 22</span></a><span class="sd"> Returns:</span> +</span><span id="L-23"><a href="#L-23"><span class="linenos"> 23</span></a><span class="sd"> sqlglot.Expression: unnested expression</span> +</span><span id="L-24"><a href="#L-24"><span class="linenos"> 24</span></a><span class="sd"> """</span> +</span><span id="L-25"><a href="#L-25"><span class="linenos"> 25</span></a> <span class="n">sequence</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">()</span> +</span><span id="L-26"><a href="#L-26"><span class="linenos"> 26</span></a> +</span><span id="L-27"><a href="#L-27"><span class="linenos"> 27</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-28"><a href="#L-28"><span class="linenos"> 28</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span> +</span><span id="L-29"><a href="#L-29"><span class="linenos"> 29</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">parent_select</span> +</span><span id="L-30"><a href="#L-30"><span class="linenos"> 30</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">parent</span><span class="p">:</span> +</span><span id="L-31"><a href="#L-31"><span class="linenos"> 31</span></a> <span class="k">continue</span> +</span><span id="L-32"><a href="#L-32"><span class="linenos"> 32</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">:</span> +</span><span id="L-33"><a href="#L-33"><span class="linenos"> 33</span></a> <span class="n">decorrelate</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">,</span> <span class="n">sequence</span><span class="p">)</span> +</span><span id="L-34"><a href="#L-34"><span class="linenos"> 34</span></a> <span class="k">elif</span> <span class="n">scope</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">SUBQUERY</span><span class="p">:</span> +</span><span id="L-35"><a href="#L-35"><span class="linenos"> 35</span></a> <span class="n">unnest</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">sequence</span><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">return</span> <span class="n">expression</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><span id="L-40"><a href="#L-40"><span class="linenos"> 40</span></a><span class="k">def</span> <span class="nf">unnest</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent_select</span><span class="p">,</span> <span class="n">sequence</span><span class="p">):</span> +</span><span id="L-41"><a href="#L-41"><span class="linenos"> 41</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">select</span><span class="o">.</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-42"><a href="#L-42"><span class="linenos"> 42</span></a> <span class="k">return</span> +</span><span id="L-43"><a href="#L-43"><span class="linenos"> 43</span></a> +</span><span id="L-44"><a href="#L-44"><span class="linenos"> 44</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> +</span><span id="L-45"><a href="#L-45"><span class="linenos"> 45</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</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">if</span> <span class="ow">not</span> <span class="n">predicate</span> <span class="ow">or</span> <span class="n">parent_select</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">predicate</span><span class="o">.</span><span class="n">parent_select</span><span class="p">:</span> +</span><span id="L-48"><a href="#L-48"><span class="linenos"> 48</span></a> <span class="k">return</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="c1"># this subquery returns a scalar and can just be converted to a cross join</span> +</span><span id="L-51"><a href="#L-51"><span class="linenos"> 51</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">)):</span> +</span><span id="L-52"><a href="#L-52"><span class="linenos"> 52</span></a> <span class="n">having</span> <span class="o">=</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">)</span> +</span><span id="L-53"><a href="#L-53"><span class="linenos"> 53</span></a> <span class="n">column</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="n">select</span><span class="o">.</span><span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="L-54"><a href="#L-54"><span class="linenos"> 54</span></a> <span class="k">if</span> <span class="n">having</span> <span class="ow">and</span> <span class="n">having</span><span class="o">.</span><span class="n">parent_select</span> <span class="ow">is</span> <span class="n">parent_select</span><span class="p">:</span> +</span><span id="L-55"><a href="#L-55"><span class="linenos"> 55</span></a> <span class="n">column</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">column</span><span class="p">)</span> +</span><span id="L-56"><a href="#L-56"><span class="linenos"> 56</span></a> <span class="n">_replace</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">column</span><span class="p">)</span> +</span><span id="L-57"><a href="#L-57"><span class="linenos"> 57</span></a> +</span><span id="L-58"><a href="#L-58"><span class="linenos"> 58</span></a> <span class="n">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="L-59"><a href="#L-59"><span class="linenos"> 59</span></a> <span class="n">select</span><span class="p">,</span> +</span><span id="L-60"><a href="#L-60"><span class="linenos"> 60</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</span><span class="p">,</span> +</span><span id="L-61"><a href="#L-61"><span class="linenos"> 61</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="L-62"><a href="#L-62"><span class="linenos"> 62</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-63"><a href="#L-63"><span class="linenos"> 63</span></a> <span class="p">)</span> +</span><span id="L-64"><a href="#L-64"><span class="linenos"> 64</span></a> <span class="k">return</span> +</span><span id="L-65"><a href="#L-65"><span class="linenos"> 65</span></a> +</span><span id="L-66"><a href="#L-66"><span class="linenos"> 66</span></a> <span class="k">if</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Limit</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Offset</span><span class="p">):</span> +</span><span id="L-67"><a href="#L-67"><span class="linenos"> 67</span></a> <span class="k">return</span> +</span><span id="L-68"><a href="#L-68"><span class="linenos"> 68</span></a> +</span><span id="L-69"><a href="#L-69"><span class="linenos"> 69</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">):</span> +</span><span id="L-70"><a href="#L-70"><span class="linenos"> 70</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> +</span><span id="L-71"><a href="#L-71"><span class="linenos"> 71</span></a> +</span><span id="L-72"><a href="#L-72"><span class="linenos"> 72</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span> <span class="ow">or</span> <span class="n">parent_select</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">predicate</span><span class="o">.</span><span class="n">parent_select</span><span class="p">:</span> +</span><span id="L-73"><a href="#L-73"><span class="linenos"> 73</span></a> <span class="k">return</span> +</span><span id="L-74"><a href="#L-74"><span class="linenos"> 74</span></a> +</span><span id="L-75"><a href="#L-75"><span class="linenos"> 75</span></a> <span class="n">column</span> <span class="o">=</span> <span class="n">_other_operand</span><span class="p">(</span><span class="n">predicate</span><span class="p">)</span> +</span><span id="L-76"><a href="#L-76"><span class="linenos"> 76</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">select</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-77"><a href="#L-77"><span class="linenos"> 77</span></a> +</span><span id="L-78"><a href="#L-78"><span class="linenos"> 78</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">condition</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">column</span><span class="si">}</span><span class="s1"> = "</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s1">"."</span><span class="si">{</span><span class="n">value</span><span class="o">.</span><span class="n">alias</span><span class="si">}</span><span class="s1">"'</span><span class="p">)</span> +</span><span id="L-79"><a href="#L-79"><span class="linenos"> 79</span></a> <span class="n">_replace</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"NOT </span><span class="si">{</span><span class="n">on</span><span class="o">.</span><span class="n">right</span><span class="si">}</span><span class="s2"> IS NULL"</span><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">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="L-82"><a href="#L-82"><span class="linenos"> 82</span></a> <span class="n">select</span><span class="o">.</span><span class="n">group_by</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="L-83"><a href="#L-83"><span class="linenos"> 83</span></a> <span class="n">on</span><span class="o">=</span><span class="n">on</span><span class="p">,</span> +</span><span id="L-84"><a href="#L-84"><span class="linenos"> 84</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"LEFT"</span><span class="p">,</span> +</span><span id="L-85"><a href="#L-85"><span class="linenos"> 85</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="L-86"><a href="#L-86"><span class="linenos"> 86</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-87"><a href="#L-87"><span class="linenos"> 87</span></a> <span class="p">)</span> +</span><span id="L-88"><a href="#L-88"><span class="linenos"> 88</span></a> +</span><span id="L-89"><a href="#L-89"><span class="linenos"> 89</span></a> +</span><span id="L-90"><a href="#L-90"><span class="linenos"> 90</span></a><span class="k">def</span> <span class="nf">decorrelate</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent_select</span><span class="p">,</span> <span class="n">external_columns</span><span class="p">,</span> <span class="n">sequence</span><span class="p">):</span> +</span><span id="L-91"><a href="#L-91"><span class="linenos"> 91</span></a> <span class="n">where</span> <span class="o">=</span> <span class="n">select</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">"where"</span><span class="p">)</span> +</span><span id="L-92"><a href="#L-92"><span class="linenos"> 92</span></a> +</span><span id="L-93"><a href="#L-93"><span class="linenos"> 93</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">where</span> <span class="ow">or</span> <span class="n">where</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> <span class="ow">or</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Limit</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Offset</span><span class="p">):</span> +</span><span id="L-94"><a href="#L-94"><span class="linenos"> 94</span></a> <span class="k">return</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="n">table_alias</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span> +</span><span id="L-97"><a href="#L-97"><span class="linenos"> 97</span></a> <span class="n">keys</span> <span class="o">=</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="c1"># for all external columns in the where statement, find the relevant predicate</span> +</span><span id="L-100"><a href="#L-100"><span class="linenos">100</span></a> <span class="c1"># keys to convert it into a join</span> +</span><span id="L-101"><a href="#L-101"><span class="linenos">101</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">external_columns</span><span class="p">:</span> +</span><span id="L-102"><a href="#L-102"><span class="linenos">102</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">where</span><span class="p">:</span> +</span><span id="L-103"><a href="#L-103"><span class="linenos">103</span></a> <span class="k">return</span> +</span><span id="L-104"><a href="#L-104"><span class="linenos">104</span></a> +</span><span id="L-105"><a href="#L-105"><span class="linenos">105</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> +</span><span id="L-106"><a href="#L-106"><span class="linenos">106</span></a> +</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="n">predicate</span> <span class="ow">or</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">where</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><span id="L-109"><a href="#L-109"><span class="linenos">109</span></a> +</span><span id="L-110"><a href="#L-110"><span class="linenos">110</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="L-111"><a href="#L-111"><span class="linenos">111</span></a> <span class="n">key</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="L-112"><a href="#L-112"><span class="linenos">112</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-113"><a href="#L-113"><span class="linenos">113</span></a> <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">node</span> <span class="ow">is</span> <span class="n">column</span> <span class="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="o">*</span><span class="n">_</span> <span class="ow">in</span> <span class="n">predicate</span><span class="o">.</span><span class="n">left</span><span class="o">.</span><span class="n">walk</span><span class="p">())</span> +</span><span id="L-114"><a href="#L-114"><span class="linenos">114</span></a> <span class="k">else</span> <span class="n">predicate</span><span class="o">.</span><span class="n">left</span> +</span><span id="L-115"><a href="#L-115"><span class="linenos">115</span></a> <span class="p">)</span> +</span><span id="L-116"><a href="#L-116"><span class="linenos">116</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-117"><a href="#L-117"><span class="linenos">117</span></a> <span class="k">return</span> +</span><span id="L-118"><a href="#L-118"><span class="linenos">118</span></a> +</span><span id="L-119"><a href="#L-119"><span class="linenos">119</span></a> <span class="n">keys</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">key</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="n">predicate</span><span class="p">))</span> +</span><span id="L-120"><a href="#L-120"><span class="linenos">120</span></a> +</span><span id="L-121"><a href="#L-121"><span class="linenos">121</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> <span class="k">for</span> <span class="o">*</span><span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</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><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="n">is_subquery_projection</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="L-125"><a href="#L-125"><span class="linenos">125</span></a> <span class="n">node</span> <span class="ow">is</span> <span class="n">select</span><span class="o">.</span><span class="n">parent</span> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">parent_select</span><span class="o">.</span><span class="n">selects</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> +</span><span id="L-126"><a href="#L-126"><span class="linenos">126</span></a> <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="n">value</span> <span class="o">=</span> <span class="n">select</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-129"><a href="#L-129"><span class="linenos">129</span></a> <span class="n">key_aliases</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="L-130"><a href="#L-130"><span class="linenos">130</span></a> <span class="n">group_by</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-131"><a href="#L-131"><span class="linenos">131</span></a> +</span><span id="L-132"><a href="#L-132"><span class="linenos">132</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span> +</span><span id="L-133"><a href="#L-133"><span class="linenos">133</span></a> <span class="c1"># if we filter on the value of the subquery, it needs to be unique</span> +</span><span id="L-134"><a href="#L-134"><span class="linenos">134</span></a> <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">:</span> +</span><span id="L-135"><a href="#L-135"><span class="linenos">135</span></a> <span class="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">alias</span> +</span><span id="L-136"><a href="#L-136"><span class="linenos">136</span></a> <span class="n">group_by</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> +</span><span id="L-137"><a href="#L-137"><span class="linenos">137</span></a> <span class="k">else</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="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">key_aliases</span><span class="p">:</span> +</span><span id="L-139"><a href="#L-139"><span class="linenos">139</span></a> <span class="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span> +</span><span id="L-140"><a href="#L-140"><span class="linenos">140</span></a> <span class="c1"># all predicates that are equalities must also be in the unique</span> +</span><span id="L-141"><a href="#L-141"><span class="linenos">141</span></a> <span class="c1"># so that we don't do a many to many join</span> +</span><span id="L-142"><a href="#L-142"><span class="linenos">142</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-143"><a href="#L-143"><span class="linenos">143</span></a> <span class="n">group_by</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> +</span><span id="L-144"><a href="#L-144"><span class="linenos">144</span></a> +</span><span id="L-145"><a href="#L-145"><span class="linenos">145</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> +</span><span id="L-146"><a href="#L-146"><span class="linenos">146</span></a> +</span><span id="L-147"><a href="#L-147"><span class="linenos">147</span></a> <span class="c1"># if the value of the subquery is not an agg or a key, we need to collect it into an array</span> +</span><span id="L-148"><a href="#L-148"><span class="linenos">148</span></a> <span class="c1"># so that it can be grouped. For subquery projections, we use a MAX aggregation instead.</span> +</span><span id="L-149"><a href="#L-149"><span class="linenos">149</span></a> <span class="n">agg_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span> <span class="k">if</span> <span class="n">is_subquery_projection</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">ArrayAgg</span> +</span><span id="L-150"><a href="#L-150"><span class="linenos">150</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">)</span> <span class="ow">and</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-151"><a href="#L-151"><span class="linenos">151</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> +</span><span id="L-152"><a href="#L-152"><span class="linenos">152</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">agg_func</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">),</span> <span class="n">value</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">quoted</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="L-153"><a href="#L-153"><span class="linenos">153</span></a> <span class="n">append</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-154"><a href="#L-154"><span class="linenos">154</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-155"><a href="#L-155"><span class="linenos">155</span></a> <span class="p">)</span> +</span><span id="L-156"><a href="#L-156"><span class="linenos">156</span></a> +</span><span id="L-157"><a href="#L-157"><span class="linenos">157</span></a> <span class="c1"># exists queries should not have any selects as it only checks if there are any rows</span> +</span><span id="L-158"><a href="#L-158"><span class="linenos">158</span></a> <span class="c1"># all selects will be added by the optimizer and only used for join keys</span> +</span><span id="L-159"><a href="#L-159"><span class="linenos">159</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">):</span> +</span><span id="L-160"><a href="#L-160"><span class="linenos">160</span></a> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"expressions"</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="L-161"><a href="#L-161"><span class="linenos">161</span></a> +</span><span id="L-162"><a href="#L-162"><span class="linenos">162</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">key_aliases</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="L-163"><a href="#L-163"><span class="linenos">163</span></a> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-164"><a href="#L-164"><span class="linenos">164</span></a> <span class="c1"># add all keys to the projections of the subquery</span> +</span><span id="L-165"><a href="#L-165"><span class="linenos">165</span></a> <span class="c1"># so that we can use it as a join key</span> +</span><span id="L-166"><a href="#L-166"><span class="linenos">166</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">)</span> <span class="ow">or</span> <span class="n">key</span> <span class="o">!=</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">:</span> +</span><span id="L-167"><a href="#L-167"><span class="linenos">167</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s2"> AS </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-168"><a href="#L-168"><span class="linenos">168</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-169"><a href="#L-169"><span class="linenos">169</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">agg_func</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">key</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> <span class="n">alias</span><span class="p">,</span> <span class="n">quoted</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="L-170"><a href="#L-170"><span class="linenos">170</span></a> +</span><span id="L-171"><a href="#L-171"><span class="linenos">171</span></a> <span class="n">alias</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="n">value</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="L-172"><a href="#L-172"><span class="linenos">172</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">_other_operand</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">)</span> +</span><span id="L-173"><a href="#L-173"><span class="linenos">173</span></a> +</span><span id="L-174"><a href="#L-174"><span class="linenos">174</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">):</span> +</span><span id="L-175"><a href="#L-175"><span class="linenos">175</span></a> <span class="n">alias</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="nb">list</span><span class="p">(</span><span class="n">key_aliases</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="L-176"><a href="#L-176"><span class="linenos">176</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"NOT </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2"> IS NULL"</span><span class="p">)</span> +</span><span id="L-177"><a href="#L-177"><span class="linenos">177</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">All</span><span class="p">):</span> +</span><span id="L-178"><a href="#L-178"><span class="linenos">178</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="L-179"><a href="#L-179"><span class="linenos">179</span></a> <span class="n">parent_predicate</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"ARRAY_ALL(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2">)"</span> +</span><span id="L-180"><a href="#L-180"><span class="linenos">180</span></a> <span class="p">)</span> +</span><span id="L-181"><a href="#L-181"><span class="linenos">181</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">):</span> +</span><span id="L-182"><a href="#L-182"><span class="linenos">182</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-183"><a href="#L-183"><span class="linenos">183</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2"> = </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-184"><a href="#L-184"><span class="linenos">184</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-185"><a href="#L-185"><span class="linenos">185</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"ARRAY_ANY(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2">)"</span><span class="p">)</span> +</span><span id="L-186"><a href="#L-186"><span class="linenos">186</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">):</span> +</span><span id="L-187"><a href="#L-187"><span class="linenos">187</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-188"><a href="#L-188"><span class="linenos">188</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2"> = </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="L-189"><a href="#L-189"><span class="linenos">189</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-190"><a href="#L-190"><span class="linenos">190</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="L-191"><a href="#L-191"><span class="linenos">191</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="L-192"><a href="#L-192"><span class="linenos">192</span></a> <span class="sa">f</span><span class="s2">"ARRAY_ANY(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">parent_predicate</span><span class="o">.</span><span class="n">this</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="L-193"><a href="#L-193"><span class="linenos">193</span></a> <span class="p">)</span> +</span><span id="L-194"><a href="#L-194"><span class="linenos">194</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-195"><a href="#L-195"><span class="linenos">195</span></a> <span class="k">if</span> <span class="n">is_subquery_projection</span><span class="p">:</span> +</span><span id="L-196"><a href="#L-196"><span class="linenos">196</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias</span><span class="p">)</span> +</span><span id="L-197"><a href="#L-197"><span class="linenos">197</span></a> +</span><span id="L-198"><a href="#L-198"><span class="linenos">198</span></a> <span class="c1"># COUNT always returns 0 on empty datasets, so we need take that into consideration here</span> +</span><span id="L-199"><a href="#L-199"><span class="linenos">199</span></a> <span class="c1"># by transforming all counts into 0 and using that as the coalesced value</span> +</span><span id="L-200"><a href="#L-200"><span class="linenos">200</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span> +</span><span id="L-201"><a href="#L-201"><span class="linenos">201</span></a> +</span><span id="L-202"><a href="#L-202"><span class="linenos">202</span></a> <span class="k">def</span> <span class="nf">remove_aggs</span><span class="p">(</span><span class="n">node</span><span class="p">):</span> +</span><span id="L-203"><a href="#L-203"><span class="linenos">203</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span> +</span><span id="L-204"><a href="#L-204"><span class="linenos">204</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> +</span><span id="L-205"><a href="#L-205"><span class="linenos">205</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">):</span> +</span><span id="L-206"><a href="#L-206"><span class="linenos">206</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="L-207"><a href="#L-207"><span class="linenos">207</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="L-208"><a href="#L-208"><span class="linenos">208</span></a> +</span><span id="L-209"><a href="#L-209"><span class="linenos">209</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">(</span> +</span><span id="L-210"><a href="#L-210"><span class="linenos">210</span></a> <span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="L-211"><a href="#L-211"><span class="linenos">211</span></a> <span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">remove_aggs</span><span class="p">)],</span> +</span><span id="L-212"><a href="#L-212"><span class="linenos">212</span></a> <span class="p">)</span> +</span><span id="L-213"><a href="#L-213"><span class="linenos">213</span></a> +</span><span id="L-214"><a href="#L-214"><span class="linenos">214</span></a> <span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="L-215"><a href="#L-215"><span class="linenos">215</span></a> +</span><span id="L-216"><a href="#L-216"><span class="linenos">216</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span> +</span><span id="L-217"><a href="#L-217"><span class="linenos">217</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="L-218"><a href="#L-218"><span class="linenos">218</span></a> <span class="n">nested</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="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="L-219"><a href="#L-219"><span class="linenos">219</span></a> +</span><span id="L-220"><a href="#L-220"><span class="linenos">220</span></a> <span class="k">if</span> <span class="n">is_subquery_projection</span><span class="p">:</span> +</span><span id="L-221"><a href="#L-221"><span class="linenos">221</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">nested</span><span class="p">)</span> +</span><span id="L-222"><a href="#L-222"><span class="linenos">222</span></a> <span class="k">continue</span> +</span><span id="L-223"><a href="#L-223"><span class="linenos">223</span></a> +</span><span id="L-224"><a href="#L-224"><span class="linenos">224</span></a> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="L-225"><a href="#L-225"><span class="linenos">225</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">nested</span><span class="p">)</span> +</span><span id="L-226"><a href="#L-226"><span class="linenos">226</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">):</span> +</span><span id="L-227"><a href="#L-227"><span class="linenos">227</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="L-228"><a href="#L-228"><span class="linenos">228</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="L-229"><a href="#L-229"><span class="linenos">229</span></a> <span class="sa">f</span><span class="s2">"(</span><span class="si">{</span><span class="n">parent_predicate</span><span class="si">}</span><span class="s2"> AND ARRAY_CONTAINS(</span><span class="si">{</span><span class="n">nested</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">column</span><span class="si">}</span><span class="s2">))"</span><span class="p">,</span> +</span><span id="L-230"><a href="#L-230"><span class="linenos">230</span></a> <span class="p">)</span> +</span><span id="L-231"><a href="#L-231"><span class="linenos">231</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="L-232"><a href="#L-232"><span class="linenos">232</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="s2">"_x"</span><span class="p">))</span> +</span><span id="L-233"><a href="#L-233"><span class="linenos">233</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="L-234"><a href="#L-234"><span class="linenos">234</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="L-235"><a href="#L-235"><span class="linenos">235</span></a> <span class="sa">f</span><span class="s1">'(</span><span class="si">{</span><span class="n">parent_predicate</span><span class="si">}</span><span class="s1"> AND ARRAY_ANY(</span><span class="si">{</span><span class="n">nested</span><span class="si">}</span><span class="s1">, "_x" -> </span><span class="si">{</span><span class="n">predicate</span><span class="si">}</span><span class="s1">))'</span><span class="p">,</span> +</span><span id="L-236"><a href="#L-236"><span class="linenos">236</span></a> <span class="p">)</span> +</span><span id="L-237"><a href="#L-237"><span class="linenos">237</span></a> +</span><span id="L-238"><a href="#L-238"><span class="linenos">238</span></a> <span class="n">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="L-239"><a href="#L-239"><span class="linenos">239</span></a> <span class="n">select</span><span class="o">.</span><span class="n">group_by</span><span class="p">(</span><span class="o">*</span><span class="n">group_by</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="L-240"><a href="#L-240"><span class="linenos">240</span></a> <span class="n">on</span><span class="o">=</span><span class="p">[</span><span class="n">predicate</span> <span class="k">for</span> <span class="o">*</span><span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)],</span> +</span><span id="L-241"><a href="#L-241"><span class="linenos">241</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"LEFT"</span><span class="p">,</span> +</span><span id="L-242"><a href="#L-242"><span class="linenos">242</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">table_alias</span><span class="p">,</span> +</span><span id="L-243"><a href="#L-243"><span class="linenos">243</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="L-244"><a href="#L-244"><span class="linenos">244</span></a> <span class="p">)</span> +</span><span id="L-245"><a href="#L-245"><span class="linenos">245</span></a> +</span><span id="L-246"><a href="#L-246"><span class="linenos">246</span></a> +</span><span id="L-247"><a href="#L-247"><span class="linenos">247</span></a><span class="k">def</span> <span class="nf">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">):</span> +</span><span id="L-248"><a href="#L-248"><span class="linenos">248</span></a> <span class="k">return</span> <span class="sa">f</span><span class="s2">"_u_</span><span class="si">{</span><span class="nb">next</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> +</span><span id="L-249"><a href="#L-249"><span class="linenos">249</span></a> +</span><span id="L-250"><a href="#L-250"><span class="linenos">250</span></a> +</span><span id="L-251"><a href="#L-251"><span class="linenos">251</span></a><span class="k">def</span> <span class="nf">_replace</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">condition</span><span class="p">):</span> +</span><span id="L-252"><a href="#L-252"><span class="linenos">252</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">condition</span><span class="p">(</span><span class="n">condition</span><span class="p">))</span> +</span><span id="L-253"><a href="#L-253"><span class="linenos">253</span></a> +</span><span id="L-254"><a href="#L-254"><span class="linenos">254</span></a> +</span><span id="L-255"><a href="#L-255"><span class="linenos">255</span></a><span class="k">def</span> <span class="nf">_other_operand</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="L-256"><a href="#L-256"><span class="linenos">256</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">):</span> +</span><span id="L-257"><a href="#L-257"><span class="linenos">257</span></a> <span class="k">return</span> <span class="n">expression</span><span class="o">.</span><span class="n">this</span> +</span><span id="L-258"><a href="#L-258"><span class="linenos">258</span></a> +</span><span id="L-259"><a href="#L-259"><span class="linenos">259</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">All</span><span class="p">)):</span> +</span><span id="L-260"><a href="#L-260"><span class="linenos">260</span></a> <span class="k">return</span> <span class="n">_other_operand</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span> +</span><span id="L-261"><a href="#L-261"><span class="linenos">261</span></a> +</span><span id="L-262"><a href="#L-262"><span class="linenos">262</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="L-263"><a href="#L-263"><span class="linenos">263</span></a> <span class="k">return</span> <span class="p">(</span> +</span><span id="L-264"><a href="#L-264"><span class="linenos">264</span></a> <span class="n">expression</span><span class="o">.</span><span class="n">right</span> +</span><span id="L-265"><a href="#L-265"><span class="linenos">265</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">All</span><span class="p">))</span> +</span><span id="L-266"><a href="#L-266"><span class="linenos">266</span></a> <span class="k">else</span> <span class="n">expression</span><span class="o">.</span><span class="n">left</span> +</span><span id="L-267"><a href="#L-267"><span class="linenos">267</span></a> <span class="p">)</span> +</span><span id="L-268"><a href="#L-268"><span class="linenos">268</span></a> +</span><span id="L-269"><a href="#L-269"><span class="linenos">269</span></a> <span class="k">return</span> <span class="kc">None</span> +</span></pre></div> + + + </section> + <section id="unnest_subqueries"> + <input id="unnest_subqueries-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">unnest_subqueries</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expression</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="unnest_subqueries-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#unnest_subqueries"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="unnest_subqueries-8"><a href="#unnest_subqueries-8"><span class="linenos"> 8</span></a><span class="k">def</span> <span class="nf">unnest_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="unnest_subqueries-9"><a href="#unnest_subqueries-9"><span class="linenos"> 9</span></a><span class="w"> </span><span class="sd">"""</span> +</span><span id="unnest_subqueries-10"><a href="#unnest_subqueries-10"><span class="linenos">10</span></a><span class="sd"> Rewrite sqlglot AST to convert some predicates with subqueries into joins.</span> +</span><span id="unnest_subqueries-11"><a href="#unnest_subqueries-11"><span class="linenos">11</span></a> +</span><span id="unnest_subqueries-12"><a href="#unnest_subqueries-12"><span class="linenos">12</span></a><span class="sd"> Convert scalar subqueries into cross joins.</span> +</span><span id="unnest_subqueries-13"><a href="#unnest_subqueries-13"><span class="linenos">13</span></a><span class="sd"> Convert correlated or vectorized subqueries into a group by so it is not a many to many left join.</span> +</span><span id="unnest_subqueries-14"><a href="#unnest_subqueries-14"><span class="linenos">14</span></a> +</span><span id="unnest_subqueries-15"><a href="#unnest_subqueries-15"><span class="linenos">15</span></a><span class="sd"> Example:</span> +</span><span id="unnest_subqueries-16"><a href="#unnest_subqueries-16"><span class="linenos">16</span></a><span class="sd"> >>> import sqlglot</span> +</span><span id="unnest_subqueries-17"><a href="#unnest_subqueries-17"><span class="linenos">17</span></a><span class="sd"> >>> expression = sqlglot.parse_one("SELECT * FROM x AS x WHERE (SELECT y.a AS a FROM y AS y WHERE x.a = y.a) = 1 ")</span> +</span><span id="unnest_subqueries-18"><a href="#unnest_subqueries-18"><span class="linenos">18</span></a><span class="sd"> >>> unnest_subqueries(expression).sql()</span> +</span><span id="unnest_subqueries-19"><a href="#unnest_subqueries-19"><span class="linenos">19</span></a><span class="sd"> 'SELECT * FROM x AS x LEFT JOIN (SELECT y.a AS a FROM y AS y WHERE TRUE GROUP BY y.a) AS _u_0 ON x.a = _u_0.a WHERE _u_0.a = 1'</span> +</span><span id="unnest_subqueries-20"><a href="#unnest_subqueries-20"><span class="linenos">20</span></a> +</span><span id="unnest_subqueries-21"><a href="#unnest_subqueries-21"><span class="linenos">21</span></a><span class="sd"> Args:</span> +</span><span id="unnest_subqueries-22"><a href="#unnest_subqueries-22"><span class="linenos">22</span></a><span class="sd"> expression (sqlglot.Expression): expression to unnest</span> +</span><span id="unnest_subqueries-23"><a href="#unnest_subqueries-23"><span class="linenos">23</span></a><span class="sd"> Returns:</span> +</span><span id="unnest_subqueries-24"><a href="#unnest_subqueries-24"><span class="linenos">24</span></a><span class="sd"> sqlglot.Expression: unnested expression</span> +</span><span id="unnest_subqueries-25"><a href="#unnest_subqueries-25"><span class="linenos">25</span></a><span class="sd"> """</span> +</span><span id="unnest_subqueries-26"><a href="#unnest_subqueries-26"><span class="linenos">26</span></a> <span class="n">sequence</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">()</span> +</span><span id="unnest_subqueries-27"><a href="#unnest_subqueries-27"><span class="linenos">27</span></a> +</span><span id="unnest_subqueries-28"><a href="#unnest_subqueries-28"><span class="linenos">28</span></a> <span class="k">for</span> <span class="n">scope</span> <span class="ow">in</span> <span class="n">traverse_scope</span><span class="p">(</span><span class="n">expression</span><span class="p">):</span> +</span><span id="unnest_subqueries-29"><a href="#unnest_subqueries-29"><span class="linenos">29</span></a> <span class="n">select</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">expression</span> +</span><span id="unnest_subqueries-30"><a href="#unnest_subqueries-30"><span class="linenos">30</span></a> <span class="n">parent</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">parent_select</span> +</span><span id="unnest_subqueries-31"><a href="#unnest_subqueries-31"><span class="linenos">31</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">parent</span><span class="p">:</span> +</span><span id="unnest_subqueries-32"><a href="#unnest_subqueries-32"><span class="linenos">32</span></a> <span class="k">continue</span> +</span><span id="unnest_subqueries-33"><a href="#unnest_subqueries-33"><span class="linenos">33</span></a> <span class="k">if</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">:</span> +</span><span id="unnest_subqueries-34"><a href="#unnest_subqueries-34"><span class="linenos">34</span></a> <span class="n">decorrelate</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">scope</span><span class="o">.</span><span class="n">external_columns</span><span class="p">,</span> <span class="n">sequence</span><span class="p">)</span> +</span><span id="unnest_subqueries-35"><a href="#unnest_subqueries-35"><span class="linenos">35</span></a> <span class="k">elif</span> <span class="n">scope</span><span class="o">.</span><span class="n">scope_type</span> <span class="o">==</span> <span class="n">ScopeType</span><span class="o">.</span><span class="n">SUBQUERY</span><span class="p">:</span> +</span><span id="unnest_subqueries-36"><a href="#unnest_subqueries-36"><span class="linenos">36</span></a> <span class="n">unnest</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">sequence</span><span class="p">)</span> +</span><span id="unnest_subqueries-37"><a href="#unnest_subqueries-37"><span class="linenos">37</span></a> +</span><span id="unnest_subqueries-38"><a href="#unnest_subqueries-38"><span class="linenos">38</span></a> <span class="k">return</span> <span class="n">expression</span> +</span></pre></div> + + + <div class="docstring"><p>Rewrite sqlglot AST to convert some predicates with subqueries into joins.</p> + +<p>Convert scalar subqueries into cross joins. +Convert correlated or vectorized subqueries into a group by so it is not a many to many left join.</p> + +<h6 id="example">Example:</h6> + +<blockquote> + <div class="pdoc-code codehilite"> +<pre><span></span><code><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlglot</span> +<span class="gp">>>> </span><span class="n">expression</span> <span class="o">=</span> <span class="n"><a href="../../sqlglot.html#parse_one">sqlglot.parse_one</a></span><span class="p">(</span><span class="s2">"SELECT * FROM x AS x WHERE (SELECT y.a AS a FROM y AS y WHERE x.a = y.a) = 1 "</span><span class="p">)</span> +<span class="gp">>>> </span><span class="n">unnest_subqueries</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="o">.</span><span class="n">sql</span><span class="p">()</span> +<span class="go">'SELECT * FROM x AS x LEFT JOIN (SELECT y.a AS a FROM y AS y WHERE TRUE GROUP BY y.a) AS _u_0 ON x.a = _u_0.a WHERE _u_0.a = 1'</span> +</code></pre> + </div> +</blockquote> + +<h6 id="arguments">Arguments:</h6> + +<ul> +<li><strong>expression (sqlglot.Expression):</strong> expression to unnest</li> +</ul> + +<h6 id="returns">Returns:</h6> + +<blockquote> + <p>sqlglot.Expression: unnested expression</p> +</blockquote> +</div> + + + </section> + <section id="unnest"> + <input id="unnest-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">unnest</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">select</span>, </span><span class="param"><span class="n">parent_select</span>, </span><span class="param"><span class="n">sequence</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="unnest-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#unnest"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="unnest-41"><a href="#unnest-41"><span class="linenos">41</span></a><span class="k">def</span> <span class="nf">unnest</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent_select</span><span class="p">,</span> <span class="n">sequence</span><span class="p">):</span> +</span><span id="unnest-42"><a href="#unnest-42"><span class="linenos">42</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">select</span><span class="o">.</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="unnest-43"><a href="#unnest-43"><span class="linenos">43</span></a> <span class="k">return</span> +</span><span id="unnest-44"><a href="#unnest-44"><span class="linenos">44</span></a> +</span><span id="unnest-45"><a href="#unnest-45"><span class="linenos">45</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Condition</span><span class="p">)</span> +</span><span id="unnest-46"><a href="#unnest-46"><span class="linenos">46</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span> +</span><span id="unnest-47"><a href="#unnest-47"><span class="linenos">47</span></a> +</span><span id="unnest-48"><a href="#unnest-48"><span class="linenos">48</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span> <span class="ow">or</span> <span class="n">parent_select</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">predicate</span><span class="o">.</span><span class="n">parent_select</span><span class="p">:</span> +</span><span id="unnest-49"><a href="#unnest-49"><span class="linenos">49</span></a> <span class="k">return</span> +</span><span id="unnest-50"><a href="#unnest-50"><span class="linenos">50</span></a> +</span><span id="unnest-51"><a href="#unnest-51"><span class="linenos">51</span></a> <span class="c1"># this subquery returns a scalar and can just be converted to a cross join</span> +</span><span id="unnest-52"><a href="#unnest-52"><span class="linenos">52</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">)):</span> +</span><span id="unnest-53"><a href="#unnest-53"><span class="linenos">53</span></a> <span class="n">having</span> <span class="o">=</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Having</span><span class="p">)</span> +</span><span id="unnest-54"><a href="#unnest-54"><span class="linenos">54</span></a> <span class="n">column</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="n">select</span><span class="o">.</span><span class="n">selects</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">alias_or_name</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span> +</span><span id="unnest-55"><a href="#unnest-55"><span class="linenos">55</span></a> <span class="k">if</span> <span class="n">having</span> <span class="ow">and</span> <span class="n">having</span><span class="o">.</span><span class="n">parent_select</span> <span class="ow">is</span> <span class="n">parent_select</span><span class="p">:</span> +</span><span id="unnest-56"><a href="#unnest-56"><span class="linenos">56</span></a> <span class="n">column</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">column</span><span class="p">)</span> +</span><span id="unnest-57"><a href="#unnest-57"><span class="linenos">57</span></a> <span class="n">_replace</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="n">column</span><span class="p">)</span> +</span><span id="unnest-58"><a href="#unnest-58"><span class="linenos">58</span></a> +</span><span id="unnest-59"><a href="#unnest-59"><span class="linenos">59</span></a> <span class="n">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="unnest-60"><a href="#unnest-60"><span class="linenos">60</span></a> <span class="n">select</span><span class="p">,</span> +</span><span id="unnest-61"><a href="#unnest-61"><span class="linenos">61</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"CROSS"</span><span class="p">,</span> +</span><span id="unnest-62"><a href="#unnest-62"><span class="linenos">62</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="unnest-63"><a href="#unnest-63"><span class="linenos">63</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="unnest-64"><a href="#unnest-64"><span class="linenos">64</span></a> <span class="p">)</span> +</span><span id="unnest-65"><a href="#unnest-65"><span class="linenos">65</span></a> <span class="k">return</span> +</span><span id="unnest-66"><a href="#unnest-66"><span class="linenos">66</span></a> +</span><span id="unnest-67"><a href="#unnest-67"><span class="linenos">67</span></a> <span class="k">if</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Limit</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Offset</span><span class="p">):</span> +</span><span id="unnest-68"><a href="#unnest-68"><span class="linenos">68</span></a> <span class="k">return</span> +</span><span id="unnest-69"><a href="#unnest-69"><span class="linenos">69</span></a> +</span><span id="unnest-70"><a href="#unnest-70"><span class="linenos">70</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">):</span> +</span><span id="unnest-71"><a href="#unnest-71"><span class="linenos">71</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> +</span><span id="unnest-72"><a href="#unnest-72"><span class="linenos">72</span></a> +</span><span id="unnest-73"><a href="#unnest-73"><span class="linenos">73</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span> <span class="ow">or</span> <span class="n">parent_select</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">predicate</span><span class="o">.</span><span class="n">parent_select</span><span class="p">:</span> +</span><span id="unnest-74"><a href="#unnest-74"><span class="linenos">74</span></a> <span class="k">return</span> +</span><span id="unnest-75"><a href="#unnest-75"><span class="linenos">75</span></a> +</span><span id="unnest-76"><a href="#unnest-76"><span class="linenos">76</span></a> <span class="n">column</span> <span class="o">=</span> <span class="n">_other_operand</span><span class="p">(</span><span class="n">predicate</span><span class="p">)</span> +</span><span id="unnest-77"><a href="#unnest-77"><span class="linenos">77</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">select</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="unnest-78"><a href="#unnest-78"><span class="linenos">78</span></a> +</span><span id="unnest-79"><a href="#unnest-79"><span class="linenos">79</span></a> <span class="n">on</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">condition</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">column</span><span class="si">}</span><span class="s1"> = "</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s1">"."</span><span class="si">{</span><span class="n">value</span><span class="o">.</span><span class="n">alias</span><span class="si">}</span><span class="s1">"'</span><span class="p">)</span> +</span><span id="unnest-80"><a href="#unnest-80"><span class="linenos">80</span></a> <span class="n">_replace</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"NOT </span><span class="si">{</span><span class="n">on</span><span class="o">.</span><span class="n">right</span><span class="si">}</span><span class="s2"> IS NULL"</span><span class="p">)</span> +</span><span id="unnest-81"><a href="#unnest-81"><span class="linenos">81</span></a> +</span><span id="unnest-82"><a href="#unnest-82"><span class="linenos">82</span></a> <span class="n">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="unnest-83"><a href="#unnest-83"><span class="linenos">83</span></a> <span class="n">select</span><span class="o">.</span><span class="n">group_by</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="unnest-84"><a href="#unnest-84"><span class="linenos">84</span></a> <span class="n">on</span><span class="o">=</span><span class="n">on</span><span class="p">,</span> +</span><span id="unnest-85"><a href="#unnest-85"><span class="linenos">85</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"LEFT"</span><span class="p">,</span> +</span><span id="unnest-86"><a href="#unnest-86"><span class="linenos">86</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="unnest-87"><a href="#unnest-87"><span class="linenos">87</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="unnest-88"><a href="#unnest-88"><span class="linenos">88</span></a> <span class="p">)</span> +</span></pre></div> + + + + + </section> + <section id="decorrelate"> + <input id="decorrelate-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1"> +<div class="attr function"> + + <span class="def">def</span> + <span class="name">decorrelate</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">select</span>, </span><span class="param"><span class="n">parent_select</span>, </span><span class="param"><span class="n">external_columns</span>, </span><span class="param"><span class="n">sequence</span></span><span class="return-annotation">):</span></span> + + <label class="view-source-button" for="decorrelate-view-source"><span>View Source</span></label> + + </div> + <a class="headerlink" href="#decorrelate"></a> + <div class="pdoc-code codehilite"><pre><span></span><span id="decorrelate-91"><a href="#decorrelate-91"><span class="linenos"> 91</span></a><span class="k">def</span> <span class="nf">decorrelate</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">parent_select</span><span class="p">,</span> <span class="n">external_columns</span><span class="p">,</span> <span class="n">sequence</span><span class="p">):</span> +</span><span id="decorrelate-92"><a href="#decorrelate-92"><span class="linenos"> 92</span></a> <span class="n">where</span> <span class="o">=</span> <span class="n">select</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">"where"</span><span class="p">)</span> +</span><span id="decorrelate-93"><a href="#decorrelate-93"><span class="linenos"> 93</span></a> +</span><span id="decorrelate-94"><a href="#decorrelate-94"><span class="linenos"> 94</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">where</span> <span class="ow">or</span> <span class="n">where</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Or</span><span class="p">)</span> <span class="ow">or</span> <span class="n">select</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Limit</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Offset</span><span class="p">):</span> +</span><span id="decorrelate-95"><a href="#decorrelate-95"><span class="linenos"> 95</span></a> <span class="k">return</span> +</span><span id="decorrelate-96"><a href="#decorrelate-96"><span class="linenos"> 96</span></a> +</span><span id="decorrelate-97"><a href="#decorrelate-97"><span class="linenos"> 97</span></a> <span class="n">table_alias</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span> +</span><span id="decorrelate-98"><a href="#decorrelate-98"><span class="linenos"> 98</span></a> <span class="n">keys</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="decorrelate-99"><a href="#decorrelate-99"><span class="linenos"> 99</span></a> +</span><span id="decorrelate-100"><a href="#decorrelate-100"><span class="linenos">100</span></a> <span class="c1"># for all external columns in the where statement, find the relevant predicate</span> +</span><span id="decorrelate-101"><a href="#decorrelate-101"><span class="linenos">101</span></a> <span class="c1"># keys to convert it into a join</span> +</span><span id="decorrelate-102"><a href="#decorrelate-102"><span class="linenos">102</span></a> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">external_columns</span><span class="p">:</span> +</span><span id="decorrelate-103"><a href="#decorrelate-103"><span class="linenos">103</span></a> <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">where</span><span class="p">:</span> +</span><span id="decorrelate-104"><a href="#decorrelate-104"><span class="linenos">104</span></a> <span class="k">return</span> +</span><span id="decorrelate-105"><a href="#decorrelate-105"><span class="linenos">105</span></a> +</span><span id="decorrelate-106"><a href="#decorrelate-106"><span class="linenos">106</span></a> <span class="n">predicate</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> +</span><span id="decorrelate-107"><a href="#decorrelate-107"><span class="linenos">107</span></a> +</span><span id="decorrelate-108"><a href="#decorrelate-108"><span class="linenos">108</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span> <span class="ow">or</span> <span class="n">predicate</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Where</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">where</span><span class="p">:</span> +</span><span id="decorrelate-109"><a href="#decorrelate-109"><span class="linenos">109</span></a> <span class="k">return</span> +</span><span id="decorrelate-110"><a href="#decorrelate-110"><span class="linenos">110</span></a> +</span><span id="decorrelate-111"><a href="#decorrelate-111"><span class="linenos">111</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Binary</span><span class="p">):</span> +</span><span id="decorrelate-112"><a href="#decorrelate-112"><span class="linenos">112</span></a> <span class="n">key</span> <span class="o">=</span> <span class="p">(</span> +</span><span id="decorrelate-113"><a href="#decorrelate-113"><span class="linenos">113</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">right</span> +</span><span id="decorrelate-114"><a href="#decorrelate-114"><span class="linenos">114</span></a> <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">node</span> <span class="ow">is</span> <span class="n">column</span> <span class="k">for</span> <span class="n">node</span><span class="p">,</span> <span class="o">*</span><span class="n">_</span> <span class="ow">in</span> <span class="n">predicate</span><span class="o">.</span><span class="n">left</span><span class="o">.</span><span class="n">walk</span><span class="p">())</span> +</span><span id="decorrelate-115"><a href="#decorrelate-115"><span class="linenos">115</span></a> <span class="k">else</span> <span class="n">predicate</span><span class="o">.</span><span class="n">left</span> +</span><span id="decorrelate-116"><a href="#decorrelate-116"><span class="linenos">116</span></a> <span class="p">)</span> +</span><span id="decorrelate-117"><a href="#decorrelate-117"><span class="linenos">117</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-118"><a href="#decorrelate-118"><span class="linenos">118</span></a> <span class="k">return</span> +</span><span id="decorrelate-119"><a href="#decorrelate-119"><span class="linenos">119</span></a> +</span><span id="decorrelate-120"><a href="#decorrelate-120"><span class="linenos">120</span></a> <span class="n">keys</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">key</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="n">predicate</span><span class="p">))</span> +</span><span id="decorrelate-121"><a href="#decorrelate-121"><span class="linenos">121</span></a> +</span><span id="decorrelate-122"><a href="#decorrelate-122"><span class="linenos">122</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> <span class="k">for</span> <span class="o">*</span><span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">):</span> +</span><span id="decorrelate-123"><a href="#decorrelate-123"><span class="linenos">123</span></a> <span class="k">return</span> +</span><span id="decorrelate-124"><a href="#decorrelate-124"><span class="linenos">124</span></a> +</span><span id="decorrelate-125"><a href="#decorrelate-125"><span class="linenos">125</span></a> <span class="n">is_subquery_projection</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span> +</span><span id="decorrelate-126"><a href="#decorrelate-126"><span class="linenos">126</span></a> <span class="n">node</span> <span class="ow">is</span> <span class="n">select</span><span class="o">.</span><span class="n">parent</span> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">parent_select</span><span class="o">.</span><span class="n">selects</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Subquery</span><span class="p">)</span> +</span><span id="decorrelate-127"><a href="#decorrelate-127"><span class="linenos">127</span></a> <span class="p">)</span> +</span><span id="decorrelate-128"><a href="#decorrelate-128"><span class="linenos">128</span></a> +</span><span id="decorrelate-129"><a href="#decorrelate-129"><span class="linenos">129</span></a> <span class="n">value</span> <span class="o">=</span> <span class="n">select</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="decorrelate-130"><a href="#decorrelate-130"><span class="linenos">130</span></a> <span class="n">key_aliases</span> <span class="o">=</span> <span class="p">{}</span> +</span><span id="decorrelate-131"><a href="#decorrelate-131"><span class="linenos">131</span></a> <span class="n">group_by</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="decorrelate-132"><a href="#decorrelate-132"><span class="linenos">132</span></a> +</span><span id="decorrelate-133"><a href="#decorrelate-133"><span class="linenos">133</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span> +</span><span id="decorrelate-134"><a href="#decorrelate-134"><span class="linenos">134</span></a> <span class="c1"># if we filter on the value of the subquery, it needs to be unique</span> +</span><span id="decorrelate-135"><a href="#decorrelate-135"><span class="linenos">135</span></a> <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">:</span> +</span><span id="decorrelate-136"><a href="#decorrelate-136"><span class="linenos">136</span></a> <span class="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">alias</span> +</span><span id="decorrelate-137"><a href="#decorrelate-137"><span class="linenos">137</span></a> <span class="n">group_by</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> +</span><span id="decorrelate-138"><a href="#decorrelate-138"><span class="linenos">138</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-139"><a href="#decorrelate-139"><span class="linenos">139</span></a> <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">key_aliases</span><span class="p">:</span> +</span><span id="decorrelate-140"><a href="#decorrelate-140"><span class="linenos">140</span></a> <span class="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">_alias</span><span class="p">(</span><span class="n">sequence</span><span class="p">)</span> +</span><span id="decorrelate-141"><a href="#decorrelate-141"><span class="linenos">141</span></a> <span class="c1"># all predicates that are equalities must also be in the unique</span> +</span><span id="decorrelate-142"><a href="#decorrelate-142"><span class="linenos">142</span></a> <span class="c1"># so that we don't do a many to many join</span> +</span><span id="decorrelate-143"><a href="#decorrelate-143"><span class="linenos">143</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-144"><a href="#decorrelate-144"><span class="linenos">144</span></a> <span class="n">group_by</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> +</span><span id="decorrelate-145"><a href="#decorrelate-145"><span class="linenos">145</span></a> +</span><span id="decorrelate-146"><a href="#decorrelate-146"><span class="linenos">146</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">find_ancestor</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Predicate</span><span class="p">)</span> +</span><span id="decorrelate-147"><a href="#decorrelate-147"><span class="linenos">147</span></a> +</span><span id="decorrelate-148"><a href="#decorrelate-148"><span class="linenos">148</span></a> <span class="c1"># if the value of the subquery is not an agg or a key, we need to collect it into an array</span> +</span><span id="decorrelate-149"><a href="#decorrelate-149"><span class="linenos">149</span></a> <span class="c1"># so that it can be grouped. For subquery projections, we use a MAX aggregation instead.</span> +</span><span id="decorrelate-150"><a href="#decorrelate-150"><span class="linenos">150</span></a> <span class="n">agg_func</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Max</span> <span class="k">if</span> <span class="n">is_subquery_projection</span> <span class="k">else</span> <span class="n">exp</span><span class="o">.</span><span class="n">ArrayAgg</span> +</span><span id="decorrelate-151"><a href="#decorrelate-151"><span class="linenos">151</span></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">)</span> <span class="ow">and</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-152"><a href="#decorrelate-152"><span class="linenos">152</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> +</span><span id="decorrelate-153"><a href="#decorrelate-153"><span class="linenos">153</span></a> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">agg_func</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">),</span> <span class="n">value</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">quoted</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="decorrelate-154"><a href="#decorrelate-154"><span class="linenos">154</span></a> <span class="n">append</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="decorrelate-155"><a href="#decorrelate-155"><span class="linenos">155</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="decorrelate-156"><a href="#decorrelate-156"><span class="linenos">156</span></a> <span class="p">)</span> +</span><span id="decorrelate-157"><a href="#decorrelate-157"><span class="linenos">157</span></a> +</span><span id="decorrelate-158"><a href="#decorrelate-158"><span class="linenos">158</span></a> <span class="c1"># exists queries should not have any selects as it only checks if there are any rows</span> +</span><span id="decorrelate-159"><a href="#decorrelate-159"><span class="linenos">159</span></a> <span class="c1"># all selects will be added by the optimizer and only used for join keys</span> +</span><span id="decorrelate-160"><a href="#decorrelate-160"><span class="linenos">160</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">):</span> +</span><span id="decorrelate-161"><a href="#decorrelate-161"><span class="linenos">161</span></a> <span class="n">select</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s2">"expressions"</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> +</span><span id="decorrelate-162"><a href="#decorrelate-162"><span class="linenos">162</span></a> +</span><span id="decorrelate-163"><a href="#decorrelate-163"><span class="linenos">163</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">key_aliases</span><span class="o">.</span><span class="n">items</span><span class="p">():</span> +</span><span id="decorrelate-164"><a href="#decorrelate-164"><span class="linenos">164</span></a> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-165"><a href="#decorrelate-165"><span class="linenos">165</span></a> <span class="c1"># add all keys to the projections of the subquery</span> +</span><span id="decorrelate-166"><a href="#decorrelate-166"><span class="linenos">166</span></a> <span class="c1"># so that we can use it as a join key</span> +</span><span id="decorrelate-167"><a href="#decorrelate-167"><span class="linenos">167</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">)</span> <span class="ow">or</span> <span class="n">key</span> <span class="o">!=</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="p">:</span> +</span><span id="decorrelate-168"><a href="#decorrelate-168"><span class="linenos">168</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s2"> AS </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="decorrelate-169"><a href="#decorrelate-169"><span class="linenos">169</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-170"><a href="#decorrelate-170"><span class="linenos">170</span></a> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">agg_func</span><span class="p">(</span><span class="n">this</span><span class="o">=</span><span class="n">key</span><span class="o">.</span><span class="n">copy</span><span class="p">()),</span> <span class="n">alias</span><span class="p">,</span> <span class="n">quoted</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> +</span><span id="decorrelate-171"><a href="#decorrelate-171"><span class="linenos">171</span></a> +</span><span id="decorrelate-172"><a href="#decorrelate-172"><span class="linenos">172</span></a> <span class="n">alias</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="n">value</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="decorrelate-173"><a href="#decorrelate-173"><span class="linenos">173</span></a> <span class="n">other</span> <span class="o">=</span> <span class="n">_other_operand</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">)</span> +</span><span id="decorrelate-174"><a href="#decorrelate-174"><span class="linenos">174</span></a> +</span><span id="decorrelate-175"><a href="#decorrelate-175"><span class="linenos">175</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Exists</span><span class="p">):</span> +</span><span id="decorrelate-176"><a href="#decorrelate-176"><span class="linenos">176</span></a> <span class="n">alias</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="nb">list</span><span class="p">(</span><span class="n">key_aliases</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="decorrelate-177"><a href="#decorrelate-177"><span class="linenos">177</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"NOT </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2"> IS NULL"</span><span class="p">)</span> +</span><span id="decorrelate-178"><a href="#decorrelate-178"><span class="linenos">178</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">All</span><span class="p">):</span> +</span><span id="decorrelate-179"><a href="#decorrelate-179"><span class="linenos">179</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="decorrelate-180"><a href="#decorrelate-180"><span class="linenos">180</span></a> <span class="n">parent_predicate</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"ARRAY_ALL(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2">)"</span> +</span><span id="decorrelate-181"><a href="#decorrelate-181"><span class="linenos">181</span></a> <span class="p">)</span> +</span><span id="decorrelate-182"><a href="#decorrelate-182"><span class="linenos">182</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Any</span><span class="p">):</span> +</span><span id="decorrelate-183"><a href="#decorrelate-183"><span class="linenos">183</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-184"><a href="#decorrelate-184"><span class="linenos">184</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2"> = </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="decorrelate-185"><a href="#decorrelate-185"><span class="linenos">185</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-186"><a href="#decorrelate-186"><span class="linenos">186</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"ARRAY_ANY(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2">)"</span><span class="p">)</span> +</span><span id="decorrelate-187"><a href="#decorrelate-187"><span class="linenos">187</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">In</span><span class="p">):</span> +</span><span id="decorrelate-188"><a href="#decorrelate-188"><span class="linenos">188</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">this</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-189"><a href="#decorrelate-189"><span class="linenos">189</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span><span class="n">parent_predicate</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">other</span><span class="si">}</span><span class="s2"> = </span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span> +</span><span id="decorrelate-190"><a href="#decorrelate-190"><span class="linenos">190</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-191"><a href="#decorrelate-191"><span class="linenos">191</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="decorrelate-192"><a href="#decorrelate-192"><span class="linenos">192</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="decorrelate-193"><a href="#decorrelate-193"><span class="linenos">193</span></a> <span class="sa">f</span><span class="s2">"ARRAY_ANY(</span><span class="si">{</span><span class="n">alias</span><span class="si">}</span><span class="s2">, _x -> _x = </span><span class="si">{</span><span class="n">parent_predicate</span><span class="o">.</span><span class="n">this</span><span class="si">}</span><span class="s2">)"</span><span class="p">,</span> +</span><span id="decorrelate-194"><a href="#decorrelate-194"><span class="linenos">194</span></a> <span class="p">)</span> +</span><span id="decorrelate-195"><a href="#decorrelate-195"><span class="linenos">195</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-196"><a href="#decorrelate-196"><span class="linenos">196</span></a> <span class="k">if</span> <span class="n">is_subquery_projection</span><span class="p">:</span> +</span><span id="decorrelate-197"><a href="#decorrelate-197"><span class="linenos">197</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">alias_</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">alias</span><span class="p">)</span> +</span><span id="decorrelate-198"><a href="#decorrelate-198"><span class="linenos">198</span></a> +</span><span id="decorrelate-199"><a href="#decorrelate-199"><span class="linenos">199</span></a> <span class="c1"># COUNT always returns 0 on empty datasets, so we need take that into consideration here</span> +</span><span id="decorrelate-200"><a href="#decorrelate-200"><span class="linenos">200</span></a> <span class="c1"># by transforming all counts into 0 and using that as the coalesced value</span> +</span><span id="decorrelate-201"><a href="#decorrelate-201"><span class="linenos">201</span></a> <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span> +</span><span id="decorrelate-202"><a href="#decorrelate-202"><span class="linenos">202</span></a> +</span><span id="decorrelate-203"><a href="#decorrelate-203"><span class="linenos">203</span></a> <span class="k">def</span> <span class="nf">remove_aggs</span><span class="p">(</span><span class="n">node</span><span class="p">):</span> +</span><span id="decorrelate-204"><a href="#decorrelate-204"><span class="linenos">204</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span> +</span><span id="decorrelate-205"><a href="#decorrelate-205"><span class="linenos">205</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">Literal</span><span class="o">.</span><span class="n">number</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> +</span><span id="decorrelate-206"><a href="#decorrelate-206"><span class="linenos">206</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">AggFunc</span><span class="p">):</span> +</span><span id="decorrelate-207"><a href="#decorrelate-207"><span class="linenos">207</span></a> <span class="k">return</span> <span class="n">exp</span><span class="o">.</span><span class="n">null</span><span class="p">()</span> +</span><span id="decorrelate-208"><a href="#decorrelate-208"><span class="linenos">208</span></a> <span class="k">return</span> <span class="n">node</span> +</span><span id="decorrelate-209"><a href="#decorrelate-209"><span class="linenos">209</span></a> +</span><span id="decorrelate-210"><a href="#decorrelate-210"><span class="linenos">210</span></a> <span class="n">alias</span> <span class="o">=</span> <span class="n">exp</span><span class="o">.</span><span class="n">Coalesce</span><span class="p">(</span> +</span><span id="decorrelate-211"><a href="#decorrelate-211"><span class="linenos">211</span></a> <span class="n">this</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span> +</span><span id="decorrelate-212"><a href="#decorrelate-212"><span class="linenos">212</span></a> <span class="n">expressions</span><span class="o">=</span><span class="p">[</span><span class="n">value</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">remove_aggs</span><span class="p">)],</span> +</span><span id="decorrelate-213"><a href="#decorrelate-213"><span class="linenos">213</span></a> <span class="p">)</span> +</span><span id="decorrelate-214"><a href="#decorrelate-214"><span class="linenos">214</span></a> +</span><span id="decorrelate-215"><a href="#decorrelate-215"><span class="linenos">215</span></a> <span class="n">select</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">alias</span><span class="p">)</span> +</span><span id="decorrelate-216"><a href="#decorrelate-216"><span class="linenos">216</span></a> +</span><span id="decorrelate-217"><a href="#decorrelate-217"><span class="linenos">217</span></a> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span> +</span><span id="decorrelate-218"><a href="#decorrelate-218"><span class="linenos">218</span></a> <span class="n">predicate</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">true</span><span class="p">())</span> +</span><span id="decorrelate-219"><a href="#decorrelate-219"><span class="linenos">219</span></a> <span class="n">nested</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="n">key_aliases</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">table_alias</span><span class="p">)</span> +</span><span id="decorrelate-220"><a href="#decorrelate-220"><span class="linenos">220</span></a> +</span><span id="decorrelate-221"><a href="#decorrelate-221"><span class="linenos">221</span></a> <span class="k">if</span> <span class="n">is_subquery_projection</span><span class="p">:</span> +</span><span id="decorrelate-222"><a href="#decorrelate-222"><span class="linenos">222</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">nested</span><span class="p">)</span> +</span><span id="decorrelate-223"><a href="#decorrelate-223"><span class="linenos">223</span></a> <span class="k">continue</span> +</span><span id="decorrelate-224"><a href="#decorrelate-224"><span class="linenos">224</span></a> +</span><span id="decorrelate-225"><a href="#decorrelate-225"><span class="linenos">225</span></a> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">group_by</span><span class="p">:</span> +</span><span id="decorrelate-226"><a href="#decorrelate-226"><span class="linenos">226</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">nested</span><span class="p">)</span> +</span><span id="decorrelate-227"><a href="#decorrelate-227"><span class="linenos">227</span></a> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">):</span> +</span><span id="decorrelate-228"><a href="#decorrelate-228"><span class="linenos">228</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="decorrelate-229"><a href="#decorrelate-229"><span class="linenos">229</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="decorrelate-230"><a href="#decorrelate-230"><span class="linenos">230</span></a> <span class="sa">f</span><span class="s2">"(</span><span class="si">{</span><span class="n">parent_predicate</span><span class="si">}</span><span class="s2"> AND ARRAY_CONTAINS(</span><span class="si">{</span><span class="n">nested</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">column</span><span class="si">}</span><span class="s2">))"</span><span class="p">,</span> +</span><span id="decorrelate-231"><a href="#decorrelate-231"><span class="linenos">231</span></a> <span class="p">)</span> +</span><span id="decorrelate-232"><a href="#decorrelate-232"><span class="linenos">232</span></a> <span class="k">else</span><span class="p">:</span> +</span><span id="decorrelate-233"><a href="#decorrelate-233"><span class="linenos">233</span></a> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">to_identifier</span><span class="p">(</span><span class="s2">"_x"</span><span class="p">))</span> +</span><span id="decorrelate-234"><a href="#decorrelate-234"><span class="linenos">234</span></a> <span class="n">parent_predicate</span> <span class="o">=</span> <span class="n">_replace</span><span class="p">(</span> +</span><span id="decorrelate-235"><a href="#decorrelate-235"><span class="linenos">235</span></a> <span class="n">parent_predicate</span><span class="p">,</span> +</span><span id="decorrelate-236"><a href="#decorrelate-236"><span class="linenos">236</span></a> <span class="sa">f</span><span class="s1">'(</span><span class="si">{</span><span class="n">parent_predicate</span><span class="si">}</span><span class="s1"> AND ARRAY_ANY(</span><span class="si">{</span><span class="n">nested</span><span class="si">}</span><span class="s1">, "_x" -> </span><span class="si">{</span><span class="n">predicate</span><span class="si">}</span><span class="s1">))'</span><span class="p">,</span> +</span><span id="decorrelate-237"><a href="#decorrelate-237"><span class="linenos">237</span></a> <span class="p">)</span> +</span><span id="decorrelate-238"><a href="#decorrelate-238"><span class="linenos">238</span></a> +</span><span id="decorrelate-239"><a href="#decorrelate-239"><span class="linenos">239</span></a> <span class="n">parent_select</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> +</span><span id="decorrelate-240"><a href="#decorrelate-240"><span class="linenos">240</span></a> <span class="n">select</span><span class="o">.</span><span class="n">group_by</span><span class="p">(</span><span class="o">*</span><span class="n">group_by</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> +</span><span id="decorrelate-241"><a href="#decorrelate-241"><span class="linenos">241</span></a> <span class="n">on</span><span class="o">=</span><span class="p">[</span><span class="n">predicate</span> <span class="k">for</span> <span class="o">*</span><span class="n">_</span><span class="p">,</span> <span class="n">predicate</span> <span class="ow">in</span> <span class="n">keys</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">exp</span><span class="o">.</span><span class="n">EQ</span><span class="p">)],</span> +</span><span id="decorrelate-242"><a href="#decorrelate-242"><span class="linenos">242</span></a> <span class="n">join_type</span><span class="o">=</span><span class="s2">"LEFT"</span><span class="p">,</span> +</span><span id="decorrelate-243"><a href="#decorrelate-243"><span class="linenos">243</span></a> <span class="n">join_alias</span><span class="o">=</span><span class="n">table_alias</span><span class="p">,</span> +</span><span id="decorrelate-244"><a href="#decorrelate-244"><span class="linenos">244</span></a> <span class="n">copy</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> +</span><span id="decorrelate-245"><a href="#decorrelate-245"><span class="linenos">245</span></a> <span class="p">)</span> +</span></pre></div> + + + + + </section> + </main> +<script> + function escapeHTML(html) { + return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML; + } + + const originalContent = document.querySelector("main.pdoc"); + let currentContent = originalContent; + + function setContent(innerHTML) { + let elem; + if (innerHTML) { + elem = document.createElement("main"); + elem.classList.add("pdoc"); + elem.innerHTML = innerHTML; + } else { + elem = originalContent; + } + if (currentContent !== elem) { + currentContent.replaceWith(elem); + currentContent = elem; + } + } + + function getSearchTerm() { + return (new URL(window.location)).searchParams.get("search"); + } + + const searchBox = document.querySelector(".pdoc input[type=search]"); + searchBox.addEventListener("input", function () { + let url = new URL(window.location); + if (searchBox.value.trim()) { + url.hash = ""; + url.searchParams.set("search", searchBox.value); + } else { + url.searchParams.delete("search"); + } + history.replaceState("", "", url.toString()); + onInput(); + }); + window.addEventListener("popstate", onInput); + + + let search, searchErr; + + async function initialize() { + try { + search = await new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.async = true; + script.onload = () => resolve(window.pdocSearch); + script.onerror = (e) => reject(e); + script.src = "../../search.js"; + document.getElementsByTagName("head")[0].appendChild(script); + }); + } catch (e) { + console.error("Cannot fetch pdoc search index"); + searchErr = "Cannot fetch search index."; + } + onInput(); + + document.querySelector("nav.pdoc").addEventListener("click", e => { + if (e.target.hash) { + searchBox.value = ""; + searchBox.dispatchEvent(new Event("input")); + } + }); + } + + function onInput() { + setContent((() => { + const term = getSearchTerm(); + if (!term) { + return null + } + if (searchErr) { + return `<h3>Error: ${searchErr}</h3>` + } + if (!search) { + return "<h3>Searching...</h3>" + } + + window.scrollTo({top: 0, left: 0, behavior: 'auto'}); + + const results = search(term); + + let html; + if (results.length === 0) { + html = `No search results for '${escapeHTML(term)}'.` + } else { + html = `<h4>${results.length} search result${results.length > 1 ? "s" : ""} for '${escapeHTML(term)}'.</h4>`; + } + for (let result of results.slice(0, 10)) { + let doc = result.doc; + let url = `../../${doc.modulename.replaceAll(".", "/")}.html`; + if (doc.qualname) { + url += `#${doc.qualname}`; + } + + let heading; + switch (result.doc.kind) { + case "function": + if (doc.fullname.endsWith(".__init__")) { + heading = `<span class="name">${doc.fullname.replace(/\.__init__$/, "")}</span>${doc.signature}`; + } else { + heading = `<span class="def">${doc.funcdef}</span> <span class="name">${doc.fullname}</span>${doc.signature}`; + } + break; + case "class": + heading = `<span class="def">class</span> <span class="name">${doc.fullname}</span>`; + if (doc.bases) + heading += `<wbr>(<span class="base">${doc.bases}</span>)`; + heading += `:`; + break; + case "variable": + heading = `<span class="name">${doc.fullname}</span>`; + if (doc.annotation) + heading += `<span class="annotation">${doc.annotation}</span>`; + if (doc.default_value) + heading += `<span class="default_value">${doc.default_value}</span>`; + break; + default: + heading = `<span class="name">${doc.fullname}</span>`; + break; + } + html += ` + <section class="search-result"> + <a href="${url}" class="attr ${doc.kind}">${heading}</a> + <div class="docstring">${doc.doc}</div> + </section> + `; + + } + return html; + })()); + } + + if (getSearchTerm()) { + initialize(); + searchBox.value = getSearchTerm(); + onInput(); + } else { + searchBox.addEventListener("focus", initialize, {once: true}); + } + + searchBox.addEventListener("keydown", e => { + if (["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) { + let focused = currentContent.querySelector(".search-result.focused"); + if (!focused) { + currentContent.querySelector(".search-result").classList.add("focused"); + } else if ( + e.key === "ArrowDown" + && focused.nextElementSibling + && focused.nextElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.nextElementSibling.classList.add("focused"); + focused.nextElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "ArrowUp" + && focused.previousElementSibling + && focused.previousElementSibling.classList.contains("search-result") + ) { + focused.classList.remove("focused"); + focused.previousElementSibling.classList.add("focused"); + focused.previousElementSibling.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "nearest" + }); + } else if ( + e.key === "Enter" + ) { + focused.querySelector("a").click(); + } + } + }); +</script></body> +</html>
\ No newline at end of file |