Skip to content

Commit c9c6b7b

Browse files
author
Ary Borenszweig
committed
Merge
2 parents 1d7b2d8 + 90f43cf commit c9c6b7b

10 files changed

+108
-87
lines changed

README.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Extensions
4040
Rgviz supports the following extra functions:
4141

4242
* *concat*: converts each of its arguments to a string and then concatenates them. For example: <tt>concat(1, '-', '2')</tt> returns <tt>'1-2'</tt>. Can also receive just a single argument to convert it to a string.
43+
* *floor*
44+
* *round*
4345

4446
These new functions are not part of Google's query language, but they are very handy so we added them. These functions are also supported by [rgviz-rails](https://github.com/asterite/rgviz-rails).
4547

lib/rgviz/lexer.rb

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def next_token
6060
elsif scan /desc\b/i then Token::Desc
6161
elsif scan /ends\b/i then Token::Ends
6262
elsif scan /false\b/i then Token::False
63+
elsif scan /floor\b/i then Token::Floor
6364
elsif scan /format\b/i then Token::Format
6465
elsif scan /group\b/i then Token::Group
6566
elsif scan /hour\b/i then Token::Hour
@@ -85,6 +86,7 @@ def next_token
8586
elsif scan /order\b/i then Token::Order
8687
elsif scan /pivot\b/i then Token::Pivot
8788
elsif scan /quarter\b/i then Token::Quarter
89+
elsif scan /round\b/i then Token::Round
8890
elsif scan /second\b/i then Token::Second
8991
elsif scan /select\b/i then Token::Select
9092
elsif scan /starts\b/i then Token::Starts

lib/rgviz/memory_executor.rb

+4
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ def visit_scalar_function_column(node)
454454
@value = val1.sec
455455
when ScalarFunctionColumn::Quarter
456456
@value = (val1.month / 3.0).ceil
457+
when ScalarFunctionColumn::Round
458+
@value = val1.round
459+
when ScalarFunctionColumn::Floor
460+
@value = val1.floor
457461
when ScalarFunctionColumn::Millisecond
458462
raise "Millisecond is not implemented"
459463
when ScalarFunctionColumn::Lower

lib/rgviz/nodes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ class ScalarFunctionColumn
422422
Product = Token::STAR
423423
Quarter = Token::Quarter
424424
Quotient = Token::SLASH
425+
Round = Token::Round
426+
Floor = Token::Floor
425427
Second = Token::Second
426428
Sum = Token::PLUS
427429
ToDate = Token::ToDate

lib/rgviz/parser.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ def parse_atomic_column
386386
when Token::Year, Token::Month, Token::Day,
387387
Token::Hour, Token::Minute, Token::Second, Token::Millisecond,
388388
Token::Now, Token::DateDiff, Token::Lower, Token::Upper,
389-
Token::Quarter, Token::DayOfWeek, Token::ToDate, Token::Concat
389+
Token::Quarter, Token::DayOfWeek, Token::ToDate, Token::Concat,
390+
Token::Round,Token::Floor
390391
function = @token.value
391392
string = @token.string
392393
next_token
@@ -428,12 +429,12 @@ def check!(*token_values)
428429
check *token_values
429430
next_token
430431
end
431-
432+
432433
protected
433434
def parse_date(date_string)
434435
Date.parse(date_string)
435436
end
436-
437+
437438
def parse_time(time_string)
438439
Time.parse(time_string)
439440
end

lib/rgviz/token.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Token
1616
DayOfWeek = :dayOfWeek
1717
Ends = :ends
1818
False = :false
19+
Floor = :floor
1920
Format = :format
2021
Group = :group
2122
Hour = :hour
@@ -41,6 +42,7 @@ class Token
4142
Order = :order
4243
Pivot = :pivot
4344
Quarter = :quarter
45+
Round = :round
4446
Second = :second
4547
Select = :select
4648
Starts = :starts
@@ -53,17 +55,17 @@ class Token
5355
Where = :where
5456
With = :with
5557
Year = :year
56-
58+
5759
ID = :ID
5860
INTEGER = :INTEGER
5961
DECIMAL = :DECIMAL
6062
STRING = :STRING
61-
63+
6264
PLUS = :'+'
6365
MINUS = :'-'
6466
STAR = :'*'
6567
SLASH = :'/'
66-
68+
6769
COMMA = :','
6870
LPAREN = :'('
6971
RPAREN = :')'
@@ -73,9 +75,9 @@ class Token
7375
GT = :'>'
7476
GTE = :'>='
7577
NEQ = :'!='
76-
78+
7779
EOF = :'<EOF>'
78-
80+
7981
attr_accessor :start
8082
attr_accessor :value
8183
attr_accessor :string

rgviz.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = %q{rgviz}
3-
s.version = "0.45"
3+
s.version = "0.46"
44

55
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
66
s.authors = ["Ary Borenszweig"]

spec/rgviz/lexer_spec.rb

