Skip to content

Commit 51f3b40

Browse files
committed
support for UDF
1 parent d635037 commit 51f3b40

5 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎lib/grammar.coffee‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ grammar =
9595
o 'Number'
9696
o 'String'
9797
o 'Function'
98+
o 'UserFunction'
9899
]
99100

100101
Number: [
@@ -112,6 +113,10 @@ grammar =
112113
Function: [
113114
o "FUNCTION LEFT_PAREN ArgumentList RIGHT_PAREN", -> new FunctionValue($1, $3)
114115
]
116+
117+
UserFunction: [
118+
o "LITERAL LEFT_PAREN ArgumentList RIGHT_PAREN", -> new FunctionValue($1, $3, true)
119+
]
115120

116121
ArgumentList: [
117122
o 'Value', -> [$1]

‎lib/nodes.coffee‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.NumberValue = class LiteralValue
2929
toString: -> @value.toString()
3030

3131
exports.FunctionValue = class FunctionValue
32-
constructor: (@name, @arguments=[]) -> null
32+
constructor: (@name, @arguments=[], @udf=false) -> null
3333
toString: -> "#{@name}(#{@arguments.join(', ')})"
3434

3535
exports.Order = class Order

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sql-parser",
33
"description": "Lexer and Parser for SQL Syntax",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"author": {
66
"name": "Andy Kent",
77
"email": "andy@forward.co.uk"

‎spec/grammar.spec.coffee‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ describe "SQL Grammer", ->
9696
GROUP BY `x`, `y`
9797
HAVING (COUNT(`y`) > 1)
9898
"""
99+
100+
it "parses UDFs", ->
101+
expect(parse("SELECT LENGTH(a) FROM my_table").toString()).toEqual """
102+
SELECT LENGTH(`a`)
103+
FROM `my_table`
104+
"""

‎spec/lexer.spec.coffee‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ describe "SQL Lexer", ->
2020
#
2121
# console.log(tokens)
2222
#
23-
# tokens = lexer.tokenize("SELECT name, age FROM my_stream WHERE")
23+
# tokens = lexer.tokenize("SELECT LENGTH(foo) FROM data")
2424
#
2525
# console.log(tokens)
2626
#
27-
tokens = lexer.tokenize """
28-
SELECT x AS `first_name`, min(age)
29-
FROM my_stream
30-
WHERE age > 10.2 AND (age < 30 OR first_name = 'andy')
31-
GROUP BY age, name
32-
ORDER BY age DESC
33-
HAVING COUNT(*) > 5"""
27+
# tokens = lexer.tokenize """
28+
# SELECT x AS `first_name`, min(age)
29+
# FROM my_stream
30+
# WHERE age > 10.2 AND (age < 30 OR first_name = 'andy')
31+
# GROUP BY age, name
32+
# ORDER BY age DESC
33+
# HAVING COUNT(*) > 5"""
3434

3535
console.log(tokens)

0 commit comments

Comments
 (0)