improve ingestion system to match isssteleon setup
This commit is contained in:
@@ -7,12 +7,13 @@ from typing import Any, ClassVar, Iterator
|
||||
|
||||
import pandas as pd
|
||||
import sqlalchemy as sa
|
||||
import sqlglot
|
||||
from sqlalchemy.pool import NullPool
|
||||
|
||||
from webapi.imports.connectors.base import Connector, PreviewResult, SourceColumn
|
||||
from webapi.imports.connectors.errors import ConnectorError
|
||||
from webapi.imports.connectors.registry import register_connector
|
||||
from webapi.imports.connectors.sql_policy import SqlSourcePolicy
|
||||
from webapi.imports.connectors.sql_policy import SQLGLOT_DIALECTS, SqlSourcePolicy
|
||||
from webapi.imports.connectors.tabular import infer_type
|
||||
|
||||
|
||||
@@ -222,7 +223,7 @@ class SqlConnector(Connector):
|
||||
if self._table is not None:
|
||||
return sa.select(self._table).subquery("source")
|
||||
if self._query:
|
||||
return _text_subquery(self._query)
|
||||
return _text_subquery(self._query, self.get_dialect())
|
||||
raise ConnectorError("no hay tabla ni consulta seleccionada")
|
||||
|
||||
def _columns(self, names: list[str], rows: list[dict[str, Any]]) -> list[SourceColumn]:
|
||||
@@ -258,11 +259,11 @@ _ADDRESS_PORT_SEPARATORS = (",", ":")
|
||||
|
||||
def _parse_server_address(raw: str) -> tuple[str, str | None, int | None]:
|
||||
"""Tolerates the ways a server address arrives typed or pasted from
|
||||
SSMS, a JDBC string or a connection URI instead of a bare host: a
|
||||
a SQL client, a JDBC string or a connection URI instead of a bare host: a
|
||||
leading scheme (`jdbc:sqlserver://`, `postgresql://`, `tcp:`), a
|
||||
trailing path/query/property list, surrounding whitespace, a SQL
|
||||
Server named instance (`host\\instance`), and a port given as
|
||||
`host:port` or the SSMS convention `host,port` / `host\\instance,port`.
|
||||
`host:port` or SQL Server's `host,port` / `host\\instance,port`.
|
||||
|
||||
Returns `(host, instance, port)`, with `instance` and `port` `None`
|
||||
when absent, so the caller can leave the port unset for a named
|
||||
@@ -298,12 +299,41 @@ def _parse_server_address(raw: str) -> tuple[str, str | None, int | None]:
|
||||
return host, (instance or None), port
|
||||
|
||||
|
||||
def _text_subquery(query: str) -> sa.Subquery:
|
||||
def _text_subquery(query: str, dialect: str | None = None) -> sa.Subquery:
|
||||
"""Wraps policy-checked SELECT text as a derived table, so SQLAlchemy
|
||||
composes LIMIT/TOP and COUNT(*) for whichever dialect the source speaks."""
|
||||
query = _without_trailing_order_by(query, dialect)
|
||||
return sa.select(sa.literal_column("*")).select_from(sa.text(f"({query}) AS source_query")).subquery("source")
|
||||
|
||||
|
||||
_ORDER_BY = re.compile(r"\bORDER\s+BY\b", re.I)
|
||||
|
||||
|
||||
def _without_trailing_order_by(query: str, dialect: str | None) -> str:
|
||||
"""Drops a top-level `ORDER BY` that nothing limits: SQL Server refuses
|
||||
one inside a derived table, and row order means nothing to an import.
|
||||
Kept when TOP, LIMIT or OFFSET makes it choose the rows."""
|
||||
try:
|
||||
[root] = sqlglot.parse(query, read=SQLGLOT_DIALECTS.get(dialect, dialect))
|
||||
except Exception:
|
||||
return query
|
||||
if root is None or not root.args.get("order") or root.args.get("limit") or root.args.get("offset"):
|
||||
return query
|
||||
depth, quote, cut = 0, None, None
|
||||
for position, char in enumerate(query):
|
||||
if quote:
|
||||
quote = None if char == quote else quote
|
||||
elif char in "'\"[`":
|
||||
quote = "]" if char == "[" else char
|
||||
elif char == "(":
|
||||
depth += 1
|
||||
elif char == ")":
|
||||
depth -= 1
|
||||
elif depth == 0 and _ORDER_BY.match(query, position):
|
||||
cut = position
|
||||
return query[:cut].rstrip() if cut is not None else query
|
||||
|
||||
|
||||
def _sa_type_to_kind(sa_type: sa.types.TypeEngine) -> str:
|
||||
try:
|
||||
python_type = sa_type.python_type
|
||||
|
||||
Reference in New Issue
Block a user