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

Executor cannot find the aggr column in order-by clause if aggr column is not in the select clause. #1855

Closed
stonyw opened this issue Jun 29, 2023 · 0 comments · Fixed by #1863

Comments

@stonyw
Copy link

stonyw commented Jun 29, 2023

sqlglot==16.6.0

Related to #1822

('SELECT a, AVG(b) FROM x GROUP BY a ORDER BY AVG(b)', ["a", "_col_1"], [("b", 22.5), ("a", 25), ("c", 28)]), # PASS
('SELECT a FROM x GROUP BY a ORDER BY AVG(b)', ["a"], [("b",), ("a",), ("c",)]),  # FAILED

if avg not in select clause , i have the same issue.

Here is the error log.

Traceback (most recent call last):
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 46, in execute
    contexts[node] = self.sort(node, context)
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 323, in sort
    sort_ctx.sort(self.generate_tuple(step.key))
  File "/Users/.../python3.11/site-packages/sqlglot/executor/context.py", line 81, in sort
    self.table.rows.sort(key=sort_key)
  File "/Users/.../python3.11/site-packages/sqlglot/executor/context.py", line 79, in sort_key
    return self.eval_tuple(key)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/context.py", line 38, in eval_tuple
    return tuple(self.eval(code) for code in codes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/context.py", line 38, in <genexpr>
    return tuple(self.eval(code) for code in codes)
                 ^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/context.py", line 35, in eval
    return eval(code, self.env)
           ^^^^^^^^^^^^^^^^^^^^
  File "ORDERED(AVG(scope["x"]["b"]), False, True)", line 1, in <module>
  File "/Users/.../python3.11/site-packages/sqlglot/executor/table.py", line 104, in __getitem__
    return self.row[self.columns[column]]
                    ~~~~~~~~~~~~^^^^^^^^
KeyError: 'b'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants