mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
feat(explore): Postgres datatype conversion (#13294)
* test * unnecessary import * fix lint * changes * fix lint * changes * changes * changes * changes * answering comments & changes * answering comments * answering comments * changes * changes * changes * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests
This commit is contained in:
@@ -18,14 +18,28 @@ import json
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
List,
|
||||
Match,
|
||||
Optional,
|
||||
Pattern,
|
||||
Tuple,
|
||||
TYPE_CHECKING,
|
||||
Union,
|
||||
)
|
||||
|
||||
from pytz import _FixedOffset # type: ignore
|
||||
from sqlalchemy.dialects.postgresql import ARRAY, DOUBLE_PRECISION, ENUM, JSON
|
||||
from sqlalchemy.dialects.postgresql.base import PGInspector
|
||||
from sqlalchemy.types import String, TypeEngine
|
||||
|
||||
from superset.db_engine_specs.base import BaseEngineSpec
|
||||
from superset.exceptions import SupersetException
|
||||
from superset.utils import core as utils
|
||||
from superset.utils.core import ColumnSpec, GenericDataType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from superset.models.core import Database # pragma: no cover
|
||||
@@ -77,6 +91,21 @@ class PostgresEngineSpec(PostgresBaseEngineSpec):
|
||||
max_column_name_length = 63
|
||||
try_remove_schema_from_table_name = False
|
||||
|
||||
column_type_mappings = (
|
||||
(
|
||||
re.compile(r"^double precision", re.IGNORECASE),
|
||||
DOUBLE_PRECISION(),
|
||||
GenericDataType.NUMERIC,
|
||||
),
|
||||
(
|
||||
re.compile(r"^array.*", re.IGNORECASE),
|
||||
lambda match: ARRAY(int(match[2])) if match[2] else String(),
|
||||
utils.GenericDataType.STRING,
|
||||
),
|
||||
(re.compile(r"^json.*", re.IGNORECASE), JSON(), utils.GenericDataType.STRING,),
|
||||
(re.compile(r"^enum.*", re.IGNORECASE), ENUM(), utils.GenericDataType.STRING,),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_allow_cost_estimate(cls, extra: Dict[str, Any]) -> bool:
|
||||
return True
|
||||
@@ -144,3 +173,26 @@ class PostgresEngineSpec(PostgresBaseEngineSpec):
|
||||
engine_params["connect_args"] = connect_args
|
||||
extra["engine_params"] = engine_params
|
||||
return extra
|
||||
|
||||
@classmethod
|
||||
def get_column_spec( # type: ignore
|
||||
cls,
|
||||
native_type: Optional[str],
|
||||
source: utils.ColumnTypeSource = utils.ColumnTypeSource.GET_TABLE,
|
||||
column_type_mappings: Tuple[
|
||||
Tuple[
|
||||
Pattern[str],
|
||||
Union[TypeEngine, Callable[[Match[str]], TypeEngine]],
|
||||
GenericDataType,
|
||||
],
|
||||
...,
|
||||
] = column_type_mappings,
|
||||
) -> Union[ColumnSpec, None]:
|
||||
|
||||
column_spec = super().get_column_spec(native_type)
|
||||
if column_spec:
|
||||
return column_spec
|
||||
|
||||
return super().get_column_spec(
|
||||
native_type, column_type_mappings=column_type_mappings
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user