mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
fix: save columns reference from sqllab save datasets flow (#24248)
This commit is contained in:
@@ -121,9 +121,9 @@ def test_main_dttm_col(mocker: MockerFixture, test_table: "SqlaTable") -> None:
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
@@ -154,9 +154,9 @@ def test_main_dttm_col_nonexistent(
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
@@ -188,9 +188,9 @@ def test_main_dttm_col_nondttm(
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "ds", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "event_time", "type": "TIMESTAMP", "is_dttm": True},
|
||||
{"column_name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
@@ -226,9 +226,9 @@ def test_python_date_format_by_column_name(
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
@@ -274,8 +274,8 @@ def test_expression_by_column_name(
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
@@ -311,9 +311,9 @@ def test_full_setting(
|
||||
mocker.patch(
|
||||
"superset.connectors.sqla.models.get_physical_table_metadata",
|
||||
return_value=[
|
||||
{"name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "id", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "dttm", "type": "INTEGER", "is_dttm": False},
|
||||
{"column_name": "duration_ms", "type": "INTEGER", "is_dttm": False},
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ from typing import Any, Optional
|
||||
import pytest
|
||||
from sqlalchemy import types
|
||||
|
||||
from superset.superset_typing import ResultSetColumnType, SQLAColumnType
|
||||
from superset.utils.core import GenericDataType
|
||||
from tests.unit_tests.db_engine_specs.utils import assert_column_spec
|
||||
|
||||
@@ -138,3 +139,32 @@ def test_get_column_spec(
|
||||
from superset.db_engine_specs.databricks import DatabricksNativeEngineSpec as spec
|
||||
|
||||
assert_column_spec(spec, native_type, sqla_type, attrs, generic_type, is_dttm)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"cols, expected_result",
|
||||
[
|
||||
(
|
||||
[SQLAColumnType(name="John", type="integer", is_dttm=False)],
|
||||
[
|
||||
ResultSetColumnType(
|
||||
column_name="John", name="John", type="integer", is_dttm=False
|
||||
)
|
||||
],
|
||||
),
|
||||
(
|
||||
[SQLAColumnType(name="hugh", type="integer", is_dttm=False)],
|
||||
[
|
||||
ResultSetColumnType(
|
||||
column_name="hugh", name="hugh", type="integer", is_dttm=False
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_convert_inspector_columns(
|
||||
cols: list[SQLAColumnType], expected_result: list[ResultSetColumnType]
|
||||
):
|
||||
from superset.db_engine_specs.base import convert_inspector_columns
|
||||
|
||||
assert convert_inspector_columns(cols) == expected_result
|
||||
|
||||
@@ -27,6 +27,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.sql import sqltypes
|
||||
from sqlalchemy_bigquery import BigQueryDialect
|
||||
|
||||
from superset.superset_typing import ResultSetColumnType
|
||||
from tests.unit_tests.db_engine_specs.utils import assert_convert_dttm
|
||||
from tests.unit_tests.fixtures.common import dttm
|
||||
|
||||
@@ -64,7 +65,16 @@ def test_get_fields() -> None:
|
||||
"""
|
||||
from superset.db_engine_specs.bigquery import BigQueryEngineSpec
|
||||
|
||||
columns = [{"name": "limit"}, {"name": "name"}, {"name": "project.name"}]
|
||||
columns: list[ResultSetColumnType] = [
|
||||
{"column_name": "limit", "name": "limit", "type": "STRING", "is_dttm": False},
|
||||
{"column_name": "name", "name": "name", "type": "STRING", "is_dttm": False},
|
||||
{
|
||||
"column_name": "project.name",
|
||||
"name": "project.name",
|
||||
"type": "STRING",
|
||||
"is_dttm": False,
|
||||
},
|
||||
]
|
||||
fields = BigQueryEngineSpec._get_fields(columns)
|
||||
|
||||
query = select(fields)
|
||||
@@ -84,8 +94,9 @@ def test_select_star(mocker: MockFixture) -> None:
|
||||
"""
|
||||
from superset.db_engine_specs.bigquery import BigQueryEngineSpec
|
||||
|
||||
cols = [
|
||||
cols: list[ResultSetColumnType] = [
|
||||
{
|
||||
"column_name": "trailer",
|
||||
"name": "trailer",
|
||||
"type": sqltypes.ARRAY(sqltypes.JSON()),
|
||||
"nullable": True,
|
||||
@@ -94,8 +105,10 @@ def test_select_star(mocker: MockFixture) -> None:
|
||||
"precision": None,
|
||||
"scale": None,
|
||||
"max_length": None,
|
||||
"is_dttm": False,
|
||||
},
|
||||
{
|
||||
"column_name": "trailer.key",
|
||||
"name": "trailer.key",
|
||||
"type": sqltypes.String(),
|
||||
"nullable": True,
|
||||
@@ -104,8 +117,10 @@ def test_select_star(mocker: MockFixture) -> None:
|
||||
"precision": None,
|
||||
"scale": None,
|
||||
"max_length": None,
|
||||
"is_dttm": False,
|
||||
},
|
||||
{
|
||||
"column_name": "trailer.value",
|
||||
"name": "trailer.value",
|
||||
"type": sqltypes.String(),
|
||||
"nullable": True,
|
||||
@@ -114,8 +129,10 @@ def test_select_star(mocker: MockFixture) -> None:
|
||||
"precision": None,
|
||||
"scale": None,
|
||||
"max_length": None,
|
||||
"is_dttm": False,
|
||||
},
|
||||
{
|
||||
"column_name": "trailer.email",
|
||||
"name": "trailer.email",
|
||||
"type": sqltypes.String(),
|
||||
"nullable": True,
|
||||
@@ -124,6 +141,7 @@ def test_select_star(mocker: MockFixture) -> None:
|
||||
"precision": None,
|
||||
"scale": None,
|
||||
"max_length": None,
|
||||
"is_dttm": False,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ from pyhive.sqlalchemy_presto import PrestoDialect
|
||||
from sqlalchemy import sql, text, types
|
||||
from sqlalchemy.engine.url import make_url
|
||||
|
||||
from superset.superset_typing import ResultSetColumnType
|
||||
from superset.utils.core import GenericDataType
|
||||
from tests.unit_tests.db_engine_specs.utils import (
|
||||
assert_column_spec,
|
||||
@@ -131,7 +132,14 @@ def test_where_latest_partition(
|
||||
mock_latest_partition.return_value = (["partition_key"], [column_value])
|
||||
|
||||
query = sql.select(text("* FROM table"))
|
||||
columns = [{"name": "partition_key", "type": column_type}]
|
||||
columns: list[ResultSetColumnType] = [
|
||||
{
|
||||
"column_name": "partition_key",
|
||||
"name": "partition_key",
|
||||
"type": column_type,
|
||||
"is_dttm": False,
|
||||
}
|
||||
]
|
||||
|
||||
expected = f"""SELECT * FROM table \nWHERE "partition_key" = {expected_value}"""
|
||||
result = spec.where_latest_partition(
|
||||
|
||||
45
tests/unit_tests/queries/dao_test.py
Normal file
45
tests/unit_tests/queries/dao_test.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import json
|
||||
|
||||
|
||||
def test_column_attributes_on_query():
|
||||
from superset.daos.query import QueryDAO
|
||||
from superset.models.core import Database
|
||||
from superset.models.sql_lab import Query
|
||||
|
||||
db = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
||||
query_obj = Query(
|
||||
client_id="foo",
|
||||
database=db,
|
||||
tab_name="test_tab",
|
||||
sql_editor_id="test_editor_id",
|
||||
sql="select * from bar",
|
||||
select_sql="select * from bar",
|
||||
executed_sql="select * from bar",
|
||||
limit=100,
|
||||
select_as_cta=False,
|
||||
rows=100,
|
||||
error_message="none",
|
||||
results_key="abc",
|
||||
)
|
||||
|
||||
columns = [{"name": "test", "is_dttm": False, "type": "INT"}]
|
||||
payload = {"columns": columns}
|
||||
|
||||
QueryDAO.save_metadata(query_obj, payload)
|
||||
assert "column_name" in json.loads(query_obj.extra_json).get("columns")[0]
|
||||
Reference in New Issue
Block a user