Skip to content

Commit

Permalink
fix core api with plain old select
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrinot committed Jan 14, 2025
1 parent 70a778c commit fbef995
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/questdb_connect/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def group_by_clause(self, select, **kw):
text = ""

# Add SAMPLE BY first if present
if select._sample_by_clause is not None:
if _has_sample_by(select):
text += " " + self.process(select._sample_by_clause, **kw)

# Use parent's GROUP BY implementation
Expand All @@ -88,7 +88,7 @@ def visit_select(self, select, **kw):
# If we have SAMPLE BY but no GROUP BY,
# add a dummy GROUP BY clause to trigger the rendering
if (
select._sample_by_clause is not None
_has_sample_by(select)
and not select._group_by_clauses
):
select = select._clone()
Expand All @@ -103,3 +103,6 @@ def _is_safe_for_fast_insert_values_helper(self):
def visit_textclause(self, textclause, add_to_result_map=None, **kw):
textclause.text = remove_public_schema(textclause.text)
return super().visit_textclause(textclause, add_to_result_map, **kw)

def _has_sample_by(select):
return hasattr(select, '_sample_by_clause') and select._sample_by_clause is not None
34 changes: 34 additions & 0 deletions tests/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,40 @@ def test_sample_by_from_to(test_engine, test_model):
if session:
session.close()

def test_plain_select_core_api(test_engine, test_model):
"""
Test plain select with core API. Plain select means select implementation from sqlalchemy.sql.selectable,
not from questdb_connect.
"""

session = Session(test_engine)
try:
num_rows = 3
models = [
test_model(
col_int=idx,
col_ts=datetime.datetime(2023, 4, 12, 0, 0, 0) + datetime.timedelta(hours=idx),
) for idx in range(num_rows)
]
session.bulk_save_objects(models)
session.commit()

metadata = sqla.MetaData()
table = sqla.Table(ALL_TYPES_TABLE_NAME, metadata, autoload_with=test_engine)
wait_until_table_is_ready(test_engine, ALL_TYPES_TABLE_NAME, len(models))

with test_engine.connect() as conn:
query = (
# important: use sqla.select, not questdb_connect.select!
sqla.select(table.c.col_ts, table.c.col_int)
)
result = conn.execute(query)
rows = result.fetchall()
assert len(rows) == 3
finally:
if session:
session.close()

def test_sample_by_options(test_engine, test_model):
"""Test SAMPLE BY with ALIGN TO and FILL options."""
base_ts = datetime.datetime(2023, 4, 12, 0, 0, 0)
Expand Down

0 comments on commit fbef995

Please sign in to comment.