fix(dwh): validate adapter limits strictly
This commit is contained in:
@@ -4,6 +4,7 @@ from dataclasses import dataclass
|
||||
from sqlalchemy import Engine, text
|
||||
|
||||
from tht.config import ExamplesConfig, LshConfig
|
||||
from tht.execute import require_positive_int
|
||||
from tht.mschema.models import Annotations, PhysicalSchema
|
||||
from tht.ports.dwh import DistinctValues
|
||||
|
||||
@@ -26,8 +27,7 @@ def _quoted_top_values_query(engine: Engine, schema: str, table: str, column: st
|
||||
def sample_column(
|
||||
engine: Engine, schema: str, table: str, column: str, *, limit: int
|
||||
) -> list[object]:
|
||||
if limit <= 0:
|
||||
raise ValueError("limit must be a positive integer")
|
||||
limit = require_positive_int(limit, name="limit")
|
||||
query = _quoted_top_values_query(engine, schema, table, column)
|
||||
with engine.connect() as conn:
|
||||
rows = conn.execute(query, {"lim": limit}).fetchall()
|
||||
@@ -37,8 +37,7 @@ def sample_column(
|
||||
def sample_column_rest(
|
||||
client, schema: str, table: str, column: str, *, limit: int
|
||||
) -> list[object]:
|
||||
if limit <= 0:
|
||||
raise ValueError("limit must be a positive integer")
|
||||
limit = require_positive_int(limit, name="limit")
|
||||
rows = client.top_values(schema, table, column, limit)
|
||||
return [row["value"] for row in rows if row.get("value") is not None]
|
||||
|
||||
@@ -51,6 +50,7 @@ def distinct_values(
|
||||
*,
|
||||
max_values: int = DEFAULT_DISTINCT_VALUES_LIMIT,
|
||||
) -> DistinctValues:
|
||||
max_values = require_positive_int(max_values, name="max_values")
|
||||
values = sample_column(engine, schema, table, column, limit=max_values + 1)
|
||||
return DistinctValues(values=values[:max_values], truncated=len(values) > max_values)
|
||||
|
||||
@@ -63,6 +63,7 @@ def distinct_values_rest(
|
||||
*,
|
||||
max_values: int = DEFAULT_DISTINCT_VALUES_LIMIT,
|
||||
) -> DistinctValues:
|
||||
max_values = require_positive_int(max_values, name="max_values")
|
||||
values = sample_column_rest(client, schema, table, column, limit=max_values + 1)
|
||||
return DistinctValues(values=values[:max_values], truncated=len(values) > max_values)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user