Skip to content

Commit

Permalink
V1.1.0 (#35)
Browse files Browse the repository at this point in the history
Pull in contributions into one PR so CI can run against the Rinsed
snowflake instance - thanks everyone!

---------

Co-authored-by: Deividas J <lizdeika@gmail.com>
Co-authored-by: Max Forssén <max.forssen@mentimeter.com>
Co-authored-by: Fabio Papa <me@fabiopapa.dev>
  • Loading branch information
4 people authored Jun 5, 2024
1 parent 6bf30cb commit 8af8864
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
48 changes: 26 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rb_snowflake_client (1.0.5)
rb_snowflake_client (1.1.0)
concurrent-ruby (>= 1.2)
connection_pool (>= 2.4)
dotenv (>= 2.8)
Expand All @@ -12,37 +12,41 @@ PATH
GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
bigdecimal (3.1.8)
coderay (1.1.3)
concurrent-ruby (1.2.2)
concurrent-ruby (1.3.1)
connection_pool (2.4.1)
diff-lcs (1.5.0)
dotenv (2.8.1)
jwt (2.7.1)
method_source (1.0.0)
oj (3.16.1)
parallel (1.23.0)
diff-lcs (1.5.1)
dotenv (3.1.2)
jwt (2.8.1)
base64
method_source (1.1.0)
oj (3.16.3)
bigdecimal (>= 3.0)
parallel (1.24.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.1.0)
rake (13.2.1)
retryable (3.0.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)

PLATFORMS
arm64-darwin-21
arm64-darwin-22
ruby

DEPENDENCIES
bundler
Expand All @@ -59,4 +63,4 @@ DEPENDENCIES
rspec

BUNDLED WITH
2.4.19
2.5.4
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ Clients are not warehouse specific, you can override the default warehouse per q
client.query("SELECT * FROM BIGTABLE", warehouse: "FAST_WH")
```

## Binding parameters

Say we have `BIGTABLE` with a `data` column of a type `VARIANT`.

```ruby
json_string = '{"valid": "json"}'
query = "insert into BIGTABLE(data) select parse_json(?)"
bindings = {
"1": {
"type": "TEXT",
"value": json_string
}
}
client.query(query, bindings: bindings)
```

For additional information about binding parameters refer to snowflake documentation: https://docs.snowflake.com/en/developer-guide/sql-api/submitting-requests#using-bind-variables-in-a-statement

# Configuration Options

The client supports the following configuration options, each with their own getter/setter except connection pool options which must be set at construction. Additionally, all except logger can be configured with environment variables (see above, but the pattern is like: "SNOWFLAKE_HTTP_RETRIES". Configuration options can only be set on initialization through `new` or `from_env`.
Expand Down
22 changes: 11 additions & 11 deletions lib/ruby_snowflake/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ def initialize(
@_enable_polling_queries = false
end

def query(query, warehouse: nil, streaming: false, database: nil, schema: nil)
def query(query, warehouse: nil, streaming: false, database: nil, schema: nil, bindings: nil)
warehouse ||= @default_warehouse
database ||= @default_database

query_start_time = Time.now.to_i
response = nil
connection_pool.with do |connection|
request_body = {
"database" => database&.upcase,
"warehouse" => warehouse&.upcase,
"schema" => schema&.upcase,
"database" => database&.upcase,
"statement" => query,
"timeout" => @query_timeout,
"warehouse" => warehouse&.upcase,
"bindings" => bindings
}

response = request_with_auth_and_headers(
Expand All @@ -150,7 +150,7 @@ def query(query, warehouse: nil, streaming: false, database: nil, schema: nil)
Oj.dump(request_body)
)
end
retreive_result_set(query_start_time, query, response, streaming)
retrieve_result_set(query_start_time, query, response, streaming)
end

alias fetch query
Expand Down Expand Up @@ -267,7 +267,7 @@ def attempt_to_cancel_and_silence_errors(connection, statement_handle)
false
end

def retreive_result_set(query_start_time, query, response, streaming)
def retrieve_result_set(query_start_time, query, response, streaming)
json_body = Oj.load(response.body, OJ_OPTIONS)
statement_handle = json_body["statementHandle"]

Expand All @@ -277,18 +277,18 @@ def retreive_result_set(query_start_time, query, response, streaming)
end

num_threads = number_of_threads_to_use(json_body["resultSetMetaData"]["partitionInfo"].size)
retreive_proc = ->(index) { retreive_partition_data(statement_handle, index) }
retrieve_proc = ->(index) { retrieve_partition_data(statement_handle, index) }

if streaming
StreamingResultStrategy.result(json_body, retreive_proc)
StreamingResultStrategy.result(json_body, retrieve_proc)
elsif num_threads == 1
SingleThreadInMemoryStrategy.result(json_body, retreive_proc)
SingleThreadInMemoryStrategy.result(json_body, retrieve_proc)
else
ThreadedInMemoryStrategy.result(json_body, retreive_proc, num_threads)
ThreadedInMemoryStrategy.result(json_body, retrieve_proc, num_threads)
end
end

def retreive_partition_data(statement_handle, partition_index)
def retrieve_partition_data(statement_handle, partition_index)
partition_response = nil
connection_pool.with do |connection|
partition_response = request_with_auth_and_headers(
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_snowflake/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RubySnowflake
VERSION = "1.0.6"
VERSION = "1.1.0"
end

0 comments on commit 8af8864

Please sign in to comment.