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: no limit in SELECT * for TOP dbs #27215

Merged
merged 2 commits into from
Feb 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ def select_star( # pylint: disable=too-many-arguments,too-many-locals

qry = select(fields).select_from(text(full_table_name))

if limit:
if limit and cls.allow_limit_clause:
qry = qry.limit(limit)
if latest_partition:
partition_query = cls.where_latest_partition(
Expand Down
75 changes: 74 additions & 1 deletion tests/unit_tests/db_engine_specs/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=unused-argument, import-outside-toplevel, protected-access
# pylint: disable=import-outside-toplevel, protected-access

from textwrap import dedent
from typing import Any, Optional

import pytest
from pytest_mock import MockFixture
from sqlalchemy import types
from sqlalchemy.dialects import sqlite
from sqlalchemy.sql import sqltypes

from superset.superset_typing import ResultSetColumnType, SQLAColumnType
from superset.utils.core import GenericDataType
Expand Down Expand Up @@ -168,3 +171,73 @@ def test_convert_inspector_columns(
from superset.db_engine_specs.base import convert_inspector_columns

assert convert_inspector_columns(cols) == expected_result


def test_select_star(mocker: MockFixture) -> None:
"""
Test the ``select_star`` method.
"""
from superset.db_engine_specs.base import BaseEngineSpec

class NoLimitDBEngineSpec(BaseEngineSpec):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

allow_limit_clause = False

cols: list[ResultSetColumnType] = [
{
"column_name": "a",
"name": "a",
"type": sqltypes.String(),
"nullable": True,
"comment": None,
"default": None,
"precision": None,
"scale": None,
"max_length": None,
"is_dttm": False,
},
]

# mock the database so we can compile the query
database = mocker.MagicMock()
database.compile_sqla_query = lambda query: str(
query.compile(dialect=sqlite.dialect())
)

engine = mocker.MagicMock()
engine.dialect = sqlite.dialect()

sql = BaseEngineSpec.select_star(
database=database,
table_name="my_table",
engine=engine,
schema=None,
limit=100,
show_cols=True,
indent=True,
latest_partition=False,
cols=cols,
)
assert (
sql
== """SELECT a
FROM my_table
LIMIT ?
OFFSET ?"""
)

sql = NoLimitDBEngineSpec.select_star(
database=database,
table_name="my_table",
engine=engine,
schema=None,
limit=100,
show_cols=True,
indent=True,
latest_partition=False,
cols=cols,
)
assert (
sql
== """SELECT a
FROM my_table"""
)
Loading