Skip to content

Commit

Permalink
Fix SQL Lab
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Aug 6, 2024
1 parent 03059e8 commit 5e7d979
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
15 changes: 10 additions & 5 deletions superset/commands/dataset/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,27 @@ def run(self) -> Model:
def validate(self) -> None:
exceptions: list[ValidationError] = []
database_id = self._properties["database"]
schema = self._properties.get("schema")
catalog = self._properties.get("catalog")
schema = self._properties.get("schema")
table_name = self._properties["table_name"]
sql = self._properties.get("sql")
owner_ids: Optional[list[int]] = self._properties.get("owners")

table = Table(self._properties["table_name"], schema, catalog)

# Validate/Populate database
database = DatasetDAO.get_database_by_id(database_id)
if not database:
exceptions.append(DatabaseNotFoundValidationError())
self._properties["database"] = database

# Validate uniqueness
if database and not DatasetDAO.validate_uniqueness(database, table):
exceptions.append(DatasetExistsValidationError(table))
if database:
if not catalog:
catalog = self._properties["catalog"] = database.get_default_catalog()

table = Table(table_name, schema, catalog)

if not DatasetDAO.validate_uniqueness(database, table):
exceptions.append(DatasetExistsValidationError(table))

# Validate table exists on dataset if sql is not provided
# This should be validated when the dataset is physical
Expand Down
8 changes: 7 additions & 1 deletion superset/commands/dataset/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ def validate(self) -> None:

database_id = self._properties.get("database")

catalog = self._properties.get("catalog")
if not catalog:
catalog = self._properties["catalog"] = (
self._model.database.get_default_catalog()
)

table = Table(
self._properties.get("table_name"), # type: ignore
self._properties.get("schema"),
self._properties.get("catalog"),
catalog,
)

# Validate uniqueness
Expand Down
2 changes: 1 addition & 1 deletion superset/databases/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ def select_star(
self.incr_stats("init", self.select_star.__name__)
try:
result = database.select_star(
Table(table_name, schema_name),
Table(table_name, schema_name, database.get_default_catalog()),
latest_partition=True,
)
except NoSuchTableError:
Expand Down
3 changes: 2 additions & 1 deletion superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def process_template(self, sql: str, **kwargs: Any) -> str:
kwargs.update(self._context)

context = validate_template_context(self.engine, kwargs)
print("FOO", type(template.render(context)))
return template.render(context)


Expand Down Expand Up @@ -565,7 +566,7 @@ def process_template(self, sql: str, **kwargs: Any) -> str:
"""
Makes processing a template a noop
"""
return sql
return str(sql)


class PrestoTemplateProcessor(JinjaTemplateProcessor):
Expand Down
1 change: 1 addition & 0 deletions superset/sqllab/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def export_csv(self, client_id: str) -> CsvResponse:
"client_id": client_id,
"row_count": row_count,
"database": query.database.name,
"catalog": query.catalog,
"schema": query.schema,
"sql": query.sql,
"exported_format": "csv",
Expand Down
2 changes: 2 additions & 0 deletions superset/sqllab/sqllab_execution_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def select_as_cta(self) -> bool:
def set_database(self, database: Database) -> None:
self._validate_db(database)
self.database = database
if self.catalog is None:
self.catalog = database.get_default_catalog()
if self.select_as_cta:
schema_name = self._get_ctas_target_schema_name(database)
self.create_table_as_select.target_schema_name = schema_name # type: ignore
Expand Down
2 changes: 2 additions & 0 deletions superset/views/sql_lab/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,15 @@ def post(self) -> FlaskResponse:
db.session.query(TableSchema).filter(
TableSchema.tab_state_id == table["queryEditorId"],
TableSchema.database_id == table["dbId"],
TableSchema.catalog == table["catalog"],
TableSchema.schema == table["schema"],
TableSchema.table == table["name"],
).delete(synchronize_session=False)

table_schema = TableSchema(
tab_state_id=table["queryEditorId"],
database_id=table["dbId"],
catalog=table["catalog"],
schema=table["schema"],
table=table["name"],
description=json.dumps(table),
Expand Down

0 comments on commit 5e7d979

Please sign in to comment.