-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `explain` support
- Loading branch information
Showing
5 changed files
with
78 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module Sequel | ||
module Snowflake | ||
# sequel-snowflake version | ||
VERSION = "2.0.0" | ||
VERSION = "2.1.0" | ||
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,32 @@ | ||
module Sequel | ||
module Snowflake | ||
Sequel::Database.set_shared_adapter_scheme(:snowflake, self) | ||
|
||
module DatasetMethods | ||
# Return an array of strings specifying a query explanation for a SELECT of the | ||
# current dataset. | ||
# The options (symbolized, in lowercase) are: | ||
# JSON: JSON output is easier to store in a table and query. | ||
# TABULAR (default): tabular output is generally more human-readable than JSON output. | ||
# TEXT: formatted text output is generally more human-readable than JSON output. | ||
def explain(opts=OPTS) | ||
# Load the PrettyTable class, needed for explain output | ||
Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable) | ||
|
||
explain_with_format = if opts[:tabular] | ||
"EXPLAIN USING TABULAR" | ||
elsif opts[:json] | ||
"EXPLAIN USING JSON" | ||
elsif opts[:text] | ||
"EXPLAIN USING TEXT" | ||
else | ||
"EXPLAIN" | ||
end | ||
|
||
ds = db.send(:metadata_dataset).clone(:sql=>"#{explain_with_format} #{select_sql}") | ||
rows = ds.all | ||
Sequel::PrettyTable.string(rows, ds.columns) | ||
end | ||
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
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 |
---|---|---|
|
@@ -68,4 +68,36 @@ | |
expect(db[test_table].select(:n).all).to eq([{ n: 17 }, { n: 18 }]) | ||
end | ||
end | ||
|
||
describe '#explain' do | ||
# Create a test table with a reasonably-random suffix | ||
let!(:test_table) { "SEQUEL_SNOWFLAKE_SPECS_#{SecureRandom.hex(10)}".to_sym } | ||
let!(:db) { Sequel.connect(adapter: :snowflake, drvconnect: ENV['SNOWFLAKE_CONN_STR']) } | ||
|
||
before(:each) do | ||
db.create_table(test_table, :temp => true) do | ||
Numeric :id | ||
String :name | ||
String :email | ||
String :title | ||
end | ||
|
||
db[test_table].insert( | ||
{ id: 1, name: 'John Null', email: '[email protected]', title: 'Software Tester' } | ||
) | ||
end | ||
|
||
after(:each) do | ||
db.drop_table(test_table) | ||
end | ||
|
||
it "should have explain output" do | ||
query = db.fetch("SELECT * FROM #{test_table} WHERE ID=1;") | ||
|
||
expect(query.explain).to be_a_kind_of(String) | ||
expect(query.explain(:tabular=>true)).to be_a_kind_of(String) | ||
expect(query.explain(:json=>true)).to be_a_kind_of(String) | ||
expect(query.explain(:text=>true)).to be_a_kind_of(String) | ||
end | ||
end | ||
end |