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

feat(snowflake): implement Table.sample #9071

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ibis/backends/snowflake/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis import util
from ibis.backends.sql.compiler import NULL, C, FuncGen, SQLGlotCompiler
from ibis.backends.sql.compiler import NULL, STAR, C, FuncGen, SQLGlotCompiler
from ibis.backends.sql.datatypes import SnowflakeType
from ibis.backends.sql.dialects import Snowflake
from ibis.backends.sql.rewrites import (
Expand Down Expand Up @@ -622,3 +622,14 @@ def visit_TimestampRange(self, op, *, start, stop, step):
)
.subquery()
)

def visit_Sample(
self, op, *, parent, fraction: float, method: str, seed: int | None, **_
):
sample = sge.TableSample(
this=parent,
method="bernoulli" if method == "row" else "system",
percent=sge.convert(fraction * 100.0),
seed=None if seed is None else sge.convert(seed),
)
return sg.select(STAR).from_(sample)
29 changes: 20 additions & 9 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,28 +1923,40 @@ def test_dynamic_table_slice_with_computed_offset(backend):
backend.assert_frame_equal(result, expected)


@pytest.mark.notimpl(["druid", "polars", "snowflake"])
@pytest.mark.notimpl(["druid", "polars"])
@pytest.mark.notimpl(
["risingwave"],
raises=PsycoPg2InternalError,
reason="function random() does not exist",
)
def test_sample(backend):
@pytest.mark.parametrize(
"method",
[
"row",
param(
"block",
marks=[
pytest.mark.notimpl(
["snowflake"],
raises=SnowflakeProgrammingError,
reason="SAMPLE clause on views only supports row wise sampling without seed.",
)
],
),
],
)
def test_sample(backend, method):
t = backend.functional_alltypes.filter(_.int_col >= 2)

total_rows = t.count().execute()
empty = t.limit(1).execute().iloc[:0]

df = t.sample(0.1, method="row").execute()
assert len(df) <= total_rows
backend.assert_frame_equal(empty, df.iloc[:0])

df = t.sample(0.1, method="block").execute()
df = t.sample(0.1, method=method).execute()
assert len(df) <= total_rows
backend.assert_frame_equal(empty, df.iloc[:0])


@pytest.mark.notimpl(["druid", "polars", "snowflake"])
@pytest.mark.notimpl(["druid", "polars"])
@pytest.mark.notimpl(
["risingwave"],
raises=PsycoPg2InternalError,
Expand All @@ -1971,7 +1983,6 @@ def test_sample_memtable(con, backend):
"polars",
"postgres",
"risingwave",
"snowflake",
"sqlite",
"trino",
"exasol",
Expand Down
Loading