presto-client 0.6.0 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -16,3 +16,27 @@ require 'webmock/rspec'
16
16
 
17
17
  require 'presto-client'
18
18
  include Presto::Client
19
+
20
+ require 'tiny-presto'
21
+
22
+ MAX_RETRY_COUNT = 5
23
+ RETRYABLE_ERRORS = [
24
+ /No nodes available to run query/
25
+ ]
26
+
27
+ def run_with_retry(client, sql)
28
+ i = 0
29
+ while i < MAX_RETRY_COUNT
30
+ begin
31
+ columns, rows = @client.run(sql)
32
+ return columns, rows
33
+ rescue Presto::Client::PrestoQueryError => e
34
+ if RETRYABLE_ERRORS.any? { |error| e.message =~ error }
35
+ sleep(i)
36
+ i += 1
37
+ next
38
+ end
39
+ raise "Fail to run query: #{e}"
40
+ end
41
+ end
42
+ end
@@ -10,6 +10,7 @@ describe Presto::Client::StatementClient do
10
10
  time_zone: "US/Pacific",
11
11
  language: "ja_JP",
12
12
  debug: true,
13
+ follow_redirect: true
13
14
  }
14
15
  end
15
16
 
@@ -112,24 +113,53 @@ describe Presto::Client::StatementClient do
112
113
  retry_p.should be_true
113
114
  end
114
115
 
115
- it "decodes DeleteHandle" do
116
- dh = Models::DeleteHandle.decode({
117
- "handle" => {
118
- "connectorId" => "c1",
119
- "connectorHandle" => {}
120
- }
121
- })
122
- dh.handle.should be_a_kind_of Models::TableHandle
123
- dh.handle.connector_id.should == "c1"
124
- dh.handle.connector_handle.should == {}
125
- end
116
+ # presto version could be "V0_ddd" or "Vddd"
117
+ /\APresto::Client::ModelVersions::V(\w+)/ =~ Presto::Client::Models.to_s
118
+
119
+ # https://github.com/prestosql/presto/commit/80a2c5113d47e3390bf6dc041486a1c9dfc04592
120
+ # renamed DeleteHandle to DeleteTarget, then DeleteHandle exists when presto version
121
+ # is less than 313.
122
+ if $1[0, 2] == "0_" || $1.to_i < 314
123
+ it "decodes DeleteHandle" do
124
+ dh = Models::DeleteHandle.decode({
125
+ "handle" => {
126
+ "connectorId" => "c1",
127
+ "connectorHandle" => {}
128
+ }
129
+ })
130
+ dh.handle.should be_a_kind_of Models::TableHandle
131
+ dh.handle.connector_id.should == "c1"
132
+ dh.handle.connector_handle.should == {}
133
+ end
126
134
 
