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

Fix: ensure bool(check_query_exists) returns True or False #43978

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,8 @@ def check_query_exists(query_stmt: Select, *, session: Session) -> bool:
:meta private:
"""
count_stmt = select(literal(True)).select_from(query_stmt.order_by(None).subquery())
return session.scalar(count_stmt)
# we must cast to bool because scalar() can return None
return bool(session.scalar(count_stmt))


def exists_query(*where: ClauseElement, session: Session) -> bool:
Expand Down
16 changes: 15 additions & 1 deletion tests/utils/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
from alembic.migration import MigrationContext
from alembic.runtime.environment import EnvironmentContext
from alembic.script import ScriptDirectory
from sqlalchemy import MetaData
from sqlalchemy import Column, Integer, MetaData, Table, select

from airflow.models import Base as airflow_base
from airflow.settings import engine
from airflow.utils.db import (
LazySelectSequence,
_get_alembic_config,
check_migrations,
compare_server_default,
Expand Down Expand Up @@ -251,3 +252,16 @@ def test_alembic_configuration(self):
import airflow

assert config.config_file_name == os.path.join(os.path.dirname(airflow.__file__), "alembic.ini")

def test_bool_lazy_select_sequence(self):
class MockSession:
def __init__(self):
pass

def scalar(self, stmt):
return None

t = Table("t", MetaData(), Column("id", Integer, primary_key=True))
lss = LazySelectSequence.from_select(select(t.c.id), order_by=[], session=MockSession())

assert bool(lss) is False