Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental CTE support #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bin/q.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,19 @@ def __init__(self, sql):

self.query_column_names = None

aliases_to_skip = []

# Go over all sql parts
idx = 0
while idx < len(self.sql_parts):
# Get the part string
part = self.sql_parts[idx]

if part.upper() == 'WITH':
aliases_to_skip.append(self.sql_parts[idx + 1])
idx += 2
continue

# If it's a FROM or a JOIN
if part.upper() in ['FROM', 'JOIN']:
# and there is nothing after it,
Expand All @@ -395,6 +403,11 @@ def __init__(self, sql):
'FROM/JOIN is missing a table name after it')

qtable_name = self.sql_parts[idx + 1]

if qtable_name in aliases_to_skip:
idx += 1
continue

# Otherwise, the next part contains the qtable name. In most cases the next part will be only the qtable name.
# We handle one special case here, where this is a subquery as a column: "SELECT (SELECT ... FROM qtable),100 FROM ...".
# In that case, there will be an ending paranthesis as part of the name, and we want to handle this case gracefully.
Expand Down