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 doesn't work when GROUP BY..ORDER BY COUNT(*) #1887

Closed
stonyw opened this issue Jul 4, 2023 · 1 comment
Closed

Executor doesn't work when GROUP BY..ORDER BY COUNT(*) #1887

stonyw opened this issue Jul 4, 2023 · 1 comment
Assignees

Comments

@stonyw
Copy link

stonyw commented Jul 4, 2023

sqlglot==16.8.1

    def test_execute_group_by_order_by_count(self):
        tables = {
            "x": [
                {"t": "a", "a": 6, "y": 2019},
                {"t": "b", "a": 12, "y": 2019},
                {"t": "c", "a": 15, "y": 2019},
                {"t": "a", "a": 10, "y": 2018},
                {"t": "b", "a": 12, "y": 2018},
                {"t": "c", "a": 2, "y": 2018},
                {"t": "a", "a": 9, "y": 2017},
            ],
        }
        schema = {
            "x": {
                "t": "text",
                "a": "number",
                "y": "number"
            },
        }
        gen = SqlGenerator("mysql")
        creates = gen.get_sql_tables_schema(schema)
        for c in creates:
            print(c.sql(pretty=True, dialect="mysql"), ";", sep="")
        inserts = gen.get_sql_tables_content(tables)
        for i in inserts:
            print(i.sql(pretty=True, dialect="mysql"), ";", sep="")

        for sql, cols, rows in [
            ('select t , sum ( a ) from x group by t order by avg(a)  asc limit 1;', ["t", "_col_1"], [("a", 25)]), # PASS
            ('select t , sum ( a ) from x group by t order by count ( * ) asc limit 1;', ["t", "_col_1"], [("b", 24)]),  # FAIL
        ]:
            with self.subTest(sql):
                result = execute(sql, read="mysql", schema=schema, tables=tables)
                self.assertEqual(result.columns, tuple(cols))
                self.assertEqual(result.rows, rows)
SubTest error: Traceback (most recent call last):
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 40, in execute
    contexts[node] = self.aggregate(node, context)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 288, in aggregate
    add_row()
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 278, in add_row
    table.append(group + context.eval_tuple(aggregations))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  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 "COUNT(scope[None]["_a_1"])", line 1, in <module>
KeyError: None


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/.../python3.11/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/.../python3.11/unittest/case.py", line 538, in subTest
    yield
  File "/Users/.../src/tr/dbtool/test_executor.py", line 237, in test_execute_group_by_order_by_count
    result = execute(sql, read="mysql", schema=schema, tables=tables)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/..../python3.11/site-packages/sqlglot/executor/__init__.py", line 82, in execute
    result = PythonExecutor(tables=tables_).execute(plan)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../python3.11/site-packages/sqlglot/executor/python.py", line 60, in execute
    raise ExecuteError(f"Step '{node.id}' failed: {e}") from e
sqlglot.errors.ExecuteError: Step 'Aggregate: x (4552043856)' failed: None

@georgesittas
Copy link
Collaborator

I'll take a look, thanks

@tobymao tobymao self-assigned this Jul 4, 2023
tobymao added a commit that referenced this issue Jul 4, 2023
@tobymao tobymao closed this as completed in 5a84605 Jul 4, 2023
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

No branches or pull requests

3 participants