From c9c72aefaa7b84f266ff73efc09562b7619403ec Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Tue, 29 Aug 2023 06:05:10 -0400 Subject: [PATCH] refactor(clickhouse): move `ClickhouseTable.insert` method to clickhouse 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. --- ibis/backends/clickhouse/__init__.py | 40 ++++++++----------- ibis/backends/clickhouse/tests/test_client.py | 12 +++--- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ibis/backends/clickhouse/__init__.py b/ibis/backends/clickhouse/__init__.py index 4bde8c6296be..a9e2d8f6fb0d 100644 --- a/ibis/backends/clickhouse/__init__.py +++ b/ibis/backends/clickhouse/__init__.py @@ -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" @@ -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, diff --git a/ibis/backends/clickhouse/tests/test_client.py b/ibis/backends/clickhouse/tests/test_client.py index e9fd3ac4e011..6a557417b14e 100644 --- a/ibis/backends/clickhouse/tests/test_client.py +++ b/ibis/backends/clickhouse/tests/test_client.py @@ -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(