From 6bfb689ed4046194690b5ea0a6a0fdf83dead582 Mon Sep 17 00:00:00 2001 From: Yongjie Zhao Date: Thu, 3 Jun 2021 01:20:47 +0100 Subject: [PATCH] feat: add type_generic and is_dttm to table metadata (#14863) --- .pylintrc | 2 +- .../datasource/DatasourceEditor_spec.jsx | 3 +++ .../src/datasource/DatasourceEditor.jsx | 8 +++++++- superset/connectors/sqla/models.py | 16 ++++++++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.pylintrc b/.pylintrc index 53a9b2ffd2f1a..49b3984d1943d 100644 --- a/.pylintrc +++ b/.pylintrc @@ -81,7 +81,7 @@ confidence= # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" -disable=long-builtin,dict-view-method,intern-builtin,suppressed-message,no-absolute-import,unpacking-in-except,apply-builtin,delslice-method,indexing-exception,old-raise-syntax,print-statement,cmp-builtin,reduce-builtin,useless-suppression,coerce-method,input-builtin,cmp-method,raw_input-builtin,nonzero-method,backtick,basestring-builtin,setslice-method,reload-builtin,oct-method,map-builtin-not-iterating,execfile-builtin,old-octal-literal,zip-builtin-not-iterating,buffer-builtin,getslice-method,metaclass-assignment,xrange-builtin,long-suffix,round-builtin,range-builtin-not-iterating,next-method-called,parameter-unpacking,unicode-builtin,unichr-builtin,import-star-module-level,raising-string,filter-builtin-not-iterating,using-cmp-argument,coerce-builtin,file-builtin,old-division,hex-method,missing-docstring,too-many-lines,ungrouped-imports,import-outside-toplevel,raise-missing-from,super-with-arguments,bad-option-value,too-few-public-methods +disable=long-builtin,dict-view-method,intern-builtin,suppressed-message,no-absolute-import,unpacking-in-except,apply-builtin,delslice-method,indexing-exception,old-raise-syntax,print-statement,cmp-builtin,reduce-builtin,useless-suppression,coerce-method,input-builtin,cmp-method,raw_input-builtin,nonzero-method,backtick,basestring-builtin,setslice-method,reload-builtin,oct-method,map-builtin-not-iterating,execfile-builtin,old-octal-literal,zip-builtin-not-iterating,buffer-builtin,getslice-method,metaclass-assignment,xrange-builtin,long-suffix,round-builtin,range-builtin-not-iterating,next-method-called,parameter-unpacking,unicode-builtin,unichr-builtin,import-star-module-level,raising-string,filter-builtin-not-iterating,using-cmp-argument,coerce-builtin,file-builtin,old-division,hex-method,missing-docstring,too-many-lines,ungrouped-imports,import-outside-toplevel,raise-missing-from,super-with-arguments,bad-option-value,too-few-public-methods,too-many-locals [REPORTS] diff --git a/superset-frontend/spec/javascripts/datasource/DatasourceEditor_spec.jsx b/superset-frontend/spec/javascripts/datasource/DatasourceEditor_spec.jsx index a9e1454b0e4fa..2ab5ea296bd59 100644 --- a/superset-frontend/spec/javascripts/datasource/DatasourceEditor_spec.jsx +++ b/superset-frontend/spec/javascripts/datasource/DatasourceEditor_spec.jsx @@ -86,6 +86,7 @@ describe('DatasourceEditor', () => { nullable: true, default: '', primary_key: false, + is_dttm: true, }, { name: 'gender', @@ -93,6 +94,7 @@ describe('DatasourceEditor', () => { nullable: true, default: '', primary_key: false, + is_dttm: false, }, { name: 'new_column', @@ -100,6 +102,7 @@ describe('DatasourceEditor', () => { nullable: true, default: '', primary_key: false, + is_dttm: false, }, ]; diff --git a/superset-frontend/src/datasource/DatasourceEditor.jsx b/superset-frontend/src/datasource/DatasourceEditor.jsx index f41b6980f6213..5727fb946fe98 100644 --- a/superset-frontend/src/datasource/DatasourceEditor.jsx +++ b/superset-frontend/src/datasource/DatasourceEditor.jsx @@ -391,6 +391,7 @@ class DatasourceEditor extends React.PureComponent { } setColumns(obj) { + // update calculatedColumns or databaseColumns this.setState(obj, this.validateAndChange); } @@ -426,13 +427,18 @@ class DatasourceEditor extends React.PureComponent { type: col.type, groupby: true, filterable: true, + is_dttm: col.is_dttm, }); results.added.push(col.name); - } else if (currentCol.type !== col.type) { + } else if ( + currentCol.type !== col.type || + currentCol.is_dttm !== col.is_dttm + ) { // modified column finalColumns.push({ ...currentCol, type: col.type, + is_dttm: col.is_dttm, }); results.modified.push(col.name); } else { diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 82cb1e265dd17..3c98ee5a4c32f 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -681,13 +681,25 @@ def external_metadata(self) -> List[Dict[str, str]]: for col in cols: try: if isinstance(col["type"], TypeEngine): - col["type"] = db_engine_spec.column_datatype_to_string( + db_type = db_engine_spec.column_datatype_to_string( col["type"], db_dialect ) + type_spec = db_engine_spec.get_column_spec(db_type) + col.update( + { + "type": db_type, + "type_generic": type_spec.generic_type + if type_spec + else None, + "is_dttm": type_spec.is_dttm if type_spec else None, + } + ) # Broad exception catch, because there are multiple possible exceptions # from different drivers that fall outside CompileError except Exception: # pylint: disable=broad-except - col["type"] = "UNKNOWN" + col.update( + {"type": "UNKNOWN", "generic_type": None, "is_dttm": None,} + ) return cols @property