127
- it "validates models" do
128
- lambda do
129
- Models::DeleteHandle.decode({
130
- "handle" => "invalid"
135
+ it "validates models" do
136
+ lambda do
137
+ Models::DeleteHandle.decode({
138
+ "handle" => "invalid"
139
+ })
140
+ end.should raise_error(TypeError, /String to Hash/)
141
+ end
142
+ else
143
+ it "decodes DeleteTarget" do
144
+ dh = Models::DeleteTarget.decode({
145
+ "handle" => {
146
+ "catalogName" => "c1",
147
+ "connectorHandle" => {}
148
+ }
131
149
  })
132
- end.should raise_error(TypeError, /String to Hash/)
150
+ dh.handle.should be_a_kind_of Models::TableHandle
151
+ dh.handle.catalog_name.should == "c1"
152
+ dh.handle.connector_handle.should == {}
153
+ end
154
+
155
+ it "validates models" do
156
+ lambda do
157
+ Models::DeleteTarget.decode({
158
+ "catalogName" => "c1",
159
+ "handle" => "invalid"
160
+ })
161
+ end.should raise_error(TypeError, /String to Hash/)
162
+ end
133
163
  end
134
164
 
135
165
  it "receives headers of POST" do
@@ -148,6 +178,22 @@ describe Presto::Client::StatementClient do
148
178
  q.current_results_headers["X-Test-Header"].should == "123"
149
179
  end
150
180
 
181
+ describe "#query_id" do
182
+ it "returns query_id" do
183
+ stub_request(:post, "localhost/v1/statement").
184
+ with(body: query).to_return(body: response_json2.to_json, headers: {"X-Test-Header" => "123"})
185
+
186
+ stub_request(:get, "localhost/v1/next_uri").
187
+ to_return(body: response_json.to_json, headers: {"X-Test-Header" => "123"})
188
+
189
+ sc = StatementClient.new(faraday, query, options.merge(http_open_timeout: 1))
190
+ sc.query_id.should == "queryid"
191
+ sc.has_next?.should be_true
192
+ sc.advance.should be_true
193
+ sc.query_id.should == "queryid"
194
+ end
195
+ end
196
+
151
197
  describe '#query_info' do
152
198
  let :headers do
153
199
  {
@@ -184,6 +230,19 @@ describe Presto::Client::StatementClient do
184
230
  statement_client.query_info
185
231
  end.should raise_error(PrestoHttpError, /Presto API returned unexpected data format./)
186
232
  end
233
+
234
+ it "is redirected if server returned 301" do
235
+ stub_request(:get, "http://localhost/v1/query/#{response_json2[:id]}").
236
+ with(headers: headers).
237
+ to_return(status: 301, headers: {"Location" => "http://localhost/v1/query/redirected"})
238
+
239
+ stub_request(:get, "http://localhost/v1/query/redirected").
240
+ with(headers: headers).
241
+ to_return(body: {"queryId" => "queryid"}.to_json)
242
+
243
+ query_info = statement_client.query_info
244
+ query_info.query_id.should == "queryid"
245
+ end
187
246
  end
188
247
 
189
248
  describe "Killing a query" do
@@ -564,11 +623,13 @@ describe Presto::Client::StatementClient do
564
623
  to_return(body: early_running_response.to_json)
565
624
  client.advance
566
625
 
567
- sleep 1
568
626
  stub_request(:get, "localhost/v1/next_uri").
569
627
  with(headers: headers).
570
628
  to_return(body: done_response.to_json)
571
- client.advance
629
+ client.advance # set finished
630
+
631
+ sleep 1
632
+ client.advance # set finished
572
633
  end
573
634
 
574
635
  end
data/spec/tpch/q01.sql ADDED
@@ -0,0 +1,21 @@
1
+ SELECT
2
+ l.returnflag,
3
+ l.linestatus,
4
+ sum(l.quantity) AS sum_qty,
5
+ sum(l.extendedprice) AS sum_base_price,
6
+ sum(l.extendedprice * (1 - l.discount)) AS sum_disc_price,
7
+ sum(l.extendedprice * (1 - l.discount) * (1 + l.tax)) AS sum_charge,
8
+ avg(l.quantity) AS avg_qty,
9
+ avg(l.extendedprice) AS avg_price,
10
+ avg(l.discount) AS avg_disc,
11
+ count(*) AS count_order
12
+ FROM
13
+ "tpch"."tiny"."lineitem" AS l
14
+ WHERE
15
+ l.shipdate <= DATE '1998-12-01' - INTERVAL '90' DAY
16
+ GROUP BY
17
+ l.returnflag,
18
+ l.linestatus
19
+ ORDER BY
20
+ l.returnflag,
21
+ l.linestatus
data/spec/tpch/q02.sql ADDED
@@ -0,0 +1,43 @@
1
+ SELECT
2
+ s.acctbal,
3
+ s.name,
4
+ n.name,
5
+ p.partkey,
6
+ p.mfgr,
7
+ s.address,
8
+ s.phone,
9
+ s.comment
10
+ FROM
11
+ "tpch"."tiny"."part" p,
12
+ "tpch"."tiny"."supplier" s,
13
+ "tpch"."tiny"."partsupp" ps,
14
+ "tpch"."tiny"."nation" n,
15
+ "tpch"."tiny"."region" r
16
+ WHERE
17
+ p.partkey = ps.partkey
18
+ AND s.suppkey = ps.suppkey
19
+ AND p.size = 15
20
+ AND p.type like '%BRASS'
21
+ AND s.nationkey = n.nationkey
22
+ AND n.regionkey = r.regionkey
23
+ AND r.name = 'EUROPE'
24
+ AND ps.supplycost = (
25
+ SELECT
26
+ min(ps.supplycost)
27
+ FROM
28
+ "tpch"."tiny"."partsupp" ps,
29
+ "tpch"."tiny"."supplier" s,
30
+ "tpch"."tiny"."nation" n,
31
+ "tpch"."tiny"."region" r
32
+ WHERE
33
+ p.partkey = ps.partkey
34
+ AND s.suppkey = ps.suppkey
35
+ AND s.nationkey = n.nationkey
36
+ AND n.regionkey = r.regionkey
37
+ AND r.name = 'EUROPE'
38
+ )
39
+ ORDER BY
40
+ s.acctbal desc,
41
+ n.name,
42
+ s.name,
43
+ p.partkey
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Presto::Client::Client do
4
+ before(:all) do
5
+ @spec_path = File.dirname(__FILE__)
6
+ WebMock.disable!
7
+ @cluster = TinyPresto::Cluster.new('ghcr.io/trinodb/presto', '316')
8
+ @container = @cluster.run
9
+ @client = Presto::Client.new(server: 'localhost:8080', catalog: 'tpch', user: 'test-user', schema: 'tiny')
10
+ loop do
11
+ begin
12
+ # Make sure to all workers are available.
13
+ @client.run('select 1234')
14
+ break
15
+ rescue StandardError => exception
16
+ puts "Waiting for cluster ready... #{exception}"
17
+ sleep(5)
18
+ end
19
+ end
20
+ puts 'Cluster is ready'
21
+ end
22
+
23
+ after(:all) do
24
+ @cluster.stop
25
+ WebMock.enable!
26
+ end
27
+
28
+ it 'q01' do
29
+ q = File.read("#{@spec_path}/tpch/q01.sql")
30
+ columns, rows = run_with_retry(@client, q)
31
+ expect(columns.length).to be(10)
32
+ expect(rows.length).to be(4)
33
+ end
34
+
35
+ it 'q02' do
36
+ q = File.read("#{@spec_path}/tpch/q02.sql")
37
+ columns, rows = run_with_retry(@client, q)
38
+ expect(columns.length).to be(8)
39
+ expect(rows.length).to be(4)
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presto-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.2
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: msgpack
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -121,10 +135,13 @@ executables: []
121
135
  extensions: []
122
136
  extra_rdoc_files: []
123
137
  files:
138
+ - ".github/CODEOWNERS"
139
+ - ".github/PULL_REQUEST_TEMPLATE.md"
124
140
  - ".gitignore"
125
141
  - ".travis.yml"
126
- - ChangeLog
142
+ - ChangeLog.md
127
143
  - Gemfile
144
+ - LICENSE
128
145
  - README.md
129
146
  - Rakefile
130
147
  - lib/presto-client.rb
@@ -148,13 +165,19 @@ files:
148
165
  - modelgen/models.rb
149
166
  - modelgen/presto_models.rb
150
167
  - presto-client.gemspec
168
+ - release.rb
169
+ - spec/basic_query_spec.rb
151
170
  - spec/client_spec.rb
171
+ - spec/gzip_spec.rb
152
172
  - spec/model_spec.rb
153
173
  - spec/spec_helper.rb
154
174
  - spec/statement_client_spec.rb
175
+ - spec/tpch/q01.sql
176
+ - spec/tpch/q02.sql
177
+ - spec/tpch_query_spec.rb
155
178
  homepage: https://github.com/treasure-data/presto-client-ruby
156
179
  licenses:
157
- - Apache 2.0
180
+ - Apache-2.0
158
181
  metadata: {}
159
182
  post_install_message:
160
183
  rdoc_options: []
@@ -171,13 +194,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
194
  - !ruby/object:Gem::Version
172
195
  version: '0'
173
196
  requirements: []
174
- rubyforge_project:
175
- rubygems_version: 2.5.1
197
+ rubygems_version: 3.0.3
176
198
  signing_key:
177
199
  specification_version: 4
178
200
  summary: Presto client library
179
201
  test_files:
202
+ - spec/basic_query_spec.rb
180
203
  - spec/client_spec.rb
204
+ - spec/gzip_spec.rb
181
205
  - spec/model_spec.rb
182
206
  - spec/spec_helper.rb
183
207
  - spec/statement_client_spec.rb
208
+ - spec/tpch/q01.sql
209
+ - spec/tpch/q02.sql
210
+ - spec/tpch_query_spec.rb
data/ChangeLog DELETED
@@ -1,176 +0,0 @@
1
- 2019-07-23 version 0.6.0
2
-
3
- * Support presto 316 model class
4
-
5
-
6
- 2019-01-30 version 0.5.14
7
-
8
- * Added `Query#current_results_headers` that returns HTTP response headers
9
-
10
- 2018-12-10 version 0.5.13
11
-
12
- * Added `query_timeout` and `plan_timeout` options with default disabled
13
- * Changed timer to use CLOCK_MONOTONIC to avoid unexpected behavior when
14
- system clock is updated
15
-
16
- 2018-08-07 version 0.5.12
17
-
18
- * Upgrade to Presto 0.205 model
19
-
20
- 2018-06-27 version 0.5.11
21
-
22
- * Support multiple session properties
23
- * Check invalid JSON data response
24
-
25
- 2018-03-22 version 0.5.10
26
-
27
- * Added client_info, client_tags, and http_headers options.
28
-
29
- 2018-03-02 version 0.5.9
30
-
31
- * Added error_name field at PrestoQueryError
32
-
33
- 2017-11-13 version 0.5.8
34
-
35
- * Added `Client#kill(query_id)` method.
36
- * Added additional checking of internal exceptions so that client doesn't
37
- silently return in case when Presto query is killed and Presto returns a
38
- valid `200 OK` response with `result_uri: null`.
39
- * Fixed `undefined local variable 'body'` error that was possibly happening
40
- when Presto returned an unexpected data structure.
41
-
42
- 2017-08-28 version 0.5.7
43
- * Support a password option with HTTP basic auth
44
- * Changed retry timeout from hard coded 2h to configurable default 2min
45
- * Fix too deep nested json failure
46
-
47
- 2017-07-03 version 0.5.6:
48
- * Added missing inner class models for version 0.178
49
-
50
- 2017-06-28 version 0.5.5:
51
- * Added support for model version 0.178
52
-
53
- 2017-05-15 version 0.5.4:
54
-
55
- * Support "Content-Type: application/x-msgpack" for more efficient parsing of
56
- HTTP response body.
57
- * Added "enable_x_msgpack: true" option to send Accept header with
58
- application/x-msgpack.
59
-
60
-
61
- 2017-04-26 version 0.5.3:
62
-
63
- * Added support for model version 0.173.
64
- * Changed the default latest model version to 0.173.
65
- * Fixed compatibility with the new major version of Farady
66
- * Require Faraday 0.12 or later
67
-
68
-
69
- 2017-02-01 version 0.5.2:
70
-
71
- * Relax dependent version of Faraday to be able to use all 0.x versions.
72
- * Fix build script that was broken due to new major version of rake.
73
-
74
-
75
- 2016-11-01 version 0.5.1:
76
-
77
- * Assume ConnectorId as a primitive type to be able to decode "connectorId"
78
- fields.
79
-
80
-
81
- 2016-10-28 version 0.5.0:
82
-
83
- * Support multiple model versions
84
- * Added support for model version 0.153.
85
- * Changed the default latest model version to 0.513.
86
-
87
-
88
- 2016-08-09 version 0.4.17:
89
-
90
- * Added support for :ssl option.
91
-
92
-
93
- 2016-08-03 version 0.4.16:
94
-
95
- * Upgraded Presto model version to 0.151
96
-
97
-
98
- 2016-08-03 version 0.4.15:
99
-
100
- * decode method of model classes validate Hash type
101
-
102
-
103
- 2016-08-02 version 0.4.14:
104
-
105
- * Added support for resuming fetching query results by using new `Query.resume(next_uri, options)` method (@tetrakai++)
106
-
107
-
108
- 2016-08-02 version 0.4.13:
109
-
110
- * Added support for :http_proxy option to use a HTTP proxy server
111
- * Added support for hashed Client response using `run_with_names` (thanks to MoovWeb for allowing me to contribute)
112
- * Upgraded Presto model version to 0.134
113
-
114
- 2015-04-01 version 0.4.5:
115
-
116
- * Upgraded Presto model version to 0.99
117
-
118
-
119
- 2014-11-20 version 0.4.3:
120
-
121
- * Updated gem dependency to accept faraday ~> 0.9.x as well as ~> 0.8.8
122
-
123
-
124
- 2014-10-15 version 0.4.2:
125
-
126
- * Added support for :properties option to set session properties introduced
127
- since Presto 0.78
128
-
129
-
130
- 2014-06-12 version 0.4.1:
131
-
132
- * Added EquiJoinClause model class
133
- * Added StageId#query_id and #id methods
134
- * Added TaskId#query_id, #stage_id and #id methods
135
-
136
-
137
- 2014-06-10 version 0.4.0:
138
-
139
- * Added Query#current_results, #advance and #query_info for advanced users
140
- * Generate model classes from Presto source code to include complete classes
141
-
142
-
143
- 2014-05-06 version 0.3.3:
144
-
145
- * Added :time_zone and :language options added by Presto 0.66
146
-
147
-
148
- 2014-04-01 version 0.3.2:
149
-
150
- * Fixed a problem that client skips the last chunk if result is large
151
-
152
-
153
- 2014-01-30 version 0.3.1:
154
-
155
- * Added http_debug option
156
- * Disabled HTTP debug logging by default
157
-
158
-
159
- 2014-01-22 version 0.3.0:
160
-
161
- * Added http_timeout option
162
- * Added http_open_timeout option
163
- * Changed Query.start API to start(query, options) to http options
164
-
165
-
166
- 2014-01-22 version 0.2.0:
167
-
168
- * Added Query#cancel
169
- * Added Query#close
170
- * Added Client#run
171
- * Changed required_ruby_version from 1.9.3 to 1.9.1
172
-
173
-
174
- 2014-01-07 version 0.1.0:
175
-
176
- * First release