-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for SQL querying - #33
- Loading branch information
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use polars::sql::SQLContext; | ||
use std::cell::RefCell; | ||
|
||
use crate::{RbLazyFrame, RbPolarsErr, RbResult}; | ||
|
||
#[magnus::wrap(class = "Polars::RbSQLContext")] | ||
#[repr(transparent)] | ||
#[derive(Clone)] | ||
pub struct RbSQLContext { | ||
pub context: RefCell<SQLContext>, | ||
} | ||
|
||
#[allow( | ||
clippy::wrong_self_convention, | ||
clippy::should_implement_trait, | ||
clippy::len_without_is_empty | ||
)] | ||
impl RbSQLContext { | ||
#[allow(clippy::new_without_default)] | ||
pub fn new() -> RbSQLContext { | ||
RbSQLContext { | ||
context: SQLContext::new().into(), | ||
} | ||
} | ||
|
||
pub fn execute(&self, query: String) -> RbResult<RbLazyFrame> { | ||
Ok(self | ||
.context | ||
.borrow_mut() | ||
.execute(&query) | ||
.map_err(RbPolarsErr::from)? | ||
.into()) | ||
} | ||
|
||
pub fn get_tables(&self) -> RbResult<Vec<String>> { | ||
Ok(self.context.borrow().get_tables()) | ||
} | ||
|
||
pub fn register(&self, name: String, lf: &RbLazyFrame) { | ||
self.context.borrow_mut().register(&name, lf.ldf.clone()) | ||
} | ||
|
||
pub fn unregister(&self, name: String) { | ||
self.context.borrow_mut().unregister(&name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Polars | ||
class SQLContext | ||
# @private | ||
attr_accessor :_ctxt, :_eager_execution | ||
|
||
def initialize(frames = nil, eager_execution: false, **named_frames) | ||
self._ctxt = RbSQLContext.new | ||
self._eager_execution = eager_execution | ||
|
||
frames = (frames || {}).to_h | ||
|
||
if frames.any? || named_frames.any? | ||
register_many(frames, **named_frames) | ||
end | ||
end | ||
|
||
def execute(query, eager: nil) | ||
res = Utils.wrap_ldf(_ctxt.execute(query)) | ||
eager || _eager_execution ? res.collect : res | ||
end | ||
|
||
def register(name, frame) | ||
if frame.is_a?(DataFrame) | ||
frame = frame.lazy | ||
end | ||
_ctxt.register(name.to_s, frame._ldf) | ||
self | ||
end | ||
|
||
def register_many(frames, **named_frames) | ||
frames = (frames || {}).to_h | ||
frames = frames.merge(named_frames) | ||
frames.each do |name, frame| | ||
register(name, frame) | ||
end | ||
self | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative "test_helper" | ||
|
||
class SqlTest < Minitest::Test | ||
def test_works | ||
lf = Polars::LazyFrame.new({"a" => [1, 2, 3], "b" => ["x", nil, "z"]}) | ||
res = Polars::SQLContext.new(frame: lf).execute( | ||
"SELECT b, a*2 AS two_a FROM frame WHERE b IS NOT NULL" | ||
) | ||
expected = Polars::DataFrame.new({"b" => ["x", "z"], "two_a" => [2, 6]}) | ||
assert_frame expected, res.collect | ||
end | ||
end |