Skip to content

Commit

Permalink
chore: organize SQL parsing files (apache#30258)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Sep 13, 2024
1 parent 8cd18ca commit bdf29cb
Show file tree
Hide file tree
Showing 13 changed files with 1,650 additions and 886 deletions.
3 changes: 2 additions & 1 deletion superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
from superset.databases.utils import get_table_metadata, make_url_safe
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.exceptions import DisallowedSQLFunction, OAuth2Error, OAuth2RedirectError
from superset.sql_parse import ParsedQuery, SQLScript, Table
from superset.sql.parse import SQLScript, Table
from superset.sql_parse import ParsedQuery
from superset.superset_typing import (
OAuth2ClientConfig,
OAuth2State,
Expand Down
2 changes: 1 addition & 1 deletion superset/db_engine_specs/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.exceptions import SupersetException, SupersetSecurityException
from superset.models.sql_lab import Query
from superset.sql_parse import SQLScript
from superset.sql.parse import SQLScript
from superset.utils import core as utils, json
from superset.utils.core import GenericDataType

Expand Down
27 changes: 24 additions & 3 deletions superset/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from collections import defaultdict
from typing import Any, Optional

Expand Down Expand Up @@ -304,12 +307,30 @@ class SupersetParseError(SupersetErrorException):

status = 422

def __init__(self, sql: str, engine: Optional[str] = None):
def __init__( # pylint: disable=too-many-arguments
self,
sql: str,
engine: Optional[str] = None,
message: Optional[str] = None,
highlight: Optional[str] = None,
line: Optional[int] = None,
column: Optional[int] = None,
):
if message is None:
parts = [_("Error parsing")]
if highlight:
parts.append(_(" near '%(highlight)s'", highlight=highlight))
if line:
parts.append(_(" at line %(line)d", line=line))
if column:
parts.append(_(":%(column)d", column=column))
message = "".join(parts)

error = SupersetError(
message=_("The SQL is invalid and cannot be parsed."),
message=message,
error_type=SupersetErrorType.INVALID_SQL_ERROR,
level=ErrorLevel.ERROR,
extra={"sql": sql, "engine": engine},
extra={"sql": sql, "engine": engine, "line": line, "column": column},
)
super().__init__(error)

Expand Down
3 changes: 1 addition & 2 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@
)
from superset.extensions import feature_flag_manager
from superset.jinja_context import BaseTemplateProcessor
from superset.sql.parse import SQLScript, SQLStatement
from superset.sql_parse import (
has_table_query,
insert_rls_in_predicate,
ParsedQuery,
sanitize_clause,
SQLScript,
SQLStatement,
)
from superset.superset_typing import (
AdhocMetric,
Expand Down
16 changes: 16 additions & 0 deletions superset/sql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Loading

0 comments on commit bdf29cb

Please sign in to comment.