tiny-presto 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c0da1870722311b6290c6eab394e3afbb5783a9010b4a7c746dec95e72bedb0
4
- data.tar.gz: caa42bf7f62ae2d0ebf26cd7d9e13f9d3aea6919dbe1c129985a0997b1e4e95f
3
+ metadata.gz: 22d98c71de7a327595dd05b6a59a8ed1ff747208995eb9ab5c02bc4ef954dade
4
+ data.tar.gz: 86b59421bc8379e130956108781965da6d51bad55bb274b907ba53ae688c0d4a
5
5
  SHA512:
6
- metadata.gz: 368cdc53a9e308c9c4f89db14c09fc10c228cd9bdf825620c0f8469b8df82c73d11d9b7990609e76a436ac3fda84f2fb85ce33b0f520cc6e4ea6ce1fcb2d1fc2
7
- data.tar.gz: 0f0e25544d7f6d8c9caede435fdc4b860a5e18f55dbd70cc4c25b3e5c42d43663a95476feb85c9778b9c9a0a2c3122b8b302f7f50bee7011f924c9d11dea36e5
6
+ metadata.gz: 47f6d98e15f9071f31c424fcb63f8521f7b99a09c796bbac4df5447da5ba494101460f56d6a242b213737b9ba57af3fdc2bc2498cb390a961bdc5a2366fe467d
7
+ data.tar.gz: a1801999ed1897d976e70d90b8728f92b428a91b08a8fd29a703d852728b2f1e4cc895b0ecaeac7dd09f650bc6ebb24f505f3de362e6f942fa48f10e96411920
data/.rubocop.yml CHANGED
@@ -1 +1,4 @@
1
1
  inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-01-14 16:36:39 +0900 using RuboCop version 0.52.1.
3
+ # on 2020-02-12 15:52:06 +0900 using RuboCop version 0.52.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -18,15 +18,19 @@ Lint/UselessAssignment:
18
18
  Exclude:
19
19
  - 'spec/cluster_spec.rb'
20
20
 
21
+ # Offense count: 1
22
+ Metrics/AbcSize:
23
+ Max: 17
24
+
21
25
  # Offense count: 2
22
26
  # Configuration parameters: CountComments, ExcludedMethods.
23
27
  Metrics/BlockLength:
24
- Max: 32
28
+ Max: 30
25
29
 
26
- # Offense count: 1
30
+ # Offense count: 3
27
31
  # Configuration parameters: CountComments.
28
32
  Metrics/MethodLength:
29
- Max: 15
33
+ Max: 19
30
34
 
31
35
  # Offense count: 1
32
36
  # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
@@ -35,15 +39,21 @@ Naming/FileName:
35
39
  Exclude:
36
40
  - 'lib/tiny-presto.rb'
37
41
 
38
- # Offense count: 2
42
+ # Offense count: 1
43
+ # Cop supports --auto-correct.
44
+ # Configuration parameters: AutoCorrect.
45
+ Performance/HashEachMethods:
46
+ Exclude:
47
+ - 'lib/tiny-presto.rb'
48
+
49
+ # Offense count: 1
39
50
  Style/Documentation:
40
51
  Exclude:
41
52
  - 'spec/**/*'
42
53
  - 'test/**/*'
43
54
  - 'lib/tiny-presto.rb'
44
- - 'lib/tiny-presto/cluster.rb'
45
55
 
46
- # Offense count: 2
56
+ # Offense count: 5
47
57
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
48
58
  # URISchemes: http, https
49
59
  Metrics/LineLength:
data/Gemfile CHANGED
@@ -7,8 +7,8 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
7
7
  gem 'docker-api'
8
8
  gem 'presto-client'
9
9
  gem 'rake'
10
- gem 'rubocop'
11
10
  gem 'rdoc'
11
+ gem 'rubocop'
12
12
 
13
13
  group :development, :test do
14
14
  gem 'rspec'
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
2
4
  require 'bundler/gem_tasks'
3
5
 
4
6
  require 'rake/testtask'
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'docker-api'
2
4
 
3
5
  module TinyPresto
4
6
  # Represents a Presto cluster
5
7
  #
6
8
  class Cluster
7
- def initialize(url, tag = 'latest')
8
- @url = url
9
+ def initialize(tag = 'latest')
9
10
  @tag = tag
10
11
  @image_name = "prestosql/presto:#{@tag}"
11
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TinyPresto
2
- VERSION = '0.0.3'.freeze
4
+ VERSION = '0.0.4'
3
5
  end
data/lib/tiny-presto.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'tiny-presto/version'
2
4
  require 'tiny-presto/cluster'
3
5
 
@@ -12,18 +14,15 @@ module TinyPresto
12
14
  attr_reader :cluster, :client
13
15
 
14
16
  def initialize
15
- url = 'localhost'
16
- @cluster = Cluster.new(url)
17
+ @cluster = Cluster.new
17
18
  @cluster.run
18
- @client = Presto::Client.new(server: "#{url}:8080", catalog: 'memory', user: 'tiny-user', schema: 'default')
19
+ @client = Presto::Client.new(server: 'localhost:8080', catalog: 'memory', user: 'tiny-user', schema: 'default')
19
20
  loop do
