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 SQL runner utility .io.sql.run_sql #74

Merged
merged 1 commit into from
Nov 8, 2023
Merged

Add SQL runner utility .io.sql.run_sql #74

merged 1 commit into from
Nov 8, 2023

Conversation

amotl
Copy link
Member

@amotl amotl commented Nov 7, 2023

About

Classical yak shaving.

Details

Similar to pyveci/pueblo#12, this patch adds a generalized testing helper function to avoid code duplication within the educational repository cratedb-examples, originally conceived at 1. We need this utility function at many more spots, so we needed to find a good place to store it.

Synopsis

from pprint import pprint
from cratedb_toolkit.io.sql import run_sql

result = run_sql(
  dburi="crate://crate@localhost:4200",
  sql="SELECT * FROM sys.summits LIMIT 3;",
  records=True,
)
pprint(result)

Notes

Note that details on the interface may change while we go, specifically on the "naming things" / DWIM side of things. If you have any suggestions, feel free to add your voice.

/cc @karynzv, @marijaselakovic, @hlcianfagna, @hammerhead, @WalBeh, @andnig, @ckurze, @vvulf

Footnotes

  1. https://github.com/crate/cratedb-examples/blob/main/topic/machine-learning/llm-langchain/test.py

@amotl amotl force-pushed the amo/add-run-sql branch 2 times, most recently from dfd535f to 71e67ff Compare November 7, 2023 18:00
@amotl amotl changed the title Add cratedb_toolkit.shell.run_sql utility primitive Add cratedb_toolkit.io.sql.run_sql utility primitive Nov 7, 2023
@amotl amotl force-pushed the amo/add-run-sql branch 2 times, most recently from ead3c71 to d604d81 Compare November 7, 2023 18:39
@amotl amotl changed the title Add cratedb_toolkit.io.sql.run_sql utility primitive Add SQL runner utility .io.sql.run_sql Nov 7, 2023
Copy link

codecov bot commented Nov 7, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Files Coverage Δ
cratedb_toolkit/io/sql.py 100.00% <100.00%> (ø)
cratedb_toolkit/util/database.py 91.02% <100.00%> (+6.02%) ⬆️

📢 Thoughts on this report? Let us know!

@amotl amotl requested review from seut and matriv November 7, 2023 18:50
@amotl amotl marked this pull request as ready for review November 7, 2023 18:51
@amotl amotl force-pushed the amo/add-run-sql branch 2 times, most recently from d7bae2d to 108a992 Compare November 7, 2023 19:01
Comment on lines +35 to +43
sql_effective: str
if isinstance(sql, str):
sql_effective = sql
elif isinstance(sql, Path):
sql_effective = sql.read_text()
elif isinstance(sql, io.IOBase):
sql_effective = sql.read()
else:
raise TypeError("SQL statement type must be either string, Path, or IO handle")
Copy link
Member

Choose a reason for hiding this comment

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

Just curious: couldn't this be done by using function overloads with @typing.overload() instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi. I have to admit I never used it. If you think the code can be improved, I will be happy to learn about it.

@amotl amotl merged commit 52e3f1f into main Nov 8, 2023
6 checks passed
@amotl amotl deleted the amo/add-run-sql branch November 8, 2023 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants