apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.99k stars 13.59k forks source link

KeyError: 'sql' when opening a Trino virtual dataset that contains latest_partition on SQL Lab #30338

Closed michael-s-molina closed 1 week ago

michael-s-molina commented 1 week ago

Bug description

Every time I try to open, in SQL Lab from Explore, a virtual dataset that contains WHERE time_column <= '{{presto.latest_partition('table_name')}}' it throws a DB engine Error with the following stacktrace:

/apache-superset/superset/utils/decorators.py:263: SAWarning: Session's state has been changed on a non-active transaction - this state will be discarded.
  db.session.rollback()  # pylint: disable=consider-using-transaction
2024-09-19 14:04:59,932:warning:superset.views.error_handling:Exception
Traceback (most recent call last):
  File "/apache-superset/superset/commands/sql_lab/execute.py", line 106, in run
    status = self._run_sql_json_exec_from_scratch()
  File "/apache-superset/superset/commands/sql_lab/execute.py", line 161, in _run_sql_json_exec_from_scratch
    return self._sql_json_executor.execute(
  File "/apache-superset/superset/sqllab/sql_json_executer.py", line 170, in execute
    query_id = execution_context.query.id
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/attributes.py", line 487, in __get__
    return self.impl.get(state, dict_)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/attributes.py", line 959, in get
    value = self._fire_loader_callables(state, key, passive)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/attributes.py", line 990, in _fire_loader_callables
    return state._load_expired(state, passive)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/state.py", line 712, in _load_expired
    self.manager.expired_attribute_loader(self, toload, passive)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/loading.py", line 1451, in load_scalar_attributes
    result = load_on_ident(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/loading.py", line 407, in load_on_ident
    return load_on_pk_identity(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/loading.py", line 530, in load_on_pk_identity
    session.execute(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/session.py", line 1665, in execute
    ) = compile_state_cls.orm_pre_session_exec(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/context.py", line 312, in orm_pre_session_exec
    session._autoflush()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/session.py", line 2253, in _autoflush
    self.flush()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/session.py", line 3449, in flush
    self._flush(objects)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/session.py", line 3588, in _flush
    with util.safe_reraise():
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/session.py", line 3549, in _flush
    flush_context.execute()
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
    rec.execute(self)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
    util.preloaded.orm_persistence.save_obj(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/persistence.py", line 237, in save_obj
    _emit_update_statements(
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/persistence.py", line 886, in _emit_update_statements
    for (
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/persistence.py", line 612, in _collect_update_commands
    value = state_dict[propkey]
KeyError: 'sql'

The exception only happens if latest_partition is used. If you click on Run query again, the query executes.

How to reproduce the bug

  1. From Explore open a virtual dataset on SQL Lab (Trino)
  2. You'll see a DB Engine error
  3. Run the query again
  4. It works

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

Also present in 4.1.0 RC2

Checklist