Skip to content

Commit

Permalink
refactor(clickhouse): move ClickhouseTable.insert method to clickho…
Browse files Browse the repository at this point in the history
…use backend and remove `ClickhouseTable` class

BREAKING CHANGE: `ClickhouseTable` is removed. This class only provided a single `insert` method. Use the Clickhouse backend's `insert` method instead.
  • Loading branch information
cpcloud authored and jcrist committed Aug 29, 2023
1 parent f9f54e5 commit c9c72ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
40 changes: 17 additions & 23 deletions ibis/backends/clickhouse/__init__.py
Expand Up @@ -38,28 +38,6 @@ def _to_memtable(v):
return ibis.memtable(v).op() if not isinstance(v, ops.InMemoryTable) else v


class ClickhouseTable(ir.Table):
"""References a physical table in Clickhouse."""

@property
def _client(self):
return self.op().source

@property
def name(self):
return self.op().name

def insert(self, obj, settings: Mapping[str, Any] | None = None, **kwargs):
import pandas as pd

if not isinstance(obj, pd.DataFrame):
raise com.IbisError(
f"Invalid input type {type(obj)}; only pandas DataFrames are accepted as input"
)

return self._client.con.insert_df(self.name, obj, settings=settings, **kwargs)


class Backend(BaseBackend, CanCreateDatabase):
name = "clickhouse"

Expand Down Expand Up @@ -437,7 +415,23 @@ def table(self, name: str, database: str | None = None) -> ir.Table:
"""
schema = self.get_schema(name, database=database)
qname = self._fully_qualified_name(name, database)
return ClickhouseTable(ops.DatabaseTable(qname, schema, self))
return ops.DatabaseTable(qname, schema, self).to_expr()

def insert(
self,
name: str,
obj: pd.DataFrame,
settings: Mapping[str, Any] | None = None,
**kwargs: Any,
):
import pandas as pd

if not isinstance(obj, pd.DataFrame):
raise com.IbisError(
f"Invalid input type {type(obj)}; only pandas DataFrames are accepted as input"
)

return self.con.insert_df(name, obj, settings=settings, **kwargs)

def raw_sql(
self,
Expand Down
12 changes: 6 additions & 6 deletions ibis/backends/clickhouse/tests/test_client.py
Expand Up @@ -125,32 +125,32 @@ def temporary_alltypes(con):
con.drop_table(table)


def test_insert(temporary_alltypes, df):
def test_insert(con, temporary_alltypes, df):
temporary = temporary_alltypes
records = df[:10]

assert temporary.count().execute() == 0
temporary.insert(records)
con.insert(temporary.op().name, records)

tm.assert_frame_equal(temporary.execute(), records)


def test_insert_with_less_columns(temporary_alltypes, df):
def test_insert_with_less_columns(con, temporary_alltypes, df):
temporary = temporary_alltypes
records = df.loc[:10, ["string_col"]].copy()
records["date_col"] = None

with pytest.raises(cc.driver.exceptions.ProgrammingError):
temporary.insert(records)
con.insert(temporary.op().name, records)


def test_insert_with_more_columns(temporary_alltypes, df):
def test_insert_with_more_columns(con, temporary_alltypes, df):
temporary = temporary_alltypes
records = df[:10].copy()
records["non_existing_column"] = "raise on me"

with pytest.raises(cc.driver.exceptions.ProgrammingError):
temporary.insert(records)
con.insert(temporary.op().name, records)


@pytest.mark.parametrize(
Expand Down

1 comment on commit c9c72ae

@ibis-squawk-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 3.

Benchmark suite Current: c9c72ae Previous: 4c6687e Ratio
ibis/tests/benchmarks/test_benchmarks.py::test_compile[small-impala] 1662.359688284731 iter/sec (stddev: 0.008572025211092292) 12504.974539900828 iter/sec (stddev: 0.000015348002454192702) 7.52

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.