hipster_sql_to_hbase 0.2.0 → 0.2.2

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
  SHA1:
3
- metadata.gz: 1e9b6ec47f4bf7ea5891b324f89503e45cf8c3eb
4
- data.tar.gz: c5e995d9ff7c8cf4518a9721c799316ce991bcad
3
+ metadata.gz: c8f7de19de530e43d65fbb34022c20b589158c99
4
+ data.tar.gz: 2f618ebe82db8ccc67f78a35448c981a50f1b97b
5
5
  SHA512:
6
- metadata.gz: 69e228786d1539ea179445b1b73160d109fe111348748cbd761790a3ec38f7a4199351023895aeb2322dd96ad5899dc4565167524de8464fc624ce9d0898742d
7
- data.tar.gz: 01d0174443732c50f334f6d145f80a2d1d5e0b1f5eb3915311d6afeb39a1c05201da54a0591ef884d1bf6f547c387b4223cb5f645feeeb315f7a650a799bf16d
6
+ metadata.gz: af120d262c539e082624bf1af5addbdbc8e29dbfd01162530585ba30ffc88ee54e2b1d643d17b2c907b6e932ade7587e4b539adaf73e5c9cb29a4db09eec8acc
7
+ data.tar.gz: 41223aa5cd25db917af38b0cc4766a4716a6026a21f44e9449ec1b959820fddc1243ec395992fd69bc9646047bb4c37c1439e3846c9254bad6addbbda2b7103d
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.2
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "hipster_sql_to_hbase"
5
- s.version = "0.2.0"
5
+ s.version = "0.2.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jean Lescure"]
@@ -143,7 +143,13 @@ module HipsterSqlToHbase
143
143
  #
144
144
  # === Note: the main difference between **parse_tree** and **parse_hash** is that a **ResultTree** can be further converted to Thrift calls while a **Hash** cannot.
145
145
  def parse_tree(string)
146
- HipsterSqlToHbase::ResultTree.new(parse_hash(string))
146
+ parsed_hash = parse_hash(string)
147
+
148
+ if parsed_hash.nil?
149
+ nil
150
+ else
151
+ HipsterSqlToHbase::ResultTree.new(parsed_hash)
152
+ end
147
153
  end
148
154
 
149
155
  # Generate a <b>HipsterSqlToHbase</b>::<b>ThriftCallGroup</b> from a valid, SQL string.
@@ -56,6 +56,7 @@ grammar SQLCreateTable
56
56
  rule optional_column_feature
57
57
  optional_primary_key /
58
58
  optional_auto_increment /
59
+ optional_default /
59
60
  optional_not_null
60
61
  end
61
62
 
@@ -65,6 +66,12 @@ grammar SQLCreateTable
65
66
  SPACES "primary" SPACES "key"
66
67
  end
67
68
 
69
+ rule optional_default
70
+ SPACES "DEFAULT" SPACES (quoted_string / NULL_KEYWORD / MINUS_OR_DIGIT+)
71
+ /
72
+ SPACES "default" SPACES (quoted_string / NULL_KEYWORD / MINUS_OR_DIGIT+)
73
+ end
74
+
68
75
  rule optional_auto_increment
69
76
  SPACES "AUTO_INCREMENT"
70
77
  /
@@ -1,5 +1,4 @@
1
1
  grammar SQLFromClause
2
- include SQLPrimitives
3
2
  include SQLRowSupport
4
3
 
5
4
  rule from
@@ -9,4 +8,10 @@ grammar SQLFromClause
9
8
  end
10
9
  }
11
10
  end
11
+
12
+ rule from_or_empty
13
+ from
14
+ /
15
+ EMPTY_STRING
16
+ end
12
17
  end
@@ -43,6 +43,22 @@ grammar SQLPrimitives
43
43
  }
44
44
  end
45
45
 
46
+ rule string
47
+ STRING_CHARS+ {
48
+ def eval
49
+ text_value
50
+ end
51
+ }
52
+ end
53
+
54
+ rule column_string
55
+ STRING_CHARS_W_NON_XCLUSIVE_NUMS+ {
56
+ def eval
57
+ text_value.downcase
58
+ end
59
+ }
60
+ end
61
+
46
62
  rule backtick_string
47
63
  BACKTICK string BACKTICK {
48
64
  def eval
@@ -51,10 +67,10 @@ grammar SQLPrimitives
51
67
  }