20
- begin
21
- @client.run('show schemas')
22
- break
23
- rescue StandardError => _
24
- # Waiting for the cluster is launched
25
- sleep(1)
26
- end
21
+ @client.run('show schemas')
22
+ break
23
+ rescue StandardError => _
24
+ # Waiting for the cluster is launched
25
+ sleep(1)
27
26
  end
28
27
  end
29
28
 
@@ -32,6 +31,44 @@ module TinyPresto
32
31
  end
33
32
  end
34
33
 
34
+ def self.print_record(record)
35
+ ret = '('
36
+ record.each_with_index do |v, idx|
37
+ ret << ',' if idx != 0
38
+ ret << if v.is_a? Numeric
39
+ v.to_s
40
+ else
41
+ # Non numeric value is interpreted as string
42
+ "'#{v}'"
43
+ end
44
+ end
45
+ ret << ')'
46
+ ret
47
+ end
48
+
49
+ def self.prepare(table_name, table_data)
50
+ # {'c1': [1, 2, 3], 'c2': ['a', 'b', 'c']}
51
+ columns = table_data.keys
52
+ records = []
53
+ table_data.each do |_, rows|
54
+ rows.each_with_index do |r, idx|
55
+ if records[idx].nil?
56
+ records << [r]
57
+ else
58
+ records[idx] << r
59
+ end
60
+ end
61
+ end
62
+ values_clause = '(values '
63
+ records.each_with_index do |record, idx|
64
+ values_clause << ',' if idx != 0
65
+ values_clause << print_record(record)
66
+ end
67
+ values_clause << ')'
68
+ query = "CREATE TABLE #{table_name} AS SELECT * FROM #{values_clause} t(#{columns.join(',')})"
69
+ run_with_retry(query)
70
+ end
71
+
35
72
  # Run the given SQL.
36
73
  #
37
74
  # TinyPresto.run("show schemas")
@@ -42,13 +79,27 @@ module TinyPresto
42
79
  rows
43
80
  end
44
81
 
82
+ # Run the given query with retrying in case of undeterministic error.
83
+ #
84
+ # TinyPresto.run_with_retry("show schemas")
85
+ #
86
+ def self.run_with_retry(sql, max_retry = 3)
87
+ max_retry.times do
88
+ return run(sql)
89
+ rescue Presto::Client::PrestoQueryError => e
90
+ # Cluster may be in the initialization phase.
91
+ raise unless e.message.match?(/^No nodes available to run query/)
92
+
93
+ next
94
+ end
95
+ end
96
+
45
97
  # Run the given SQL and verify the result.
46
98
  #
47
99
  # TinyPresto.verify("show schemas", [["default"], ["information_schema"]])
48
100
  # # => return true
49
101
  def self.verify(sql, expected_result)
50
- presto = TinyPresto.instance
51
- _, rows = presto.client.run(sql)
102
+ rows = run_with_retry(sql)
52
103
  rows == expected_result
53
104
  end
54
105
 
data/spec/cluster_spec.rb CHANGED
@@ -1,19 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe TinyPresto::Cluster do
4
6
  describe 'cluster' do
5
7
  before(:all) do
6
- @cluster = TinyPresto::Cluster.new('localhost')
8
+ @cluster = TinyPresto::Cluster.new
7
9
  @container = @cluster.run
8
10
  @client = Presto::Client.new(server: 'localhost:8080', catalog: 'memory', user: 'tiny-user', schema: 'default')
9
11
  loop do
10
- begin
11
- @client.run('show schemas')
12
- break
13
- rescue StandardError => exception
14
- puts 'Waiting for cluster ready...'
15
- sleep(3)
16
- end
12
+ @client.run('show schemas')
13
+ break
14
+ rescue StandardError => exception
15
+ puts 'Waiting for cluster ready...'
16
+ sleep(3)
17
17
  end
18
18
  puts 'Cluster is ready'
19
19
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler'
2
4
 
3
5
  begin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe TinyPresto do
@@ -19,4 +21,14 @@ RSpec.describe TinyPresto do
19
21
  expect(result).to eq(true)
20
22
  end
21
23
  end
24
+
25
+ describe 'prepare' do
26
+ it 'dataset correctly' do
27
+ data = { 'c1': [1, 2, 3], 'c2': %w[a b c] }
28
+ TinyPresto.prepare('new_table', data)
29
+ expected = [[1, 'a'], [2, 'b'], [3, 'c']]
30
+ rows = TinyPresto.run_with_retry('select * from new_table')
31
+ expect(rows).to eq(expected)
32
+ end
33
+ end
22
34
  end
data/spec/version_spec.rb CHANGED
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe TinyPresto do
4
6
  describe '#version' do
5
7
  it 'correct' do
6
- expect(TinyPresto::VERSION).to eq('0.0.1')
8
+ expect(TinyPresto::VERSION).to eq('0.0.3')
7
9
  end
8
10
  end
9
11
  end
data/tiny-presto.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path 'lib/tiny-presto/version', File.dirname(__FILE__)
2
4
 
3
5
  Gem::Specification.new do |gem|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny-presto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Sasaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-28 00:00:00.000000000 Z
11
+ date: 2020-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api
@@ -110,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.7.6
113
+ rubygems_version: 3.0.3
115
114
  signing_key:
116
115
  specification_version: 4
117
116
  summary: For Presto functionality testing