Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load table warnings from metastore #1317

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions querybook/server/const/metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class DataOwner(NamedTuple):
type: str = None


class DataTableWarningSeverity(Enum):
WARNING = 0
ERROR = 1


class DataTable(NamedTuple):
name: str

Expand Down Expand Up @@ -73,6 +78,9 @@ class DataTable(NamedTuple):
golden: bool = False
boost_score: float = 1

# table warnings
warnings: list[tuple[DataTableWarningSeverity, str]] = None


class DataColumn(NamedTuple):
name: str
Expand All @@ -91,11 +99,6 @@ class DataColumn(NamedTuple):
data_element: DataElementAssociationTuple = None


class DataTableWarningSeverity(Enum):
WARNING = 0
ERROR = 1


class MetadataType(Enum):
TABLE_DESCRIPTION = "table_description"
COLUMN_DESCRIPTION = "column_description"
Expand Down
11 changes: 10 additions & 1 deletion querybook/server/lib/metastore/base_metastore_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
create_table,
create_table_information,
create_table_ownerships,
create_table_warnings,
delete_column,
delete_schema,
delete_table,
Expand Down Expand Up @@ -328,7 +329,7 @@ def _create_tables_batched(self, schema_tables):

def _create_tables(self, schema_tables):
with DBSession() as session:
for (schema_id, schema_name, table) in schema_tables:
for schema_id, schema_name, table in schema_tables:
self._create_table_table(schema_id, schema_name, table, session=session)

@with_session
Expand Down Expand Up @@ -380,6 +381,14 @@ def _create_table_table(
custom_properties=table.custom_properties,
session=session,
)
if table.warnings is not None:
create_table_warnings(
table_id=table_id,
warnings=table.warnings,
commit=False,
session=session,
)

delete_column_not_in_metastore(
table_id, set(map(lambda c: c.name, columns)), session=session
)
Expand Down
36 changes: 33 additions & 3 deletions querybook/server/logic/metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from app.db import with_session
from const.elasticsearch import ElasticsearchItem
from const.metastore import DataOwner
from const.metastore import DataOwner, DataTableWarningSeverity
from lib.logger import get_logger
from lib.sqlalchemy import update_model_fields
from logic.user import get_user_by_name
Expand All @@ -17,6 +17,7 @@
DataTableOwnership,
DataTableQueryExecution,
DataTableStatistics,
DataTableWarning,
)
from models.query_execution import QueryExecution
from sqlalchemy import and_, func
Expand Down Expand Up @@ -75,7 +76,6 @@ def get_schema_by_id(schema_id, session=None):

@with_session
def update_schema(schema_id, description, session=None):

schema = get_schema_by_id(schema_id, session=session)

schema.description = description
Expand Down Expand Up @@ -320,6 +320,37 @@ def create_table_information(
return table_information


@with_session
def create_table_warnings(
table_id,
warnings: tuple[DataTableWarningSeverity, str] = [],
commit=False,
session=None,
):
"""This function is used for loading table warnings from metastore.

For warnings from metastore, created_by will be None.
"""
# delete all warnings without created_by from the table
session.query(DataTableWarning).filter_by(
jczhong84 marked this conversation as resolved.
Show resolved Hide resolved
DataTableWarning.created_by is None
).delete()

# add warnings from metastore to the table
for severiy, message in warnings:
jczhong84 marked this conversation as resolved.
Show resolved Hide resolved
DataTableWarning.create(
{
"message": message,
"severity": severiy,
"table_id": table_id,
}
)
if commit:
session.commit()
else:
session.flush()


@with_session
def delete_table(table_id=None, commit=True, session=None):
table = get_table_by_id(table_id=table_id, session=session)
Expand Down Expand Up @@ -525,7 +556,6 @@ def update_column_by_id(
commit=True,
session=None,
):

table_column = get_column_by_id(id, session=session)
if not table_column:
return
Expand Down
Loading