Skip to content

Commit

Permalink
chore: move pandas import inside use site
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jan 16, 2025
1 parent 66c0e3a commit 55e510e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
},
{
"addLabels": ["risingwave"],
"matchPackageNames": ["/psycopg/", "/risingwave/"]
"matchPackageNames": ["/psycopg2/", "/risingwave/"]
},
{
"addLabels": ["snowflake"],
Expand Down
35 changes: 4 additions & 31 deletions ibis/backends/risingwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import sqlglot as sg
import sqlglot.expressions as sge
from pandas.api.types import is_float_dtype

import ibis
import ibis.backends.sql.compilers as sc
Expand All @@ -29,6 +28,7 @@

import pandas as pd
import polars as pl
import psycopg2.extensions
import pyarrow as pa


Expand Down Expand Up @@ -105,6 +105,7 @@ def _from_url(self, url: ParseResult, **kwargs):
return self.connect(**kwargs)

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
from pandas.api.types import is_float_dtype
from psycopg2.extras import execute_batch

schema = op.schema
Expand Down Expand Up @@ -528,13 +529,14 @@ def do_connect(
year int32
month int32
"""
import psycopg2

self.con = psycopg2.connect(
host=host,
port=port,
user=user,
password=password,
dbname=database,
database=database,
options=(f"-csearch_path={schema}" * (schema is not None)) or None,
)

Expand Down Expand Up @@ -681,35 +683,6 @@ def create_table(
name, schema=schema, source=self, namespace=ops.Namespace(database=database)
).to_expr()

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
schema = op.schema
if null_columns := [col for col, dtype in schema.items() if dtype.is_null()]:
raise com.IbisTypeError(
f"{self.name} cannot yet reliably handle `null` typed columns; "
f"got null typed columns: {null_columns}"
)

name = op.name
quoted = self.compiler.quoted

create_stmt = sg.exp.Create(
kind="TABLE",
this=sg.exp.Schema(
this=sg.to_identifier(name, quoted=quoted),
expressions=schema.to_sqlglot(self.dialect),
),
)
create_stmt_sql = create_stmt.sql(self.dialect)

df = op.data.to_frame()
data = df.itertuples(index=False)
sql = self._build_insert_template(
name, schema=schema, columns=True, placeholder="%s"
)
with self.begin() as cur:
cur.execute(create_stmt_sql)
cur.executemany(sql, data)

def list_databases(
self, *, like: str | None = None, catalog: str | None = None
) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/risingwave/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestConf(ServiceBackendTest):
supports_structs = False
rounding_method = "half_to_even"
service_name = "risingwave"
deps = ("psycopg",)
deps = ("psycopg2",)

@property
def test_files(self) -> Iterable[Path]:
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/risingwave/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ibis.expr.types as ir
from ibis.util import gen_name

pytest.importorskip("psycopg")
pytest.importorskip("psycopg2")

RISINGWAVE_TEST_DB = os.environ.get("IBIS_TEST_RISINGWAVE_DATABASE", "dev")
IBIS_RISINGWAVE_HOST = os.environ.get("IBIS_TEST_RISINGWAVE_HOST", "localhost")
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/risingwave/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import ibis.expr.datatypes as dt
from ibis import literal as L

pytest.importorskip("psycopg")
pytest.importorskip("psycopg2")


@pytest.mark.parametrize(("value", "expected"), [(0, None), (5.5, 5.5)])
Expand Down
19 changes: 19 additions & 0 deletions ibis/backends/tests/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@
except ImportError:
TrinoUserError = None

try:
from psycopg2.errors import ArraySubscriptError as PsycoPg2ArraySubscriptError
from psycopg2.errors import DivisionByZero as PsycoPg2DivisionByZero
from psycopg2.errors import IndeterminateDatatype as PsycoPg2IndeterminateDatatype
from psycopg2.errors import InternalError_ as PsycoPg2InternalError
from psycopg2.errors import (
InvalidTextRepresentation as PsycoPg2InvalidTextRepresentation,
)
from psycopg2.errors import OperationalError as PsycoPg2OperationalError
from psycopg2.errors import ProgrammingError as PsycoPg2ProgrammingError
from psycopg2.errors import SyntaxError as PsycoPg2SyntaxError
from psycopg2.errors import UndefinedObject as PsycoPg2UndefinedObject
except ImportError:
PsycoPg2SyntaxError = PsycoPg2IndeterminateDatatype = (
PsycoPg2InvalidTextRepresentation
) = PsycoPg2DivisionByZero = PsycoPg2InternalError = PsycoPg2ProgrammingError = (
PsycoPg2OperationalError
) = PsycoPg2UndefinedObject = PsycoPg2ArraySubscriptError = None

try:
from psycopg.errors import ArraySubscriptError as PsycoPgArraySubscriptError
from psycopg.errors import DivisionByZero as PsycoPgDivisionByZero
Expand Down

0 comments on commit 55e510e

Please sign in to comment.