Skip to content

Commit

Permalink
feat: add execute method on session (#77)
Browse files Browse the repository at this point in the history
adds .execute(query) on the sesson context so to that
various queries like `COPY` can be run eagerly
  • Loading branch information
tshauck authored Dec 7, 2023
1 parent ae8337f commit e300ab0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 20 additions & 0 deletions python/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ def test_with_error():
session.sql(query)


def test_execute(tmp_path):
"""Test the execute query returns immediately."""

output_path = tmp_path / "output.parquet"

session = connect()

gff_path = DATA / "test.gff"

query = f"CREATE EXTERNAL TABLE gff_file STORED AS GFF LOCATION '{gff_path}'"
session.execute(query)

copy_query = (
f"COPY (SELECT seqname FROM gff_file) TO '{output_path}' (FORMAT PARQUET)"
)
session.execute(copy_query)

assert output_path.exists()


def test_to_record_batch_reader():
"""Test converting to a record batch reader."""
session = connect()
Expand Down
12 changes: 11 additions & 1 deletion src/session_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ impl ExonSessionContext {
Ok(Self::default())
}

/// Execute a SQL query and return the result as a [`PyExecutionResult`].
/// Generate the plan from a SQL query and return the result as a [`PyExecutionResult`].
fn sql(&mut self, query: &str, py: Python) -> PyResult<PyExecutionResult> {
let result = self.ctx.sql(query);
let df = wait_for_future(py, result).map_err(error::BioBearError::from)?;

Ok(PyExecutionResult::new(df))
}

/// Execute the SQL query eagerly, but do not collect the results.
fn execute(&mut self, query: &str, py: Python) -> PyResult<()> {
let result = self.ctx.sql(query);
let df = wait_for_future(py, result).map_err(error::BioBearError::from)?;

wait_for_future(py, df.collect()).map_err(error::BioBearError::from)?;

Ok(())
}

/// Register an object store with the given URI.
fn register_object_store_from_url(&mut self, url: &str, py: Python) -> PyResult<()> {
let runtime = self.ctx.runtime_env();
Expand Down

0 comments on commit e300ab0

Please sign in to comment.