diff --git a/lib/explorer/polars_backend/lazy_frame.ex b/lib/explorer/polars_backend/lazy_frame.ex index 378b09c44..43fe0ce59 100644 --- a/lib/explorer/polars_backend/lazy_frame.ex +++ b/lib/explorer/polars_backend/lazy_frame.ex @@ -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 diff --git a/native/explorer/src/lazyframe.rs b/native/explorer/src/lazyframe.rs index 8ae120786..c6193095b 100644 --- a/native/explorer/src/lazyframe.rs +++ b/native/explorer/src/lazyframe.rs @@ -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 { 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)), + } }