52
68
  end
53
69
 
54
- rule string
55
- STRING_CHARS+ {
70
+ rule backtick_column_string
71
+ BACKTICK column_string BACKTICK {
56
72
  def eval
57
- text_value
73
+ column_string.eval
58
74
  end
59
75
  }
60
76
  end
@@ -64,7 +80,15 @@ grammar SQLPrimitives
64
80
  end
65
81
 
66
82
  rule integer
67
- MINUS_OR_DIGIT DIGIT* {
83
+ DASH? DIGIT* {
84
+ def eval
85
+ text_value.to_i
86
+ end
87
+ }
88
+ end
89
+
90
+ rule positive_integer
91
+ DIGIT* {
68
92
  def eval
69
93
  text_value.to_i
70
94
  end
@@ -72,7 +96,7 @@ grammar SQLPrimitives
72
96
  end
73
97
 
74
98
  rule float
75
- integer "." integer {
99
+ integer "." positive_integer {
76
100
  def eval
77
101
  text_value.to_f
78
102
  end
@@ -2,39 +2,76 @@ grammar SQLRowSupport
2
2
  include SQLPrimitives
3
3
 
4
4
  rule one_or_more_table_names
5
- table_name "," SPACE one_or_more_table_names {
5
+ table_name COMMA SPACES one_or_more_table_names {
6
6
  def eval
7
7
  [table_name.eval, one_or_more_table_names.eval]
8
8
  end
9
9
  }
10
10
  /
11
- table_name
11
+ EMPTY_STRING table_name {
12
+ def eval
13
+ [table_name.eval]
14
+ end
15
+ }
12
16
  end
13
-
14
- rule one_or_more_column_names_with_sort
15
- one_column_name_with_sort "," SPACE one_or_more_column_names {
17
+
18
+ rule one_or_more_numbers
19
+ number optional_spaces COMMA optional_spaces one_or_more_numbers {
16
20
  def eval
17
- result1, result2 = one_column_name.eval, one_or_more_column_names.eval
21
+ result1, result2 = number.eval, one_or_more_numbers.eval
18
22
  [result1, result2].flatten
19
23
  end
20
24
  }
21
- /
22
- one_column_name_with_sort
25
+ /
26
+ EMPTY_STRING number {
27
+ def eval
28
+ [number.eval]
29
+ end
30
+ }
23
31
  end
24
-
25
- rule one_or_more_column_names
26
- one_column_name SPACE? "," SPACE? one_or_more_column_names {
32
+
33
+ rule one_number_with_alias
34
+ number SPACES "AS" SPACES alias_column_name {
27
35
  def eval
28
- result1, result2 = one_column_name.eval, one_or_more_column_names.eval
36
+ {
37
+ :number => number.eval,
38
+ :alias_hash => {
39
+ :number => number.eval,
40
+ :alias => alias_column_name.eval
41
+ }
42
+ }
43
+ end
44
+ }
45
+ end
46
+
47
+ rule one_or_more_number_with_alias
48
+ one_number_with_alias optional_spaces COMMA optional_spaces one_or_more_number_with_alias {
49
+ def eval
50
+ result1, result2 = one_number_with_alias.eval, one_or_more_number_with_alias.eval
29
51
  [result1, result2].flatten
30
52
  end
31
53
  }
32
54
  /
33
- one_column_name
55
+ EMPTY_STRING one_number_with_alias {
56
+ def eval
57
+ [one_number_with_alias.eval]
58
+ end
59
+ }
34
60
  end
35
61
 
62
+ #rule one_or_more_column_names_with_sort
63
+ # one_column_name_with_sort COMMA SPACES one_or_more_column_names {
64
+ # def eval
65
+ # result1, result2 = one_column_name.eval, one_or_more_column_names.eval
66
+ # [result1, result2].flatten
67
+ # end
68
+ # }
69
+ # /
70
+ # one_column_name_with_sort
71
+ #end
72
+
36
73
  rule one_column_name
37
- table_name dot column_name {
74
+ table_name DOT column_name {
38
75
  def eval
39
76
  column_name.eval
40
77
  end
@@ -43,8 +80,68 @@ grammar SQLRowSupport
43
80
  column_name
44
81
  end
45
82
 
83
+ rule one_or_more_column_names
84
+ one_column_name optional_spaces COMMA optional_spaces one_or_more_column_names {
85
+ def eval
86
+ result1, result2 = one_column_name.eval, one_or_more_column_names.eval
87
+ [result1, result2].flatten
88
+ end
89
+ }
90
+ /
91
+ EMPTY_STRING one_column_name {
92
+ def eval
93
+ [one_column_name.eval]
94
+ end
95
+ }
96
+ end
97
+
98
+ rule one_column_name_with_alias
99
+ table_name DOT column_name SPACES "AS" SPACES alias_column_name {
100
+ def eval
101
+ {
102
+ :column => column_name.eval,
103
+ :alias_hash => {
104
+ :column => column_name.eval,
105
+ :alias => alias_column_name.eval
106
+ }
107
+ }
108
+ end
109
+ }
110
+ /
111
+ column_name SPACES "AS" SPACES alias_column_name {
112
+ def eval
113
+ {
114
+ :column => column_name.eval,
115
+ :alias_hash => {
116
+ :column => column_name.eval,
117
+ :alias => alias_column_name.eval
118
+ }
119
+ }
120
+ end
121
+ }
122
+ end
123
+
124
+ rule one_or_more_column_name_with_alias
125
+ one_column_name_with_alias optional_spaces COMMA optional_spaces one_or_more_column_name_with_alias {
126
+ def eval
127
+ result1, result2 = one_column_name_with_alias.eval, one_or_more_column_name_with_alias.eval
128
+ [result1, result2].flatten
129
+ end
130
+ }
131
+ /
132
+ EMPTY_STRING one_column_name_with_alias {
133
+ def eval
134
+ [one_column_name_with_alias.eval]
135
+ end
136
+ }
137
+ end
138
+
46
139
  rule column_name
47
- backtick_string / column_string / all_columns
140
+ backtick_column_string / column_string / all_columns
141
+ end
142
+
143
+ rule alias_column_name
144
+ column_name
48
145
  end
49
146
 
50
147
  rule index_name
@@ -58,18 +155,6 @@ grammar SQLRowSupport
58
155
  string
59
156
  end
60
157
 
61
- rule column_string
62
- string {
63
- def eval
64
- text_value.downcase
65
- end
66
- }
67
- end
68
-
69
- rule dot
70
- "." { def eval; "."; end }
71
- end
72
-
73
158
  rule all_columns
74
159
  "*" { def eval; "*"; end }
75
160
  end
@@ -10,35 +10,29 @@ grammar SQLSelect
10
10
 
11
11
  rule select_expression
12
12
  select
13
- from
13
+ from_or_empty
14
14
  where_tree
15
15
  limit_condition_or_empty
16
16
  order_by_condition_or_empty
17
- {
18
- def eval
19
- SelectExpression.new({
20
- :string => self.text_value,
21
-
22
- :select => select.eval,
23
- :from => from.eval,
24
- :where => where_tree.where_condition_or_empty.eval,
25
- :limit => limit_condition_or_empty.eval,
26
- :order_by => order_by_condition_or_empty.eval
27
- })
28
- end
29
-
17
+ {
30
18
  def query_type
31
19
  :select
32
20
  end
33
21
 
34
22
  def tree
35
- {
36
- :select => select.columns,
37
- :from => from.tables,
38
- :where => where_tree.eval,
39
- :limit => limit_condition_or_empty.eval,
40
- :order_by => order_by_condition_or_empty.eval
41
- }
23
+ result = {}
24
+
25
+ selectables = select.eval
26
+
27
+ result[:limit] = limit_condition_or_empty.eval unless limit_condition_or_empty.eval.nil?
28
+ result[:order_by] = order_by_condition_or_empty.eval unless order_by_condition_or_empty.eval.nil?
29
+ result[:from] = from_or_empty.tables if defined? from_or_empty.tables
30
+ result[:where] = where_tree.eval unless where_tree.eval.nil?
31
+ result[:columns] = selectables[:columns] unless selectables[:columns].nil?
32
+ result[:numbers] = selectables[:numbers] unless selectables[:numbers].nil?
33
+ result[:aliases] = selectables[:aliases] unless selectables[:aliases].nil?
34
+
35
+ result
42
36
  end
43
37
  }
44
38
  end
@@ -1,17 +1,112 @@
1
1
  grammar SQLSelectClause
2
- include SQLPrimitives
3
2
  include SQLRowSupport
4
3
 
5
4
  rule select
6
- "SELECT" SPACES one_or_more_column_names optional_spaces {
7
- def columns
8
- result = one_or_more_column_names.eval
9
- if result.kind_of?(Array)
10
- result
11
- else
12
- [result]
13
- end
14
- end
5
+ "SELECT" SPACES selectable_list optional_spaces {
6
+ def eval
7
+ result = selectable_list.eval
8
+ result
9
+ end
10
+ }
11
+ end
12
+
13
+ rule selectable_list
14
+ selectables optional_spaces COMMA selectable_list {
15
+ def eval
16
+ result = {}
17
+ one_result = selectables.eval
18
+ list_result = selectable_list.eval
19
+
20
+ result[:columns] = []
21
+ result[:columns] += one_result[:columns] unless one_result[:columns].nil?
22
+ result[:columns] += list_result[:columns] unless list_result[:columns].nil?
23
+ result.delete(:columns) if result[:columns].empty?
24
+
25
+ result[:numbers] = []
26
+ result[:numbers] += one_result[:numbers] unless one_result[:numbers].nil?
27
+ result[:numbers] += list_result[:numbers] unless list_result[:numbers].nil?
28
+ result.delete(:numbers) if result[:numbers].empty?
29
+
30
+ result[:aliases] = []
31
+ result[:aliases] += one_result[:aliases] unless one_result[:aliases].nil?
32
+ result[:aliases] += list_result[:aliases] unless list_result[:aliases].nil?
33
+ result.delete(:aliases) if result[:aliases].empty?
34
+
35
+ result
36
+ end
37
+ }
38
+ /
39
+ selectables
40
+ end
41
+
42
+ rule selectables
43
+ selectable_columns_with_alias
44
+ /
45
+ selectable_columns
46
+ /
47
+ selectable_numbers_with_alias
48
+ /
49
+ selectable_numbers
50
+ end
51
+
52
+ rule selectable_columns_with_alias
53
+ optional_spaces one_or_more_column_name_with_alias {
54
+ def eval
55
+ result = {
56
+ :columns => [],
57
+ :aliases => []
58
+ }
59
+ num_alias_pairs = one_or_more_column_name_with_alias.eval
60
+
61
+ num_alias_pairs.each do |num_alias_pair|
62
+ result[:columns] << num_alias_pair[:column]
63
+ result[:aliases] << num_alias_pair[:alias_hash]
64
+ end
65
+
66
+ result
67
+ end
68
+ }
69
+ end
70
+
71
+ rule selectable_columns
72
+ optional_spaces one_or_more_column_names {
73
+ def eval
74
+ {
75
+ :columns => one_or_more_column_names.eval
76
+ }
77
+ end
78
+ }
79
+ end
80
+
81
+ rule selectable_numbers_with_alias
82
+ optional_spaces one_or_more_number_with_alias {
83
+ def eval
84
+ result = {
85
+ :numbers => [],
86
+ :aliases => []
87
+ }
88
+ num_alias_pairs = one_or_more_number_with_alias.eval
89
+
90
+ num_alias_pairs.each do |num_alias_pair|
91
+ result[:numbers] << num_alias_pair[:number]
92
+ result[:aliases] << num_alias_pair[:alias_hash]
93
+ end
94
+
95
+ result
96
+ end
97
+ }
98
+ end
99
+
100
+ rule selectable_numbers
101
+ optional_spaces one_or_more_numbers {
102
+ def eval
103
+ result_nums = one_or_more_numbers.eval
104
+ result_nums = [result_nums] unless result_nums.is_a? Array
105
+
106
+ {
107
+ :numbers => result_nums
108
+ }
109
+ end
15
110
  }
16
111
  end
17
112
  end
@@ -44,17 +44,25 @@ grammar SQLTokens
44
44
  end
45
45
 
46
46
  rule BACKTICK
47
- "`"
47
+ "`" { def eval; nil; end }
48
48
  end
49
49
 
50
50
  rule COMMA
51
51
  ","
52
52
  end
53
53
 
54
+ rule DOT
55
+ "." { def eval; "."; end }
56
+ end
57
+
54
58
  rule STRING_CHARS
55
59
  [0-9a-zA-Z\$_:]
56
60
  end
57
61
 
62
+ rule VALID_NON_NUM_CHARS
63
+ [a-zA-Z\$_:]
64
+ end
65
+
58
66
  rule ALPHA_CHARS
59
67
  [a-zA-Z]
60
68
  end
@@ -63,12 +71,12 @@ grammar SQLTokens
63
71
  [0-9]
64
72
  end
65
73
 
66
- rule DASH
67
- "-" { def eval; nil; end }
74
+ rule STRING_CHARS_W_NON_XCLUSIVE_NUMS
75
+ DIGIT* VALID_NON_NUM_CHARS+ DIGIT*
68
76
  end
69
77
 
70
- rule MINUS_OR_DIGIT
71
- DASH / DIGIT
78
+ rule DASH
79
+ "-" { def eval; nil; end }
72
80
  end
73
81
 
74
82
  rule REAL_KEYWORD
@@ -1,4 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'sql_sentence_types'))
3
+
4
+ require 'spec_helper'
2
5
 
3
6
 
4
7
 
@@ -63,11 +66,11 @@ describe "HipsterSqlToHbase.parse_tree method" do
63
66
  expect(response.class).to eq(HipsterSqlToHbase::ResultTree)
64
67
  expect(response[:query_type]).to eq(:select)
65
68
  expect(response[:query_hash].class).to eq(Hash)
66
- expect(response[:query_hash][:select].class).to eq(Array)
67
- expect(response[:query_hash][:select].length).to eq(1)
68
- expect(response[:query_hash][:select][0]).to eq('user')
69
- expect(response[:query_hash][:from].class).to eq(String)
70
- expect(response[:query_hash][:from]).to eq('users')
69
+ expect(response[:query_hash][:columns].class).to eq(Array)
70
+ expect(response[:query_hash][:columns].length).to eq(1)
71
+ expect(response[:query_hash][:columns][0]).to eq('user')
72
+ expect(response[:query_hash][:from].class).to eq(Array)
73
+ expect(response[:query_hash][:from][0]).to eq('users')
71
74
  response[:query_hash][:where].should be_nil
72
75
  response[:query_hash][:limit].should be_nil
73
76
  response[:query_hash][:order].should be_nil
@@ -80,11 +83,11 @@ describe "HipsterSqlToHbase.parse_tree method" do
80
83
  expect(response.class).to eq(HipsterSqlToHbase::ResultTree)
81
84
  expect(response[:query_type]).to eq(:select)
82
85
  expect(response[:query_hash].class).to eq(Hash)
83
- expect(response[:query_hash][:select].class).to eq(Array)
84
- expect(response[:query_hash][:select].length).to eq(1)
85
- expect(response[:query_hash][:select][0]).to eq('user')
86
- expect(response[:query_hash][:from].class).to eq(String)
87
- expect(response[:query_hash][:from]).to eq('users')
86
+ expect(response[:query_hash][:columns].class).to eq(Array)
87
+ expect(response[:query_hash][:columns].length).to eq(1)
88
+ expect(response[:query_hash][:columns][0]).to eq('user')
89
+ expect(response[:query_hash][:from].class).to eq(Array)
90
+ expect(response[:query_hash][:from][0]).to eq('users')
88
91
  response[:query_hash][:where].should be_nil
89
92
  response[:query_hash][:limit].should be_nil
90
93
  response[:query_hash][:order].should be_nil
@@ -97,11 +100,11 @@ describe "HipsterSqlToHbase.parse_tree method" do
97
100
  expect(response.class).to eq(HipsterSqlToHbase::ResultTree)
98
101
  expect(response[:query_type]).to eq(:select)
99
102
  expect(response[:query_hash].class).to eq(Hash)
100
- expect(response[:query_hash][:select].class).to eq(Array)
101
- expect(response[:query_hash][:select].length).to eq(1)
102
- expect(response[:query_hash][:select][0]).to eq('user')
103
- expect(response[:query_hash][:from].class).to eq(String)
104
- expect(response[:query_hash][:from]).to eq('users')
103
+ expect(response[:query_hash][:columns].class).to eq(Array)
104
+ expect(response[:query_hash][:columns].length).to eq(1)
105
+ expect(response[:query_hash][:columns][0]).to eq('user')
106
+ expect(response[:query_hash][:from].class).to eq(Array)
107
+ expect(response[:query_hash][:from][0]).to eq('users')
105
108
  response[:query_hash][:where].should be_nil
106
109
  response[:query_hash][:limit].should be_nil
107
110
  response[:query_hash][:order].should be_nil
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,2 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
2
  require 'hipster_sql_to_hbase'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hipster_sql_to_hbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Lescure