Class: Polars::SQLContext
- Inherits:
-
Object
- Object
- Polars::SQLContext
- Defined in:
- lib/polars/sql_context.rb
Overview
Run SQL queries against DataFrame/LazyFrame data.
Instance Method Summary collapse
-
#execute(query, eager: nil) ⇒ Object
Parse the given SQL query and execute it against the registered frame data.
-
#initialize(frames = nil, eager_execution: false, **named_frames) ⇒ SQLContext
constructor
Initialize a new
SQLContext
. -
#register(name, frame) ⇒ SQLContext
Register a single frame as a table, using the given name.
-
#register_many(frames, **named_frames) ⇒ SQLContext
Register multiple eager/lazy frames as tables, using the associated names.
-
#tables ⇒ Array
Return a list of the registered table names.
-
#unregister(names) ⇒ SQLContext
Unregister one or more eager/lazy frames by name.
Constructor Details
#initialize(frames = nil, eager_execution: false, **named_frames) ⇒ SQLContext
Initialize a new SQLContext
.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/polars/sql_context.rb', line 8 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 |
Instance Method Details
#execute(query, eager: nil) ⇒ Object
Parse the given SQL query and execute it against the registered frame data.
89 90 91 92 |
# File 'lib/polars/sql_context.rb', line 89 def execute(query, eager: nil) res = Utils.wrap_ldf(_ctxt.execute(query)) eager || _eager_execution ? res.collect : res end |
#register(name, frame) ⇒ SQLContext
Register a single frame as a table, using the given name.
116 117 118 119 120 121 122 |
# File 'lib/polars/sql_context.rb', line 116 def register(name, frame) if frame.is_a?(DataFrame) frame = frame.lazy end _ctxt.register(name.to_s, frame._ldf) self end |
#register_many(frames, **named_frames) ⇒ SQLContext
Register multiple eager/lazy frames as tables, using the associated names.
132 133 134 135 136 137 138 139 |
# File 'lib/polars/sql_context.rb', line 132 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 |
#tables ⇒ Array
Return a list of the registered table names.
190 191 192 |
# File 'lib/polars/sql_context.rb', line 190 def tables _ctxt.get_tables.sort end |
#unregister(names) ⇒ SQLContext
Unregister one or more eager/lazy frames by name.
159 160 161 162 163 164 165 166 167 |
# File 'lib/polars/sql_context.rb', line 159 def unregister(names) if names.is_a?(::String) names = [names] end names.each do |nm| _ctxt.unregister(nm) end self end |