+17-15
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def self.it_lexes_keyword(str, token_value)
99
tok = lex.next_token
1010
tok.value.should == token_value
1111
end
12-
12+
1313
it "lexes #{str} upcase" do
1414
lex = Lexer.new str.upcase
1515
tok = lex.next_token
1616
tok.value.should == token_value
1717
end
1818
end
19-
19+
2020
def self.it_lexes_id(str, id = str)
2121
it "lexes identifier #{str}" do
2222
lex = Lexer.new str
@@ -25,7 +25,7 @@ def self.it_lexes_id(str, id = str)
2525
tok.string.should == id
2626
end
2727
end
28-
28+
2929
def self.it_lexes_string(str, id = str)
3030
it "lexes string #{str}" do
3131
lex = Lexer.new str
@@ -34,15 +34,15 @@ def self.it_lexes_string(str, id = str)
3434
tok.string.should == id
3535
end
3636
end
37-
37+
3838
def self.it_lexes_token(str, token_value)
3939
it "lexes #{str}" do
4040
lex = Lexer.new str
4141
tok = lex.next_token
4242
tok.value.should == token_value
4343
end
4444
end
45-
45+
4646
def self.it_lexes_integer(str, number)
4747
it "lexes #{str}" do
4848
lex = Lexer.new str
@@ -51,7 +51,7 @@ def self.it_lexes_integer(str, number)
5151
tok.number.should == number
5252
end
5353
end
54-
54+
5555
def self.it_lexes_decimal(str, number)
5656
it "lexes #{str}" do
5757
lex = Lexer.new str
@@ -60,15 +60,15 @@ def self.it_lexes_decimal(str, number)
6060
tok.number.should == number
6161
end
6262
end
63-
63+
6464
def self.it_lexes_eof(str)
6565
it "lexes eof #{str}" do
6666
lex = Lexer.new str
6767
tok = lex.next_token
6868
tok.value.should == Token::EOF
6969
end
7070
end
71-
71+
7272
def self.it_lexes_error(str)
7373
it "lexes error #{str}" do
7474
lex = Lexer.new "x#{str}"
@@ -78,7 +78,7 @@ def self.it_lexes_error(str)
7878
lambda { lex.next_token }.should raise_error(ParseException)
7979
end
8080
end
81-
81+
8282
it_lexes_keyword 'and', Token::And
8383
it_lexes_keyword 'asc', Token::Asc
8484
it_lexes_keyword 'avg', Token::Avg
@@ -130,7 +130,7 @@ def self.it_lexes_error(str)
130130
it_lexes_keyword 'where', Token::Where
131131
it_lexes_keyword 'with', Token::With
132132
it_lexes_keyword 'year', Token::Year
133-
133+
134134
it_lexes_id 'selected'
135135
it_lexes_id ' selected ', 'selected'
136136
it_lexes_id '`selected`', 'selected'
@@ -139,12 +139,12 @@ def self.it_lexes_error(str)
139139
it_lexes_id 'hello_123_bye'
140140
it_lexes_string "'hello world'", "hello world"
141141
it_lexes_string '"hello world"', "hello world"
142-
142+
143143
it_lexes_token '+', Token::PLUS
144144
it_lexes_token '-', Token::MINUS
145145
it_lexes_token '*', Token::STAR
146146
it_lexes_token '/', Token::SLASH
147-
147+
148148
it_lexes_token ',', Token::COMMA
149149
it_lexes_token '(', Token::LPAREN
150150
it_lexes_token ')', Token::RPAREN
@@ -155,19 +155,21 @@ def self.it_lexes_error(str)
155155
it_lexes_token '>=', Token::GTE
156156
it_lexes_token '!=', Token::NEQ
157157
it_lexes_token '<>', Token::NEQ
158-
158+
159159
it_lexes_integer '1', 1
160160
it_lexes_integer '0123', 123
161161
it_lexes_integer '45678791230', 45678791230
162162
it_lexes_decimal '123.456', 123.456
163163
it_lexes_decimal '.456', 0.456
164-
164+
165165
it_lexes_eof ''
166166
it_lexes_eof " \t\n\r"
167-
167+
168168
it_lexes_error '!'
169169
it_lexes_error '?'
170170
it_lexes_error ':'
171171

172172
it_lexes_keyword 'concat', Token::Concat
173+
it_lexes_keyword 'round', Token::Round
174+
it_lexes_keyword 'floor', Token::Floor
173175
end

spec/rgviz/memory_executor_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def self.it_processes_single_select_column(query, id, type, value, label, test_o
9595
it_processes_single_select_column "minute(datetime '2010-01-02 10:11:12')", 'c0', :number, 11, "minute(datetime '2010-01-02 10:11:12')"
9696
it_processes_single_select_column "second(datetime '2010-01-02 10:11:12')", 'c0', :number, 12, "second(datetime '2010-01-02 10:11:12')"
9797
it_processes_single_select_column "quarter(datetime '2010-04-02 10:11:12')", 'c0', :number, 2, "quarter(datetime '2010-04-02 10:11:12')"
98+
it_processes_single_select_column "round(1.2)", 'c0', :number, 1, "round(1.2)", :extensions => true
99+
it_processes_single_select_column "round(1.9)", 'c0', :number, 2, "round(1.9)", :extensions => true
100+
it_processes_single_select_column "floor(1.2)", 'c0', :number, 1, "floor(1.2)", :extensions => true
101+
it_processes_single_select_column "floor(1.9)", 'c0', :number, 1, "floor(1.9)", :extensions => true
98102
it_processes_single_select_column "lower('FOO')", 'c0', :string, 'foo', "lower('FOO')"
99103
it_processes_single_select_column "upper('foo')", 'c0', :string, 'FOO', "upper('foo')"
100104
it_processes_single_select_column "toDate(date '2010-10-12')", 'c0', :date, '2010-10-12', "toDate(date '2010-10-12')"

0 commit comments

Comments
 (0)