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

Add software tests using Polars #35

Merged
merged 1 commit into from
Jan 29, 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ release = [
test = [
"crate[sqlalchemy]",
"pandas<2.3",
"polars[pyarrow]<0.21",
"pytest<9",
"pytest-asyncio<1",
"pytest-cov<5",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cratedb_polars_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys

import polars as pl
import pytest
import sqlalchemy as sa
from polars.testing import assert_frame_equal

REFERENCE_FRAME = pl.from_records([{"mountain": "Mont Blanc", "height": 4808}])
SQL_SELECT_STATEMENT = "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 1;"


if sys.version_info < (3, 8):
pytest.skip("Does not work on Python 3.7", allow_module_level=True)


def test_crate_read_sql(cratedb_http_host, cratedb_http_port):
engine = sa.create_engine(
url=f"crate://{cratedb_http_host}:{cratedb_http_port}",
echo=True,
)
conn = engine.connect()
df = pl.read_database(
query=SQL_SELECT_STATEMENT,
connection=conn,
schema_overrides={"value": pl.Utf8},
)
assert_frame_equal(df, REFERENCE_FRAME)


def test_psycopg_read_sql(cratedb_psql_host, cratedb_psql_port):
engine = sa.create_engine(
url=f"postgresql+psycopg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}",
isolation_level="AUTOCOMMIT",
use_native_hstore=False,
echo=True,
)
conn = engine.connect()
df = pl.read_database(
query=SQL_SELECT_STATEMENT,
connection=conn,
schema_overrides={"value": pl.Utf8},
)
assert_frame_equal(df, REFERENCE_FRAME)
Comment on lines +30 to +43
Copy link
Member Author

@amotl amotl Jan 29, 2024

Choose a reason for hiding this comment

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

Dear @surister,

I am only now reading your comment over at crate/crate#12952 (comment). Can I humbly ask you to check whether the operation on 600K rows worth of data is faster using the Psycopg3 driver, compared to the HTTP/JSON-based driver from crate-python?

pip install 'polars[pyarrow]<0.21' sqlalchemy-postgresql-relaxed
import polars as pl
import sqlalchemy as sa

engine = sa.create_engine("postgresql+psycopg_relaxed://crate@localhost/doc")

df = pl.read_database(
    query="SELECT * FROM metrics",
    connection=engine,
    schema_overrides={"value": pl.Utf8},
)
print(df)

With kind regards,
Andreas.

NB: This will not run the CrateDB SQLAlchemy dialect though, so it will probably not support any special data types yet. There is another patch which will bring everything together correspondingly.

This patch will, for example, unlock connectivity using the CrateDB SQLAlchemy dialect together with the Psycopg3 database driver.

engine = sa.create_engine("crate+psycopg://crate@localhost/doc")

Copy link

@surister surister Feb 6, 2024

Choose a reason for hiding this comment

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

  • read_database - postgresql+psycopg_relaxed 8.5s
  • read_database_uri - postgresql 5.13s
  • read_database - crate 8.8s