mirror of
https://github.com/apache/superset.git
synced 2026-04-20 00:24:38 +00:00
refactor: remove more sqlparse (#31032)
This commit is contained in:
@@ -30,7 +30,7 @@ from superset.db_engine_specs.base import (
|
||||
from superset.db_engine_specs.mysql import MySQLEngineSpec
|
||||
from superset.db_engine_specs.sqlite import SqliteEngineSpec
|
||||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
|
||||
from superset.sql_parse import ParsedQuery, Table
|
||||
from superset.sql_parse import Table
|
||||
from superset.utils.database import get_example_database
|
||||
from tests.integration_tests.db_engine_specs.base_tests import TestDbEngineSpec
|
||||
from tests.integration_tests.test_app import app
|
||||
@@ -310,20 +310,6 @@ class TestDbEngineSpecs(TestDbEngineSpec):
|
||||
)
|
||||
|
||||
|
||||
def test_is_readonly():
|
||||
def is_readonly(sql: str) -> bool:
|
||||
return BaseEngineSpec.is_readonly_query(ParsedQuery(sql))
|
||||
|
||||
assert is_readonly("SHOW LOCKS test EXTENDED")
|
||||
assert not is_readonly("SET hivevar:desc='Legislators'")
|
||||
assert not is_readonly("UPDATE t1 SET col1 = NULL")
|
||||
assert is_readonly("EXPLAIN SELECT 1")
|
||||
assert is_readonly("SELECT 1")
|
||||
assert is_readonly("WITH (SELECT 1) bla SELECT * from bla")
|
||||
assert is_readonly("SHOW CATALOGS")
|
||||
assert is_readonly("SHOW TABLES")
|
||||
|
||||
|
||||
def test_time_grain_denylist():
|
||||
config = app.config.copy()
|
||||
app.config["TIME_GRAIN_DENYLIST"] = ["PT1M", "SQLITE_NONEXISTENT_GRAIN"]
|
||||
|
||||
@@ -25,7 +25,7 @@ from sqlalchemy.sql import select
|
||||
|
||||
from superset.db_engine_specs.hive import HiveEngineSpec, upload_to_s3
|
||||
from superset.exceptions import SupersetException
|
||||
from superset.sql_parse import ParsedQuery, Table
|
||||
from superset.sql_parse import Table
|
||||
from tests.integration_tests.test_app import app
|
||||
|
||||
|
||||
@@ -227,19 +227,6 @@ def test_df_to_sql_if_exists_replace_with_schema(mock_upload_to_s3, mock_g):
|
||||
app.config = config
|
||||
|
||||
|
||||
def test_is_readonly():
|
||||
def is_readonly(sql: str) -> bool:
|
||||
return HiveEngineSpec.is_readonly_query(ParsedQuery(sql))
|
||||
|
||||
assert not is_readonly("UPDATE t1 SET col1 = NULL")
|
||||
assert not is_readonly("INSERT OVERWRITE TABLE tabB SELECT a.Age FROM TableA")
|
||||
assert is_readonly("SHOW LOCKS test EXTENDED")
|
||||
assert is_readonly("SET hivevar:desc='Legislators'")
|
||||
assert is_readonly("EXPLAIN SELECT 1")
|
||||
assert is_readonly("SELECT 1")
|
||||
assert is_readonly("WITH (SELECT 1) bla SELECT * from bla")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,upload_prefix",
|
||||
[("foo", "EXTERNAL_HIVE_TABLES/1/foo/"), (None, "EXTERNAL_HIVE_TABLES/1/")],
|
||||
|
||||
@@ -25,7 +25,7 @@ from sqlalchemy.sql import select
|
||||
|
||||
from superset.db_engine_specs.presto import PrestoEngineSpec
|
||||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
|
||||
from superset.sql_parse import ParsedQuery, Table
|
||||
from superset.sql_parse import Table
|
||||
from superset.utils.database import get_example_database
|
||||
from tests.integration_tests.db_engine_specs.base_tests import TestDbEngineSpec
|
||||
|
||||
@@ -1172,19 +1172,6 @@ class TestPrestoDbEngineSpec(TestDbEngineSpec):
|
||||
]
|
||||
|
||||
|
||||
def test_is_readonly():
|
||||
def is_readonly(sql: str) -> bool:
|
||||
return PrestoEngineSpec.is_readonly_query(ParsedQuery(sql))
|
||||
|
||||
assert not is_readonly("SET hivevar:desc='Legislators'")
|
||||
assert not is_readonly("UPDATE t1 SET col1 = NULL")
|
||||
assert not is_readonly("INSERT OVERWRITE TABLE tabB SELECT a.Age FROM TableA")
|
||||
assert is_readonly("SHOW LOCKS test EXTENDED")
|
||||
assert is_readonly("EXPLAIN SELECT 1")
|
||||
assert is_readonly("SELECT 1")
|
||||
assert is_readonly("WITH (SELECT 1) bla SELECT * from bla")
|
||||
|
||||
|
||||
def test_get_catalog_names(app_context: AppContext) -> None:
|
||||
"""
|
||||
Test the ``get_catalog_names`` method.
|
||||
|
||||
@@ -20,6 +20,8 @@ from typing import Optional
|
||||
|
||||
import pytest
|
||||
|
||||
from superset.sql.parse import SQLScript
|
||||
from superset.sql_parse import ParsedQuery
|
||||
from tests.unit_tests.db_engine_specs.utils import assert_convert_dttm
|
||||
from tests.unit_tests.fixtures.common import dttm # noqa: F401
|
||||
|
||||
@@ -27,24 +29,26 @@ from tests.unit_tests.fixtures.common import dttm # noqa: F401
|
||||
@pytest.mark.parametrize(
|
||||
"sql,expected",
|
||||
[
|
||||
("SELECT foo FROM tbl", True),
|
||||
("SELECT foo FROM tbl", False),
|
||||
("SHOW TABLES", False),
|
||||
("EXPLAIN SELECT foo FROM tbl", False),
|
||||
("INSERT INTO tbl (foo) VALUES (1)", False),
|
||||
("INSERT INTO tbl (foo) VALUES (1)", True),
|
||||
],
|
||||
)
|
||||
def test_sql_is_readonly_query(sql: str, expected: bool) -> None:
|
||||
def test_sql_has_mutation(sql: str, expected: bool) -> None:
|
||||
"""
|
||||
Make sure that SQL dialect consider only SELECT statements as read-only
|
||||
"""
|
||||
|
||||
from superset.db_engine_specs.kusto import KustoSqlEngineSpec
|
||||
from superset.sql_parse import ParsedQuery
|
||||
|
||||
parsed_query = ParsedQuery(sql)
|
||||
is_readonly = KustoSqlEngineSpec.is_readonly_query(parsed_query)
|
||||
|
||||
assert expected == is_readonly
|
||||
assert (
|
||||
SQLScript(
|
||||
sql,
|
||||
engine=KustoSqlEngineSpec.engine,
|
||||
).has_mutation()
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -62,38 +66,37 @@ def test_kql_is_select_query(kql: str, expected: bool) -> None:
|
||||
"""
|
||||
|
||||
from superset.db_engine_specs.kusto import KustoKqlEngineSpec
|
||||
from superset.sql_parse import ParsedQuery
|
||||
|
||||
parsed_query = ParsedQuery(kql)
|
||||
is_select = KustoKqlEngineSpec.is_select_query(parsed_query)
|
||||
|
||||
assert expected == is_select
|
||||
assert KustoKqlEngineSpec.is_select_query(parsed_query) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kql,expected",
|
||||
[
|
||||
("tbl | limit 100", True),
|
||||
("let foo = 1; tbl | where bar == foo", True),
|
||||
(".show tables", True),
|
||||
("print 1", True),
|
||||
("set querytrace; Events | take 100", True),
|
||||
(".drop table foo", False),
|
||||
(".set-or-append table foo <| bar", False),
|
||||
("tbl | limit 100", False),
|
||||
("let foo = 1; tbl | where bar == foo", False),
|
||||
(".show tables", False),
|
||||
("print 1", False),
|
||||
("set querytrace; Events | take 100", False),
|
||||
(".drop table foo", True),
|
||||
(".set-or-append table foo <| bar", True),
|
||||
],
|
||||
)
|
||||
def test_kql_is_readonly_query(kql: str, expected: bool) -> None:
|
||||
def test_kql_has_mutation(kql: str, expected: bool) -> None:
|
||||
"""
|
||||
Make sure that KQL dialect consider only SELECT statements as read-only
|
||||
"""
|
||||
|
||||
from superset.db_engine_specs.kusto import KustoKqlEngineSpec
|
||||
from superset.sql_parse import ParsedQuery
|
||||
|
||||
parsed_query = ParsedQuery(kql)
|
||||
is_readonly = KustoKqlEngineSpec.is_readonly_query(parsed_query)
|
||||
|
||||
assert expected == is_readonly
|
||||
assert (
|
||||
SQLScript(
|
||||
kql,
|
||||
engine=KustoKqlEngineSpec.engine,
|
||||
).has_mutation()
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
def test_kql_parse_sql() -> None:
|
||||
|
||||
@@ -945,6 +945,28 @@ on $left.Day1 == $right.Day
|
||||
("kustokql", "set querytrace; Events | take 100", False),
|
||||
("kustokql", ".drop table foo", True),
|
||||
("kustokql", ".set-or-append table foo <| bar", True),
|
||||
("base", "SHOW LOCKS test EXTENDED", False),
|
||||
("base", "SET hivevar:desc='Legislators'", False),
|
||||
("base", "UPDATE t1 SET col1 = NULL", True),
|
||||
("base", "EXPLAIN SELECT 1", False),
|
||||
("base", "SELECT 1", False),
|
||||
("base", "WITH bla AS (SELECT 1) SELECT * FROM bla", False),
|
||||
("base", "SHOW CATALOGS", False),
|
||||
("base", "SHOW TABLES", False),
|
||||
("hive", "UPDATE t1 SET col1 = NULL", True),
|
||||
("hive", "INSERT OVERWRITE TABLE tabB SELECT a.Age FROM TableA", True),
|
||||
("hive", "SHOW LOCKS test EXTENDED", False),
|
||||
("hive", "SET hivevar:desc='Legislators'", False),
|
||||
("hive", "EXPLAIN SELECT 1", False),
|
||||
("hive", "SELECT 1", False),
|
||||
("hive", "WITH bla AS (SELECT 1) SELECT * FROM bla", False),
|
||||
("presto", "SET hivevar:desc='Legislators'", False),
|
||||
("presto", "UPDATE t1 SET col1 = NULL", True),
|
||||
("presto", "INSERT OVERWRITE TABLE tabB SELECT a.Age FROM TableA", True),
|
||||
("presto", "SHOW LOCKS test EXTENDED", False),
|
||||
("presto", "EXPLAIN SELECT 1", False),
|
||||
("presto", "SELECT 1", False),
|
||||
("presto", "WITH bla AS (SELECT 1) SELECT * FROM bla", False),
|
||||
],
|
||||
)
|
||||
def test_has_mutation(engine: str, sql: str, expected: bool) -> None:
|
||||
@@ -1042,7 +1064,7 @@ def test_custom_dialect(app: None) -> None:
|
||||
)
|
||||
def test_is_mutating(engine: str) -> None:
|
||||
"""
|
||||
Tests for `is_mutating`.
|
||||
Global tests for `is_mutating`, covering all supported engines.
|
||||
"""
|
||||
assert not SQLStatement(
|
||||
"with source as ( select 1 as one ) select * from source",
|
||||
|
||||
Reference in New Issue
Block a user