Skip to content

Commit

Permalink
return Result<> from lf_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
billylanchantin committed Jun 17, 2024
1 parent 2e232e8 commit 1882109
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/explorer/polars_backend/lazy_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,13 @@ defmodule Explorer.PolarsBackend.LazyFrame do

@impl true
def sql(ldf, sql_string, table_name) do
ldf.data
|> Native.lf_sql(sql_string, table_name)
|> Shared.create_dataframe()
with {:ok, polars_lf} <- Native.lf_sql(ldf.data, sql_string, table_name),
{:ok, names} <- Native.lf_names(polars_lf),
{:ok, dtypes} <- Native.lf_dtypes(polars_lf) do
Explorer.Backend.DataFrame.new(polars_lf, names, dtypes)
else
{:error, polars_error} -> raise polars_error
end
end

@impl true
Expand Down
12 changes: 9 additions & 3 deletions native/explorer/src/lazyframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,18 @@ pub fn lf_concat_columns(
}

#[rustler::nif]
pub fn lf_sql(lf: ExLazyFrame, sql_string: &str, table_name: &str) -> ExLazyFrame {
pub fn lf_sql(
lf: ExLazyFrame,
sql_string: &str,
table_name: &str,
) -> Result<ExLazyFrame, ExplorerError> {
let mut ctx = polars::sql::SQLContext::new();

let lf = lf.clone_inner();
ctx.register(table_name, lf);

let lf_sql = ctx.execute(sql_string).unwrap();
ExLazyFrame::new(lf_sql)
match ctx.execute(sql_string) {
Ok(lf_sql) => Ok(ExLazyFrame::new(lf_sql)),
Err(polars_error) => Err(ExplorerError::Polars(polars_error)),
}
}

0 comments on commit 1882109

Please sign in to comment.