Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query.count() with custom modifiers (sample) generates incorrect query #45

Closed
antonio-antuan opened this issue Jan 29, 2019 · 1 comment

Comments

@antonio-antuan
Copy link
Contributor

session.query(...).count()
That code generates something like that:

select count(*) from (
select ...
from table
) anon_1

If I trying to use sample, query will look like that:

session.query(...).sample(0.1)
select count(*) from (
select ...
from table
sample 0.1
) anon_1
sample 0.1

The same problem appears with ARRAY JOIN

@mrfijas
Copy link

mrfijas commented Nov 22, 2022

hi, wanted to create issue with sampling and array join, but I believe it would be a duplicate of this issue..
I'm using clickhouse-sqlalchemy==0.1.8 and

from sqlalchemy.sql.ddl import CreateTable

from clickhouse_sqlalchemy import engines, types, get_declarative_base, make_session
from sqlalchemy import create_engine, MetaData, Column

from clickhouse_sqlalchemy.drivers.base import ClickHouseDialect, clickhouse_dialect

uri = 'clickhouse+native://default:default@clickhouse:9000'

engine = create_engine(uri)
session = make_session(engine)
metadata = MetaData(bind=engine)

Base = get_declarative_base(metadata=metadata)


class Table(Base):
    id = Column(types.UInt32(), primary_key=True)
    arr = Column(types.Array(types.String))
    __table_args__ = (
        engines.MergeTree(
            order_by="id",
            sample_by="id",
        ),
    )


# print(CreateTable(Table.__table__).compile(dialect=clickhouse_dialect))

q = session.query(Table.arr).sample(0.2).array_join(Table.arr)
print(q.statement.compile(dialect=clickhouse_dialect))

was producing

SELECT "table".arr 
FROM "table" 
ARRAY JOIN "table".arr 
SAMPLE %(param_1)s

instead of

SELECT "table".arr 
FROM "table"
SAMPLE %(param_1)s
ARRAY JOIN "table".arr

when wanted to check it with latest 0.2.2 I got

SELECT "table".arr 
FROM "table"

with no ARRAY JOIN nor SAMPLE.

  1. what am I doing wrong?
  2. is sampling supported when using array join?

xzkostyan added a commit that referenced this issue Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants