From 85f266156a0ed594577c27d3810c22f31f38cf5a Mon Sep 17 00:00:00 2001 From: lcm <1137909852@qq.com> Date: Mon, 19 Jul 2021 20:05:08 -0700 Subject: [PATCH] add materialize --- superset/config.py | 1 + superset/databases/api.py | 2 +- superset/datasets/commands/create.py | 1 - superset/models/core.py | 50 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/superset/config.py b/superset/config.py index 097341a3e..45cfdb44e 100644 --- a/superset/config.py +++ b/superset/config.py @@ -1090,6 +1090,7 @@ def CSV_TO_HIVE_UPLOAD_DIRECTORY_FUNC( "MySQL", "SQLite", "Neo4j", + "Materialize", # etc. ] diff --git a/superset/databases/api.py b/superset/databases/api.py index 0ca2865d6..3a9cb58fd 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -919,7 +919,7 @@ def available(self) -> Response: preferred_databases: List[str] = app.config.get("PREFERRED_DATABASES", []) #print(preferred_databases) available_databases = [] - #print(get_available_engine_specs().items()) + print(get_available_engine_specs().items()) for engine_spec, drivers in get_available_engine_specs().items(): payload: Dict[str, Any] = { "name": engine_spec.engine_name, diff --git a/superset/datasets/commands/create.py b/superset/datasets/commands/create.py index 529bcd4ab..b9ff14af7 100644 --- a/superset/datasets/commands/create.py +++ b/superset/datasets/commands/create.py @@ -53,7 +53,6 @@ def run(self) -> Model: print(dataset.schema_perm) # Updates columns and metrics from the dataset if str(self._properties['database']) == 'Neo4j': - print("We are here now!") security_manager.add_permission_view_menu( "datasource_access", dataset.get_perm() ) diff --git a/superset/models/core.py b/superset/models/core.py index ab163a062..941b10699 100755 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -19,6 +19,7 @@ import enum import json import logging +from os import name import textwrap # import py2neo here from py2neo import * @@ -73,6 +74,19 @@ PASSWORD_MASK = "X" * 10 DB_CONNECTION_MUTATOR = config["DB_CONNECTION_MUTATOR"] +class ViewColumn(): + def __init__(self, name, is_default, type): + self.name = name + self.is_default = is_default + self.type = type + +class VIEWS_SOURCES(): + name: str + columns: list + def __init__(self, name, columns): + self.name = name + self.columns = columns + class Url(Model, AuditMixinNullable): """Used for the short url feature""" @@ -513,6 +527,14 @@ def database_kind(self) -> bool: database_kind = False return database_kind + @property + def is_materialize(self) -> bool: + if str(self.sqlalchemy_uri).endswith('materialize'): + print('Is Materialize database.') + return True + else: + return False + @cache_util.memoized_func( key=lambda self, *args, **kwargs: f"db:{self.id}:schema:None:table_list", cache=cache_manager.data_cache, @@ -668,6 +690,34 @@ def get_table(self, table_name: str, schema: Optional[str] = None) -> Table: meta = MetaData(**extra.get("metadata_params", {})) if self.database_kind: return self.get_sqla_engine() + elif self.is_materialize: + sql1 = 'SHOW SOURCES;' + sql2 = 'SHOW VIEWS;' + r1 = self.get_sqla_engine().execute(sql1) + r2 = self.get_sqla_engine().execute(sql2) + print("SOURCES: ", r1) + print("VIEWS: ", r2) + table = [] + for source in r1: + table.append(source[0]) + for view in r2: + table.append(view[0]) + if table_name not in table: + raise Exception("Entered Table Not Found") + else: + sql = 'SHOW COLUMNS FROM ' + table_name + ';' + res = self.get_sqla_engine().execute(sql) + columns = [] + for column in res: + columns.append( + ViewColumn( + column[0], column[1], column[2] + ) + ) + return VIEWS_SOURCES( + name = table_name, + columns = columns + ) else: return Table( table_name,