├── .travis.yml
├── index.js
├── .idea
├── misc.xml
├── vcs.xml
├── modules.xml
├── druid-sql-parser.iml
└── workspace.xml
├── package.json
├── LICENSE
├── README.md
├── druidsqltostring.js
├── druidsql.pegjs
├── test
└── test.js
└── druidsql.js
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "stable"
4 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const parse = require('./druidsql');
2 | const stringify = require('./druidsqltostring');
3 |
4 | module.exports = {
5 | parse: parse.parse,
6 | stringify: stringify.toSQL
7 | }
8 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/druid-sql-parser.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "druid-sql-parser",
3 | "version": "1.2.6",
4 | "description": "Parses Sql to an AST and re-stringifies SQL ASTs",
5 | "main": "index.js",
6 | "homepage": "https://github.com/mcbrewster/druid-sql-parser",
7 | "scripts": {
8 | "test": "mocha"
9 | },
10 | "keywords": [
11 | "sql",
12 | "sql-parser",
13 | "parser",
14 | "node",
15 | "node-parser",
16 | "node-sql-parser",
17 | "ast",
18 | "sql-ast"
19 | ],
20 | "author": "Maggie Brewster",
21 | "license": "MIT",
22 | "devDependencies": {
23 | "mocha": "^6.1.4"
24 | },
25 | "dependencies": {}
26 | }
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 mcbrewster
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/mcbrewster/druid-sql-parser)
2 | [](//npmjs.com/package/druid-sql-parser)
3 | # Druid Sql Parser
4 | Parses Sql to an AST and re-stringifies SQL ASTs
5 |
6 | ## Set up
7 |
8 | install druid-sql-parser
9 |
10 | `npm i druid-sql-parser`
11 |
12 | ## Sql to AST
13 |
14 | ```
15 | const parser = require('druid-sql-parser');
16 | cosnt ast = parser.parse('SELECT "server" FROM sys.server_segments');
17 | console.log(ast);
18 |
19 | ```
20 | logs:
21 | ```
22 | {
23 | "type": "query",
24 | "queryType": "SELECT",
25 | "selectParts": [
26 | {
27 | "type": "selectPart",
28 | "distinct": null,
29 | "expr": {
30 | "type": "variable",
31 | "value": "server",
32 | "spacing": [],
33 | "quote": "\""
34 | },
35 | "alias": null,
36 | "spacing": [
37 | " "
38 | ]
39 | }
40 | ],
41 | "from": {
42 | "type": "from",
43 | "value": {
44 | "type": "table",
45 | "schema": "sys",
46 | "alias": null,
47 | "table": "server_segments",
48 | "spacing": [
49 | " "
50 | ]
51 | },
52 | "spacing": [
53 | " "
54 | ],
55 | "syntax": "FROM"
56 | },
57 | "where": null,
58 | "groupby": null,
59 | "having": null,
60 | "orderBy": null,
61 | "limit": null,
62 | "unionAll": null,
63 | "syntax": "SELECT",
64 | "spacing": [],
65 | "endSpacing": []
66 | }
67 |
68 | ```
69 |
70 | ## AST to SQL
71 |
72 | ```
73 | const parser = require('druid-sql-parser');
74 | ast = {
75 | "type": "query",
76 | "queryType": "SELECT",
77 | "selectParts": [
78 | {
79 | "type": "selectPart",
80 | "distinct": null,
81 | "expr": {
82 | "type": "variable",
83 | "value": "server",
84 | "spacing": [],
85 | "quote": "\""
86 | },
87 | "alias": null,
88 | "spacing": [
89 | " "
90 | ]
91 | }
92 | ],
93 | "from": {
94 | "type": "from",
95 | "value": {
96 | "type": "table",
97 | "schema": "sys",
98 | "alias": null,
99 | "table": "server_segments",
100 | "spacing": [
101 | " "
102 | ]
103 | },
104 | "spacing": [
105 | " "
106 | ],
107 | "syntax": "FROM"
108 | },
109 | "where": null,
110 | "groupby": null,
111 | "having": null,
112 | "orderBy": null,
113 | "limit": null,
114 | "unionAll": null,
115 | "syntax": "SELECT",
116 | "spacing": [],
117 | "endSpacing": []
118 | }
119 | const sql = parser.stringify(ast);
120 | ```
121 | logs: `'SELECT "server" FROM sys.server_segments'`
122 |
123 | ## License
124 | [MIT](LICENSE)
125 |
--------------------------------------------------------------------------------
/druidsqltostring.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Generated by PEG.js 0.10.0.
3 | *
4 | * http://pegjs.org/
5 | */
6 | let sqlString = [];
7 |
8 | function toString(ast) {
9 | sqlString = [];
10 | TypeToValue(ast);
11 | return sqlString.join("");
12 | }
13 |
14 | function TypeToValue(value) {
15 | if (value.spacing) {
16 | value.spacing.map((spacing) => {
17 | sqlString.push(spacing);
18 | })
19 |
20 | }
21 | switch(value.type){
22 | case 'expressionOnly':
23 | TypeToValue(value.expression);
24 | value.endSpacing.map((spacing) => {
25 | sqlString.push(spacing);
26 | });
27 | break;
28 | case 'query':
29 | sqlString.push(value.syntax);
30 | value.selectParts.map((selectpart) => {
31 | TypeToValue(selectpart);
32 | })
33 | TypeToValue(value.from);
34 | if(value.where) {
35 | TypeToValue(value.where);
36 | }
37 | if(value.groupby) {
38 | TypeToValue(value.groupby);
39 | }
40 | if(value.having) {
41 | TypeToValue(value.having);
42 | }
43 | if(value.orderBy) {
44 | TypeToValue(value.orderBy);
45 | }
46 | if(value.limit) {
47 | TypeToValue(value.limit);
48 | }
49 | if(value.unionAll) {
50 | TypeToValue(value.limit);
51 | }
52 | value.endSpacing.map((spacing) => {
53 | sqlString.push(spacing);
54 | });
55 | break;
56 | case 'where':
57 | sqlString.push(value.syntax);
58 | TypeToValue(value.expr);
59 | break;
60 | case 'having':
61 | sqlString.push(value.syntax);
62 | TypeToValue(value.expr);
63 | break;
64 | case 'innerJoin':
65 | sqlString.push(value.syntax);
66 | TypeToValue(value.expr);
67 | break;
68 | case 'limit':
69 | sqlString.push(value.syntax);
70 | TypeToValue(value.value);
71 | break;
72 | case 'orderBy':
73 | sqlString.push(value.syntax);
74 | value.orderByParts.map((orderByPart, index) => {
75 | TypeToValue(orderByPart);
76 | })
77 | break;
78 | case 'selectPart':
79 | if(value.paren) {
80 | sqlString.push('(')
81 | }
82 | if(value.distinct) {
83 | TypeToValue(value.distinct)
84 | }
85 | TypeToValue(value.expr);
86 | if(value.alias){
87 | TypeToValue(value.alias);
88 | }
89 | if(value.paren) {
90 | sqlString.push(')')
91 | }
92 | break;
93 | case 'variable':
94 |
95 | sqlString.push(value.quote + value.value + value.quote);
96 | break;
97 | case 'Constant':
98 | sqlString.push(value.value);
99 | break;
100 | case 'star':
101 | sqlString.push("*");
102 | break;
103 | case 'function':
104 | sqlString.push(value.functionCall + "(");
105 | value.arguments.map((argument, index) => {
106 | TypeToValue(argument);
107 | })
108 | sqlString.push(")");
109 | break;
110 | case 'from':
111 | sqlString.push(value.syntax);
112 | TypeToValue(value.value);
113 | break;
114 | case 'table':
115 | if(value.table.type) {
116 | TypeToValue(value.table);
117 | } else {
118 | sqlString.push( value.schema ? value.schema + "." + value.table : value.table) ;
119 | }
120 | break;
121 | case 'argument':
122 | if(value.distinct) {
123 | TypeToValue(value.distinct)
124 | }
125 | TypeToValue(value.argumentValue);
126 | break;
127 | case 'argumentValue':
128 | if(value.argument.type) {
129 | TypeToValue(value.argument);
130 | } else {
131 | sqlString.push(value.argument);
132 | }
133 | break;
134 | case 'distinct' :
135 | sqlString.push(value.distinct);
136 | break;
137 | case 'groupBy':
138 | sqlString.push(value.syntax);
139 | value.groupByParts.map((groupByPart, index) => {
140 | TypeToValue(groupByPart);
141 | })
142 | break;
143 | case 'orderByPart':
144 | value.expr.map((expr, index) => {
145 | TypeToValue(expr);
146 | })
147 | if(value.direction) {
148 | TypeToValue(value.direction);
149 | }
150 | break;
151 | case 'direction':
152 | sqlString.push(value.direction);
153 | break;
154 | case 'exprPart':
155 | TypeToValue(value.value);
156 | break;
157 | case 'Integer':
158 | sqlString.push(value.value);
159 | break;
160 | case 'Interval':
161 | sqlString.push(value.value);
162 | break;
163 | case 'binaryExpression':
164 | TypeToValue(value.lhs);
165 | TypeToValue(value.operator);
166 | TypeToValue(value.rhs);
167 | break;
168 | case 'expression':
169 | TypeToValue(value.lhs);
170 | TypeToValue(value.operator);
171 | TypeToValue(value.rhs);
172 | break;
173 | case 'operator':
174 | sqlString.push(value.operator);
175 | break;
176 | case 'timestamp':
177 | sqlString.push("TIMESTAMP " + "'" + value.value + "'");
178 | break;
179 | case 'case':
180 | sqlString.push(value.syntax);
181 | if(value.caseValue) {
182 | TypeToValue(value.caseValue);
183 | }
184 | value.when.map((when, index) => {
185 | TypeToValue(when);
186 | })
187 | if(value.elseValue) {
188 | TypeToValue(value.elseValue);
189 | }
190 | TypeToValue(value.end);
191 | break;
192 | case 'caseValue':
193 | TypeToValue(value.caseValue);
194 | break;
195 | case 'when':
196 | sqlString.push(value.syntax);
197 | TypeToValue(value.when);
198 | TypeToValue(value.then);
199 | break;
200 | case 'elseValue':
201 | sqlString.push(value.syntax);
202 | TypeToValue(value.elseValue);
203 | break;
204 | case 'end':
205 | sqlString.push(value.syntax);
206 | break;
207 | case 'alias':
208 | sqlString.push(value.syntax);
209 | TypeToValue(value.value);
210 | break;
211 | case 'then':
212 | sqlString.push(value.syntax);
213 | TypeToValue(value.then);
214 | break;
215 | }
216 |
217 | }
218 |
219 | module.exports = {
220 | toSQL: toString
221 | };
222 |
--------------------------------------------------------------------------------
/druidsql.pegjs:
--------------------------------------------------------------------------------
1 | {
2 | var functions = ["COUNT",
3 | "SUM","MIN", "MAX","AVG","APPROX_COUNT_DISTINCT",
4 | "APPROX_COUNT_DISTINCT_DS_HLL", "APPROX_COUNT_DISTINCT_DS_THETA",
5 | "APPROX_QUANTILE", "APPROX_QUANTILE_DS", "APPROX_QUANTILE_FIXED_BUCKETS",
6 | "BLOOM_FILTER", "ABS", "CEIL", "EXP", "FLOOR", "LN", "LOG10", "POWER", "SQRT",
7 | "TRUNCATE", "TRUNC", "ROUND", "MOD", "SIN", "COS", "TAN", "COT", "ASIN", "ACOS",
8 | "ATAN", "ATAN2", "DEGREES", "RADIANS", "CONCAT", "TEXTCAT", "STRING_FORMAT",
9 | "LENGTH", "CHAR_LENGTH", "CHARARACTER_LENGTH", "STRLEN", "LOOKUP", "LOWER",
10 | "PARSE_LONG", "POSITION", "REGEXP_EXTRACT", "REPLACE", "STRPOS", "SUBSTRING",
11 | "RIGHT", "LEFT", "SUBSTR", "TRIM", "BTRIM", "LTRIM", "RTRIM", "UPPER", "REVERSE",
12 | "REPEAT", "LPAD", "RPAD", "CURRENT_TIMESTAMP", "CURRENT_DATE", "DATE_TRUNC",
13 | "TIME_FLOOR", "TIME_SHIFT", "TIME_EXTRACT", "TIME_PARSE", "TIME_FORMAT",
14 | "MILLIS_TO_TIMESTAMP", "TIMESTAMP_TO_MILIS", "EXTRACT", "FLOOR", "CEIL", "TIMESTAMPADD",
15 | "timestamp_expr", "CAST", "NULLIF", "COALESCE", "BLOOM_FILTER_TEST"];
16 | }
17 |
18 | Start =
19 | Query
20 | / spacing:_ expression: Expression endspacing:_ {
21 | return {type: 'expressionOnly',
22 | spacing: spacing,
23 | expression: expression,
24 | endSpacing: endspacing }}
25 |
26 | Query =
27 | spacing: _
28 | syntax: "SELECT"i
29 | selectParts: SelectParts
30 | from: From?
31 | where: Where?
32 | groupby: GroupBy?
33 | having: Having?
34 | orderBy: OrderBy?
35 | limit: Limit?
36 | unionAll: UnionAll?
37 | endSpacing: _
38 | {
39 | return {
40 | type: 'query',
41 | queryType: "SELECT",
42 | selectParts: selectParts,
43 | from: from,
44 | where: where,
45 | groupby: groupby,
46 | having: having,
47 | orderBy: orderBy,
48 | limit: limit,
49 | unionAll: unionAll,
50 | syntax: syntax,
51 | spacing: spacing,
52 | endSpacing: endSpacing
53 | }
54 | }
55 |
56 | SelectParts =
57 | SelectPart: (SelectPart)+
58 | {
59 | return SelectPart
60 | }
61 |
62 | SelectPart =
63 | spacing: _
64 | distinct: Distinct?
65 | selectPart: (
66 | Variable
67 | / Function
68 | / Case
69 | / Constant
70 | / star
71 | )
72 | alias:Alias?
73 | {
74 | return {
75 | type: "selectPart",
76 | distinct: distinct,
77 | expr: selectPart,
78 | alias: alias,
79 | spacing: spacing
80 | }
81 | }
82 |
83 | From =
84 | spacing: _
85 | syntax: "FROM"i
86 | value: (
87 | Query
88 | / Table
89 | )
90 | {
91 | return {
92 | type: 'from',
93 | value: value,
94 | spacing: spacing,
95 | syntax: syntax
96 | }
97 | }
98 |
99 | Where =
100 | spacing: _
101 | syntax: "WHERE"i
102 | expr:
103 | Expression
104 | {
105 | return {
106 | type: "where",
107 | expr: expr,
108 | spacing: spacing,
109 | syntax: syntax
110 | }
111 | }
112 |
113 | GroupBy =
114 | spacing: _
115 | syntax:"GROUP BY"i
116 | groupByParts: GroupByPart+
117 | {
118 | return {
119 | type: 'groupBy',
120 | groupByParts: groupByParts,
121 | spacing: spacing,
122 | syntax: syntax
123 | }
124 | }
125 |
126 | GroupByPart =
127 | !Reserved
128 | groupByPart: (
129 | Integer
130 | / Variable
131 | / Constant
132 | )
133 | {
134 | return groupByPart
135 | }
136 |
137 |
138 | Having =
139 | spacing: _
140 | syntax: "HAVING"i
141 | expr: (
142 | Expression
143 | / BinaryExpression
144 | )
145 | {
146 | return {
147 | type: "having",
148 | expr: expr,
149 | spacing: spacing,
150 | syntax: syntax
151 | }
152 | }
153 |
154 | OrderBy =
155 | spacing: _
156 | syntax: "ORDER BY"i
157 | orderByParts: OrderByPart+
158 | {
159 | return {
160 | type: 'orderBy',
161 | orderByParts: orderByParts,
162 | spacing: spacing,
163 | syntax: syntax
164 | }
165 | }
166 |
167 | OrderByPart =
168 | spacing: _
169 | !"LIMIT"i
170 | expr: ExprPart+
171 | direction: Direction?
172 | {
173 | return {
174 | type: "orderByPart",
175 | expr: expr,
176 | direction: direction,
177 | spacing: spacing
178 | }
179 | }
180 |
181 | Direction =
182 | spacing: _
183 | direction: (
184 | "DESC"i
185 | / "ASC"i
186 | )
187 | {
188 | return {
189 | type: 'direction',
190 | direction: direction,
191 | spacing: spacing
192 | }
193 | }
194 |
195 | ExprPart =
196 | spacing: _
197 | !Direction
198 | value: (
199 | Function
200 | / Variable
201 | / Constant
202 | )
203 | {
204 | return {
205 | type: 'exprPart',
206 | value: value,
207 | spacing: spacing
208 | }
209 | }
210 |
211 | Limit =
212 | spacing: _
213 | syntax: "LIMIT"i
214 | value: Integer
215 | {
216 | return {
217 | type: 'limit',
218 | value: value,
219 | spacing: spacing,
220 | syntax: syntax
221 | }
222 | }
223 |
224 | UnionAll =
225 | spacing: _
226 | syntax: "UNION ALL"i
227 | newQuery: Query
228 | {
229 | return {
230 | type: 'unionAll',
231 | expr: newQuery,
232 | spacing: spacing,
233 | syntax:syntax
234 | }
235 | }
236 |
237 | Case =
238 | spacing:_
239 | syntax: "CASE"i
240 | caseValue: CaseValue?
241 | whenClause: WhenClause+
242 | elseValue: ElseValue
243 | end: End?
244 | {
245 | return {
246 | type: "case",
247 | caseValue: caseValue,
248 | when: whenClause,
249 | elseValue: elseValue,
250 | end: end,
251 | spacing: spacing,
252 | syntax: syntax
253 |
254 | }
255 | }
256 |
257 | CaseValue =
258 | spacing: _
259 | !"WHEN"i
260 | caseValue: (Variable/Constant)
261 | {
262 | return {
263 | type: 'caseValue',
264 | caseValue: caseValue,
265 | spacing: spacing
266 | }
267 | }
268 |
269 | ElseValue =
270 | spacing: _
271 | syntax: "ELSE"i?
272 | elseValue: (
273 | BinaryExpression
274 | / Expression
275 | / Integer
276 | / Variable
277 | / Constant
278 | )?
279 | {
280 | return {
281 | type: 'elseValue',
282 | elseValue: elseValue,
283 | spacing: spacing,
284 | syntax: syntax
285 | }
286 | }
287 |
288 | End =
289 | spacing: _
290 | syntax: "END"i
291 | {
292 | return {
293 | type:'end',
294 | spacing: spacing,
295 | syntax: syntax
296 | }
297 | }
298 |
299 | WhenClause =
300 | spacing:_
301 | syntax: "WHEN"i
302 | when: (
303 | BinaryExpression
304 | / Expression
305 | / Variable
306 | / Constant
307 | / Integer
308 | )
309 | then: Then
310 | {
311 | return {
312 | type:'when',
313 | when: when,
314 | then: then,
315 | syntax: syntax,
316 | spacing: spacing
317 | }
318 | }
319 |
320 | Then =
321 | spacing: _
322 | syntax: "THEN"i
323 | then: (
324 | Integer
325 | / Case
326 | / BinaryExpression
327 | / Expression
328 | / Variable
329 | )
330 | {
331 | return {
332 | type: 'then',
333 | syntax: syntax,
334 | then: then,
335 | spacing: spacing
336 | }
337 | }
338 |
339 |
340 | BinaryExpression =
341 | spacing: _
342 | lhs: (
343 | Expression
344 | / Function
345 | / TimeStamp
346 | / Variable
347 | / Constant
348 | / Integer
349 | )?
350 | operator: BinaryOperator
351 | rhs: (
352 | BinaryExpression
353 | / Function
354 | / TimeStamp
355 | / Expression
356 | / Variable
357 | / Constant
358 | / Integer
359 | )?
360 | {
361 | return {
362 | type: "binaryExpression",
363 | operator: operator,
364 | lhs: lhs,
365 | rhs: rhs,
366 | spacing: spacing
367 | }
368 | }
369 |
370 | Expression =
371 | spacing: _
372 | lhs: (
373 | Function
374 | / TimeStamp
375 | / Variable
376 | / Constant
377 | / Integer
378 | )
379 | operator: (
380 | Operator /
381 | BinaryOperator
382 | )
383 | rhs: (
384 | Expression
385 | / Function
386 | / TimeStamp
387 | / Interval
388 | / Variable
389 | / Constant
390 | / Integer
391 | )
392 | {
393 | return {
394 | type: "expression",
395 | operator: operator,
396 | lhs: lhs,
397 | rhs: rhs,
398 | spacing: spacing
399 | }
400 | }
401 |
402 | Function =
403 | spacing: _
404 | functionCall: Functions
405 | OpenParen
406 | argument: Argument+
407 | CloseParen
408 | {
409 | return {
410 | type: "function",
411 | functionCall: functionCall,
412 | arguments: argument,
413 | spacing: spacing
414 | }
415 | }
416 |
417 | Distinct =
418 | spacing: _
419 | distinct: "DISTINCT"i
420 | {
421 | return {
422 | type: 'distinct',
423 | distinct: distinct,
424 | spacing: spacing
425 | }
426 | }
427 |
428 | Argument =
429 | distinct: Distinct?
430 | argumentValue: ArgumentValue
431 | {
432 | return {
433 | type: 'argument',
434 | distinct: distinct,
435 | argumentValue: argumentValue
436 | }
437 | }
438 |
439 | ArgumentValue =
440 | spacing: _
441 | !Reserved
442 | argument: (
443 | Constant
444 | / Variable
445 | / star
446 | / [^(), ]+
447 | )
448 | {
449 | return {
450 | type:'argumentValue',
451 | spacing: spacing,
452 | argument: Array.isArray(argument) ? argument.join("") : argument
453 | }
454 | }
455 |
456 | Variable =
457 | spacing: _
458 | quote: (
459 | QuoteMark
460 | / Apostrophe
461 | )
462 | value: [^"'()]+
463 | (
464 | QuoteMark
465 | / Apostrophe
466 | )
467 | {
468 | return {
469 | type: "variable",
470 | value: value.join(""),
471 | spacing: spacing,
472 | quote: quote
473 | }
474 | }
475 |
476 | Constant =
477 | spacing: _
478 | !Reserved
479 | value: [a-zA-Z_.]+
480 | {
481 | return {
482 | type: "Constant",
483 | value: value.join(""),
484 | spacing: spacing
485 | }
486 | }
487 |
488 | Integer =
489 | spacing: _
490 | value: [0-9]+
491 | {
492 | return {
493 | type: "Integer",
494 | value: value.join(""),
495 | spacing: spacing
496 | }
497 | }
498 |
499 | TimeStamp =
500 | spacing: _
501 | "TIMESTAMP"i
502 | _ Apostrophe
503 | timeStamp: [0-9" ":-]+
504 | Apostrophe {
505 | return {
506 | type: "timestamp",
507 | value: timeStamp.join(""),
508 | spacing: spacing
509 | }
510 | }
511 |
512 | Operator =
513 | spacing:_ operator:(
514 | "+"
515 | /"-"
516 | /"/"
517 | /"*"
518 | /"="
519 | )
520 | {
521 | return {
522 | type: 'operator',
523 | spacing: spacing,
524 | operator: operator
525 | }
526 | }
527 |
528 | Interval =
529 | spacing:_
530 | "INTERVAL"i
531 | value: (
532 | Apostrophe
533 | / Integer
534 | / Apostrophe
535 | / Variable
536 | )
537 | constant: Constant
538 | {
539 | return {
540 | type: "interval",
541 | value: value,
542 | constant: constant,
543 | spacing: spacing
544 | }
545 | }
546 |
547 | Alias =
548 | spacing:_ syntax:"AS"i value:(Variable/Constant/Integer) {
549 | return {type:'alias',
550 | value: value,
551 | spacing: spacing,
552 | syntax: syntax
553 | }
554 | }
555 |
556 | Functions =
557 | Function: IdentifierPart {
558 | if (functions.includes(Function)) {
559 | return Function
560 | }
561 | }
562 |
563 |
564 |
565 | BinaryOperator =
566 | spacing: _ !Parts operator:(
567 | ">="
568 | / ">"
569 | / "=<"
570 | / "="
571 | / "!="
572 | / "<"
573 | / "<>"
574 | / "BETWEEN"i
575 | / "NOT BETWEEN"i
576 | / "NOT LIKE"i
577 | / "LIKE"i
578 | / "IS NULL"i
579 | / "IS NOT NULL"i
580 | / "IS TRUE"i
581 | / "IS NOT TRUE"i
582 | / "IS FALSE"i
583 | / "IN"i
584 | / "NOT IN"i
585 | / "NOT IN"i
586 | / "OR"i
587 | / "AND"i
588 | / "NOT"i
589 | )
590 | {
591 | return {
592 | type: 'operator',
593 | operator: operator,
594 | spacing: spacing
595 | }
596 | }
597 | _
598 | = [ \t\n\r(),;]*
599 |
600 | Table =
601 | spacing: _
602 | schema: (
603 | IdentifierPart
604 | Dot
605 | )?
606 | table: (
607 | Variable
608 | / IdentifierPart
609 | )
610 | alias: Alias?
611 | {
612 | return {
613 | type: 'table',
614 | schema: schema ? schema.join("").replace(/[,.]/g, ""): null,
615 | alias: alias,
616 | table: table,
617 | spacing: spacing
618 | }
619 | }
620 |
621 | star =
622 | "*"
623 | {
624 | return {
625 | type: "star",
626 | }
627 | }
628 |
629 | OpenParen =
630 | "("
631 |
632 | CloseParen =
633 | ")"
634 |
635 | QuoteMark =
636 | "\""
637 |
638 | Comma =
639 | ","
640 |
641 | SemiColon =
642 | ";"
643 |
644 | Apostrophe =
645 | "\'"
646 |
647 | Dot =
648 | "."
649 |
650 | IdentifierPart =
651 | part:[a-z_]i+
652 | {
653 | return part.join("")
654 | }
655 |
656 | Reserved =
657 | BinaryOperator
658 | /Operator
659 | /Function
660 | /Parts
661 |
662 | Parts =
663 | "FROM"i
664 | /"WHERE"i
665 | /"GROUP BY"i
666 | /"HAVING"i
667 | /"LIMIT"i
668 | /"UNION ALL"i
669 | /"SELECT"i
670 | /"AS"i
671 | /"ORDER BY"i
672 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 | equal
114 | distinct
115 | deep
116 |
117 |
118 |
119 |
120 |
121 |
122 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 | 1562873626635
213 |
214 |
215 | 1562873626635
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | const parse = require('../druidsql');
2 | const stringify = require('../druidsqltostring');
3 | const assert = require('assert');
4 |
5 |
6 | Object.compare = function (obj1, obj2) {
7 | for (const p in obj1) {
8 | if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
9 |
10 | switch (typeof (obj1[p])) {
11 | case 'object':
12 | if (!Object.compare(obj1[p], obj2[p])) return false;
13 | break;
14 | case 'function':
15 | if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;
16 | break;
17 | default:
18 | if (obj1[p] != obj2[p]) return false;
19 | }
20 | }
21 |
22 | //Check object 2 for any extra properties
23 | for (const p in obj2) {
24 | if (typeof (obj1[p]) == 'undefined') return false;
25 | }
26 | return true;
27 | };
28 |
29 | describe('Expression Tests', () =>
30 | {
31 | it('basic expression', () => {
32 | ast = parse.parse('1 + 1');
33 | expected = {
34 | "type": "expressionOnly",
35 | "spacing": [],
36 | "expression": {
37 | "type": "expression",
38 | "operator": {
39 | "type": "operator",
40 | "spacing": [
41 | " "
42 | ],
43 | "operator": "+"
44 | },
45 | "lhs": {
46 | "type": "Integer",
47 | "value": "1",
48 | "spacing": []
49 | },
50 | "rhs": {
51 | "type": "Integer",
52 | "value": "1",
53 | "spacing": [
54 | " "
55 | ]
56 | },
57 | "spacing": []
58 | },
59 | "endSpacing": []
60 | }
61 |
62 | assert.equal(true,Object.compare(ast, expected));
63 | })
64 | it('expression with all operators', () => {
65 | ast = parse.parse('1 + 1 / 1 * 1 - 1');
66 | expected = {
67 | "type": "expressionOnly",
68 | "spacing": [],
69 | "expression": {
70 | "type": "expression",
71 | "operator": {
72 | "type": "operator",
73 | "spacing": [
74 | " "
75 | ],
76 | "operator": "+"
77 | },
78 | "lhs": {
79 | "type": "Integer",
80 | "value": "1",
81 | "spacing": []
82 | },
83 | "rhs": {
84 | "type": "expression",
85 | "operator": {
86 | "type": "operator",
87 | "spacing": [
88 | " "
89 | ],
90 | "operator": "/"
91 | },
92 | "lhs": {
93 | "type": "Integer",
94 | "value": "1",
95 | "spacing": []
96 | },
97 | "rhs": {
98 | "type": "expression",
99 | "operator": {
100 | "type": "operator",
101 | "spacing": [
102 | " "
103 | ],
104 | "operator": "*"
105 | },
106 | "lhs": {
107 | "type": "Integer",
108 | "value": "1",
109 | "spacing": []
110 | },
111 | "rhs": {
112 | "type": "expression",
113 | "operator": {
114 | "type": "operator",
115 | "spacing": [
116 | " "
117 | ],
118 | "operator": "-"
119 | },
120 | "lhs": {
121 | "type": "Integer",
122 | "value": "1",
123 | "spacing": []
124 | },
125 | "rhs": {
126 | "type": "Integer",
127 | "value": "1",
128 | "spacing": [
129 | " "
130 | ]
131 | },
132 | "spacing": [
133 | " "
134 | ]
135 | },
136 | "spacing": [
137 | " "
138 | ]
139 | },
140 | "spacing": [
141 | " "
142 | ]
143 | },
144 | "spacing": []
145 | },
146 | "endSpacing": []
147 | }
148 | assert.equal(true,Object.compare(ast, expected));
149 | })
150 | it('expression with brackets', () => {
151 | ast = parse.parse('2 * (3 + 4)');
152 | expected = {
153 | "type": "expressionOnly",
154 | "spacing": [],
155 | "expression": {
156 | "type": "expression",
157 | "operator": {
158 | "type": "operator",
159 | "spacing": [
160 | " "
161 | ],
162 | "operator": "*"
163 | },
164 | "lhs": {
165 | "type": "Integer",
166 | "value": "2",
167 | "spacing": []
168 | },
169 | "rhs": {
170 | "type": "expression",
171 | "operator": {
172 | "type": "operator",
173 | "spacing": [
174 | " "
175 | ],
176 | "operator": "+"
177 | },
178 | "lhs": {
179 | "type": "Integer",
180 | "value": "3",
181 | "spacing": []
182 | },
183 | "rhs": {
184 | "type": "Integer",
185 | "value": "4",
186 | "spacing": [
187 | " "
188 | ]
189 | },
190 | "spacing": [
191 | " ",
192 | "("
193 | ]
194 | },
195 | "spacing": []
196 | },
197 | "endSpacing": [
198 | ")"
199 | ]
200 | }
201 |
202 | assert.equal(true,Object.compare(ast, expected));
203 | })
204 | it('expression with string values', () => {
205 | ast = parse.parse('\'column\' = "value"');
206 | expected = {
207 | "type": "expressionOnly",
208 | "spacing": [],
209 | "expression": {
210 | "type": "expression",
211 | "operator": {
212 | "type": "operator",
213 | "spacing": [
214 | " "
215 | ],
216 | "operator": "="
217 | },
218 | "lhs": {
219 | "type": "variable",
220 | "value": "column",
221 | "spacing": [],
222 | "quote": "'"
223 | },
224 | "rhs": {
225 | "type": "variable",
226 | "value": "value",
227 | "spacing": [
228 | " "
229 | ],
230 | "quote": "\""
231 | },
232 | "spacing": []
233 | },
234 | "endSpacing": []
235 | }
236 |
237 | assert.equal(true,Object.compare(ast, expected));
238 | })
239 | });
240 |
241 | describe('Druid Query Tests', () =>
242 | {
243 | it('data sources query', () => {
244 | ast = parse.parse('SELECT\n' +
245 | ' datasource,\n' +
246 | ' COUNT(*) AS num_segments,\n' +
247 | ' SUM(is_available) AS num_available_segments,\n' +
248 | ' SUM("size") AS size,\n' +
249 | ' SUM("num_rows") AS num_rows\n' +
250 | 'FROM sys.segments\n' +
251 | 'GROUP BY 1');
252 | expected = {
253 | "type": "query",
254 | "queryType": "SELECT",
255 | "selectParts": [
256 | {
257 | "type": "selectPart",
258 | "distinct": null,
259 | "expr": {
260 | "type": "Constant",
261 | "value": "datasource",
262 | "spacing": []
263 | },
264 | "alias": null,
265 | "spacing": [
266 | "\n",
267 | " ",
268 | " "
269 | ]
270 | },
271 | {
272 | "type": "selectPart",
273 | "distinct": null,
274 | "expr": {
275 | "type": "Constant",
276 | "value": "COUNT",
277 | "spacing": []
278 | },
279 | "alias": null,
280 | "spacing": [
281 | ",",
282 | "\n",
283 | " ",
284 | " "
285 | ]
286 | },
287 | {
288 | "type": "selectPart",
289 | "distinct": null,
290 | "expr": {
291 | "type": "star"
292 | },
293 | "alias": {
294 | "type": "alias",
295 | "value": {
296 | "type": "Constant",
297 | "value": "num_segments",
298 | "spacing": [
299 | " "
300 | ]
301 | },
302 | "spacing": [
303 | ")",
304 | " "
305 | ],
306 | "syntax": "AS"
307 | },
308 | "spacing": [
309 | "("
310 | ]
311 | },
312 | {
313 | "type": "selectPart",
314 | "distinct": null,
315 | "expr": {
316 | "type": "function",
317 | "functionCall": "SUM",
318 | "arguments": [
319 | {
320 | "type": "argument",
321 | "distinct": null,
322 | "argumentValue": {
323 | "type": "argumentValue",
324 | "spacing": [],
325 | "argument": {
326 | "type": "Constant",
327 | "value": "is_available",
328 | "spacing": []
329 | }
330 | }
331 | }
332 | ],
333 | "spacing": []
334 | },
335 | "alias": {
336 | "type": "alias",
337 | "value": {
338 | "type": "Constant",
339 | "value": "num_available_segments",
340 | "spacing": [
341 | " "
342 | ]
343 | },
344 | "spacing": [
345 | " "
346 | ],
347 | "syntax": "AS"
348 | },
349 | "spacing": [
350 | ",",
351 | "\n",
352 | " ",
353 | " "
354 | ]
355 | },
356 | {
357 | "type": "selectPart",
358 | "distinct": null,
359 | "expr": {
360 | "type": "function",
361 | "functionCall": "SUM",
362 | "arguments": [
363 | {
364 | "type": "argument",
365 | "distinct": null,
366 | "argumentValue": {
367 | "type": "argumentValue",
368 | "spacing": [],
369 | "argument": {
370 | "type": "variable",
371 | "value": "size",
372 | "spacing": [],
373 | "quote": "\""
374 | }
375 | }
376 | }
377 | ],
378 | "spacing": []
379 | },
380 | "alias": {
381 | "type": "alias",
382 | "value": {
383 | "type": "Constant",
384 | "value": "size",
385 | "spacing": [
386 | " "
387 | ]
388 | },
389 | "spacing": [
390 | " "
391 | ],
392 | "syntax": "AS"
393 | },
394 | "spacing": [
395 | ",",
396 | "\n",
397 | " ",
398 | " "
399 | ]
400 | },
401 | {
402 | "type": "selectPart",
403 | "distinct": null,
404 | "expr": {
405 | "type": "function",
406 | "functionCall": "SUM",
407 | "arguments": [
408 | {
409 | "type": "argument",
410 | "distinct": null,
411 | "argumentValue": {
412 | "type": "argumentValue",
413 | "spacing": [],
414 | "argument": {
415 | "type": "variable",
416 | "value": "num_rows",
417 | "spacing": [],
418 | "quote": "\""
419 | }
420 | }
421 | }
422 | ],
423 | "spacing": []
424 | },
425 | "alias": {
426 | "type": "alias",
427 | "value": {
428 | "type": "Constant",
429 | "value": "num_rows",
430 | "spacing": [
431 | " "
432 | ]
433 | },
434 | "spacing": [
435 | " "
436 | ],
437 | "syntax": "AS"
438 | },
439 | "spacing": [
440 | ",",
441 | "\n",
442 | " ",
443 | " "
444 | ]
445 | }
446 | ],
447 | "from": {
448 | "type": "from",
449 | "value": {
450 | "type": "table",
451 | "schema": "sys",
452 | "alias": null,
453 | "table": "segments",
454 | "spacing": [
455 | " "
456 | ]
457 | },
458 | "spacing": [
459 | "\n"
460 | ],
461 | "syntax": "FROM"
462 | },
463 | "where": null,
464 | "groupby": {
465 | "type": "groupBy",
466 | "groupByParts": [
467 | {
468 | "type": "Integer",
469 | "value": "1",
470 | "spacing": [
471 | " "
472 | ]
473 | }
474 | ],
475 | "spacing": [
476 | "\n"
477 | ],
478 | "syntax": "GROUP BY"
479 | },
480 | "having": null,
481 | "orderBy": null,
482 | "limit": null,
483 | "unionAll": null,
484 | "syntax": "SELECT",
485 | "spacing": [],
486 | "endSpacing": []
487 | }
488 | assert.equal(true,Object.compare(ast, expected));
489 | })
490 | it('segments query', () => {
491 | ast = parse.parse('SELECT "segment_id", "datasource", "start", "end", "size", "version", "partition_num", "num_replicas", "num_rows", "is_published", "is_available", "is_realtime", "is_overshadowed", "payload"\n' +
492 | 'FROM sys.segments\n' +
493 | 'ORDER BY "start" DESC\n' +
494 | 'LIMIT 50');
495 | expected = {
496 | "type": "query",
497 | "queryType": "SELECT",
498 | "selectParts": [
499 | {
500 | "type": "selectPart",
501 | "distinct": null,
502 | "expr": {
503 | "type": "variable",
504 | "value": "segment_id",
505 | "spacing": [],
506 | "quote": "\""
507 | },
508 | "alias": null,
509 | "spacing": [
510 | " "
511 | ]
512 | },
513 | {
514 | "type": "selectPart",
515 | "distinct": null,
516 | "expr": {
517 | "type": "variable",
518 | "value": "datasource",
519 | "spacing": [],
520 | "quote": "\""
521 | },
522 | "alias": null,
523 | "spacing": [
524 | ",",
525 | " "
526 | ]
527 | },
528 | {
529 | "type": "selectPart",
530 | "distinct": null,
531 | "expr": {
532 | "type": "variable",
533 | "value": "start",
534 | "spacing": [],
535 | "quote": "\""
536 | },
537 | "alias": null,
538 | "spacing": [
539 | ",",
540 | " "
541 | ]
542 | },
543 | {
544 | "type": "selectPart",
545 | "distinct": null,
546 | "expr": {
547 | "type": "variable",
548 | "value": "end",
549 | "spacing": [],
550 | "quote": "\""
551 | },
552 | "alias": null,
553 | "spacing": [
554 | ",",
555 | " "
556 | ]
557 | },
558 | {
559 | "type": "selectPart",
560 | "distinct": null,
561 | "expr": {
562 | "type": "variable",
563 | "value": "size",
564 | "spacing": [],
565 | "quote": "\""
566 | },
567 | "alias": null,
568 | "spacing": [
569 | ",",
570 | " "
571 | ]
572 | },
573 | {
574 | "type": "selectPart",
575 | "distinct": null,
576 | "expr": {
577 | "type": "variable",
578 | "value": "version",
579 | "spacing": [],
580 | "quote": "\""
581 | },
582 | "alias": null,
583 | "spacing": [
584 | ",",
585 | " "
586 | ]
587 | },
588 | {
589 | "type": "selectPart",
590 | "distinct": null,
591 | "expr": {
592 | "type": "variable",
593 | "value": "partition_num",
594 | "spacing": [],
595 | "quote": "\""
596 | },
597 | "alias": null,
598 | "spacing": [
599 | ",",
600 | " "
601 | ]
602 | },
603 | {
604 | "type": "selectPart",
605 | "distinct": null,
606 | "expr": {
607 | "type": "variable",
608 | "value": "num_replicas",
609 | "spacing": [],
610 | "quote": "\""
611 | },
612 | "alias": null,
613 | "spacing": [
614 | ",",
615 | " "
616 | ]
617 | },
618 | {
619 | "type": "selectPart",
620 | "distinct": null,
621 | "expr": {
622 | "type": "variable",
623 | "value": "num_rows",
624 | "spacing": [],
625 | "quote": "\""
626 | },
627 | "alias": null,
628 | "spacing": [
629 | ",",
630 | " "
631 | ]
632 | },
633 | {
634 | "type": "selectPart",
635 | "distinct": null,
636 | "expr": {
637 | "type": "variable",
638 | "value": "is_published",
639 | "spacing": [],
640 | "quote": "\""
641 | },
642 | "alias": null,
643 | "spacing": [
644 | ",",
645 | " "
646 | ]
647 | },
648 | {
649 | "type": "selectPart",
650 | "distinct": null,
651 | "expr": {
652 | "type": "variable",
653 | "value": "is_available",
654 | "spacing": [],
655 | "quote": "\""
656 | },
657 | "alias": null,
658 | "spacing": [
659 | ",",
660 | " "
661 | ]
662 | },
663 | {
664 | "type": "selectPart",
665 | "distinct": null,
666 | "expr": {
667 | "type": "variable",
668 | "value": "is_realtime",
669 | "spacing": [],
670 | "quote": "\""
671 | },
672 | "alias": null,
673 | "spacing": [
674 | ",",
675 | " "
676 | ]
677 | },
678 | {
679 | "type": "selectPart",
680 | "distinct": null,
681 | "expr": {
682 | "type": "variable",
683 | "value": "is_overshadowed",
684 | "spacing": [],
685 | "quote": "\""
686 | },
687 | "alias": null,
688 | "spacing": [
689 | ",",
690 | " "
691 | ]
692 | },
693 | {
694 | "type": "selectPart",
695 | "distinct": null,
696 | "expr": {
697 | "type": "variable",
698 | "value": "payload",
699 | "spacing": [],
700 | "quote": "\""
701 | },
702 | "alias": null,
703 | "spacing": [
704 | ",",
705 | " "
706 | ]
707 | }
708 | ],
709 | "from": {
710 | "type": "from",
711 | "value": {
712 | "type": "table",
713 | "schema": "sys",
714 | "alias": null,
715 | "table": "segments",
716 | "spacing": [
717 | " "
718 | ]
719 | },
720 | "spacing": [
721 | "\n"
722 | ],
723 | "syntax": "FROM"
724 | },
725 | "where": null,
726 | "groupby": null,
727 | "having": null,
728 | "orderBy": {
729 | "type": "orderBy",
730 | "orderByParts": [
731 | {
732 | "type": "orderByPart",
733 | "expr": [
734 | {
735 | "type": "exprPart",
736 | "value": {
737 | "type": "variable",
738 | "value": "start",
739 | "spacing": [],
740 | "quote": "\""
741 | },
742 | "spacing": []
743 | }
744 | ],
745 | "direction": {
746 | "type": "direction",
747 | "direction": "DESC",
748 | "spacing": [
749 | " "
750 | ]
751 | },
752 | "spacing": [
753 | " "
754 | ]
755 | }
756 | ],
757 | "spacing": [
758 | "\n"
759 | ],
760 | "syntax": "ORDER BY"
761 | },
762 | "limit": {
763 | "type": "limit",
764 | "value": {
765 | "type": "Integer",
766 | "value": "50",
767 | "spacing": [
768 | " "
769 | ]
770 | },
771 | "spacing": [
772 | "\n"
773 | ],
774 | "syntax": "LIMIT"
775 | },
776 | "unionAll": null,
777 | "syntax": "SELECT",
778 | "spacing": [],
779 | "endSpacing": []
780 | }
781 | assert.equal(true,Object.compare(ast, expected));
782 | })
783 | it('task query', () => {
784 | ast = parse.parse('SELECT\n' +
785 | ' "task_id", "type", "datasource", "created_time", "location", "duration", "error_msg",\n' +
786 | ' CASE WHEN "status" = \'RUNNING\' THEN "runner_status" ELSE "status" END AS "status",\n' +
787 | ' (\n' +
788 | ' CASE WHEN "status" = \'RUNNING\' THEN\n' +
789 | ' (CASE "runner_status" WHEN \'RUNNING\' THEN 4 WHEN \'PENDING\' THEN 3 ELSE 2 END)\n' +
790 | ' ELSE 1\n' +
791 | ' END\n' +
792 | ' ) AS "rank"\n' +
793 | 'FROM sys.tasks\n' +
794 | 'ORDER BY "rank" DESC, "created_time" DESC');
795 | expected = {
796 | "type": "query",
797 | "queryType": "SELECT",
798 | "selectParts": [
799 | {
800 | "type": "selectPart",
801 | "distinct": null,
802 | "expr": {
803 | "type": "variable",
804 | "value": "task_id",
805 | "spacing": [],
806 | "quote": "\""
807 | },
808 | "alias": null,
809 | "spacing": [
810 | "\n",
811 | " ",
812 | " "
813 | ]
814 | },
815 | {
816 | "type": "selectPart",
817 | "distinct": null,
818 | "expr": {
819 | "type": "variable",
820 | "value": "type",
821 | "spacing": [],
822 | "quote": "\""
823 | },
824 | "alias": null,
825 | "spacing": [
826 | ",",
827 | " "
828 | ]
829 | },
830 | {
831 | "type": "selectPart",
832 | "distinct": null,
833 | "expr": {
834 | "type": "variable",
835 | "value": "datasource",
836 | "spacing": [],
837 | "quote": "\""
838 | },
839 | "alias": null,
840 | "spacing": [
841 | ",",
842 | " "
843 | ]
844 | },
845 | {
846 | "type": "selectPart",
847 | "distinct": null,
848 | "expr": {
849 | "type": "variable",
850 | "value": "created_time",
851 | "spacing": [],
852 | "quote": "\""
853 | },
854 | "alias": null,
855 | "spacing": [
856 | ",",
857 | " "
858 | ]
859 | },
860 | {
861 | "type": "selectPart",
862 | "distinct": null,
863 | "expr": {
864 | "type": "variable",
865 | "value": "location",
866 | "spacing": [],
867 | "quote": "\""
868 | },
869 | "alias": null,
870 | "spacing": [
871 | ",",
872 | " "
873 | ]
874 | },
875 | {
876 | "type": "selectPart",
877 | "distinct": null,
878 | "expr": {
879 | "type": "variable",
880 | "value": "duration",
881 | "spacing": [],
882 | "quote": "\""
883 | },
884 | "alias": null,
885 | "spacing": [
886 | ",",
887 | " "
888 | ]
889 | },
890 | {
891 | "type": "selectPart",
892 | "distinct": null,
893 | "expr": {
894 | "type": "variable",
895 | "value": "error_msg",
896 | "spacing": [],
897 | "quote": "\""
898 | },
899 | "alias": null,
900 | "spacing": [
901 | ",",
902 | " "
903 | ]
904 | },
905 | {
906 | "type": "selectPart",
907 | "distinct": null,
908 | "expr": {
909 | "type": "case",
910 | "caseValue": null,
911 | "when": [
912 | {
913 | "type": "when",
914 | "when": {
915 | "type": "expression",
916 | "operator": {
917 | "type": "operator",
918 | "spacing": [
919 | " "
920 | ],
921 | "operator": "="
922 | },
923 | "lhs": {
924 | "type": "variable",
925 | "value": "status",
926 | "spacing": [],
927 | "quote": "\""
928 | },
929 | "rhs": {
930 | "type": "variable",
931 | "value": "RUNNING",
932 | "spacing": [
933 | " "
934 | ],
935 | "quote": "'"
936 | },
937 | "spacing": [
938 | " "
939 | ]
940 | },
941 | "then": {
942 | "type": "then",
943 | "syntax": "THEN",
944 | "then": {
945 | "type": "variable",
946 | "value": "runner_status",
947 | "spacing": [
948 | " "
949 | ],
950 | "quote": "\""
951 | },
952 | "spacing": [
953 | " "
954 | ]
955 | },
956 | "syntax": "WHEN",
957 | "spacing": [
958 | " "
959 | ]
960 | }
961 | ],
962 | "elseValue": {
963 | "type": "elseValue",
964 | "elseValue": {
965 | "type": "variable",
966 | "value": "status",
967 | "spacing": [
968 | " "
969 | ],
970 | "quote": "\""
971 | },
972 | "spacing": [
973 | " "
974 | ],
975 | "syntax": "ELSE"
976 | },
977 | "end": {
978 | "type": "end",
979 | "spacing": [
980 | " "
981 | ],
982 | "syntax": "END"
983 | },
984 | "spacing": [],
985 | "syntax": "CASE"
986 | },
987 | "alias": {
988 | "type": "alias",
989 | "value": {
990 | "type": "variable",
991 | "value": "status",
992 | "spacing": [
993 | " "
994 | ],
995 | "quote": "\""
996 | },
997 | "spacing": [
998 | " "
999 | ],
1000 | "syntax": "AS"
1001 | },
1002 | "spacing": [
1003 | ",",
1004 | "\n",
1005 | " ",
1006 | " "
1007 | ]
1008 | },
1009 | {
1010 | "type": "selectPart",
1011 | "distinct": null,
1012 | "expr": {
1013 | "type": "case",
1014 | "caseValue": null,
1015 | "when": [
1016 | {
1017 | "type": "when",
1018 | "when": {
1019 | "type": "expression",
1020 | "operator": {
1021 | "type": "operator",
1022 | "spacing": [
1023 | " "
1024 | ],
1025 | "operator": "="
1026 | },
1027 | "lhs": {
1028 | "type": "variable",
1029 | "value": "status",
1030 | "spacing": [],
1031 | "quote": "\""
1032 | },
1033 | "rhs": {
1034 | "type": "variable",
1035 | "value": "RUNNING",
1036 | "spacing": [
1037 | " "
1038 | ],
1039 | "quote": "'"
1040 | },
1041 | "spacing": [
1042 | " "
1043 | ]
1044 | },
1045 | "then": {
1046 | "type": "then",
1047 | "syntax": "THEN",
1048 | "then": {
1049 | "type": "case",
1050 | "caseValue": {
1051 | "type": "caseValue",
1052 | "caseValue": {
1053 | "type": "variable",
1054 | "value": "runner_status",
1055 | "spacing": [],
1056 | "quote": "\""
1057 | },
1058 | "spacing": [
1059 | " "
1060 | ]
1061 | },
1062 | "when": [
1063 | {
1064 | "type": "when",
1065 | "when": {
1066 | "type": "variable",
1067 | "value": "RUNNING",
1068 | "spacing": [
1069 | " "
1070 | ],
1071 | "quote": "'"
1072 | },
1073 | "then": {
1074 | "type": "then",
1075 | "syntax": "THEN",
1076 | "then": {
1077 | "type": "Integer",
1078 | "value": "4",
1079 | "spacing": [
1080 | " "
1081 | ]
1082 | },
1083 | "spacing": [
1084 | " "
1085 | ]
1086 | },
1087 | "syntax": "WHEN",
1088 | "spacing": [
1089 | " "
1090 | ]
1091 | },
1092 | {
1093 | "type": "when",
1094 | "when": {
1095 | "type": "variable",
1096 | "value": "PENDING",
1097 | "spacing": [
1098 | " "
1099 | ],
1100 | "quote": "'"
1101 | },
1102 | "then": {
1103 | "type": "then",
1104 | "syntax": "THEN",
1105 | "then": {
1106 | "type": "Integer",
1107 | "value": "3",
1108 | "spacing": [
1109 | " "
1110 | ]
1111 | },
1112 | "spacing": [
1113 | " "
1114 | ]
1115 | },
1116 | "syntax": "WHEN",
1117 | "spacing": [
1118 | " "
1119 | ]
1120 | }
1121 | ],
1122 | "elseValue": {
1123 | "type": "elseValue",
1124 | "elseValue": {
1125 | "type": "Integer",
1126 | "value": "2",
1127 | "spacing": [
1128 | " "
1129 | ]
1130 | },
1131 | "spacing": [
1132 | " "
1133 | ],
1134 | "syntax": "ELSE"
1135 | },
1136 | "end": {
1137 | "type": "end",
1138 | "spacing": [
1139 | " "
1140 | ],
1141 | "syntax": "END"
1142 | },
1143 | "spacing": [
1144 | "\n",
1145 | " ",
1146 | " ",
1147 | " ",
1148 | " ",
1149 | " ",
1150 | "("
1151 | ],
1152 | "syntax": "CASE"
1153 | },
1154 | "spacing": [
1155 | " "
1156 | ]
1157 | },
1158 | "syntax": "WHEN",
1159 | "spacing": [
1160 | " "
1161 | ]
1162 | }
1163 | ],
1164 | "elseValue": {
1165 | "type": "elseValue",
1166 | "elseValue": {
1167 | "type": "Integer",
1168 | "value": "1",
1169 | "spacing": [
1170 | " "
1171 | ]
1172 | },
1173 | "spacing": [
1174 | ")",
1175 | "\n",
1176 | " ",
1177 | " ",
1178 | " ",
1179 | " "
1180 | ],
1181 | "syntax": "ELSE"
1182 | },
1183 | "end": {
1184 | "type": "end",
1185 | "spacing": [
1186 | "\n",
1187 | " ",
1188 | " ",
1189 | " ",
1190 | " "
1191 | ],
1192 | "syntax": "END"
1193 | },
1194 | "spacing": [],
1195 | "syntax": "CASE"
1196 | },
1197 | "alias": {
1198 | "type": "alias",
1199 | "value": {
1200 | "type": "variable",
1201 | "value": "rank",
1202 | "spacing": [
1203 | " "
1204 | ],
1205 | "quote": "\""
1206 | },
1207 | "spacing": [
1208 | "\n",
1209 | " ",
1210 | " ",
1211 | ")",
1212 | " "
1213 | ],
1214 | "syntax": "AS"
1215 | },
1216 | "spacing": [
1217 | ",",
1218 | "\n",
1219 | " ",
1220 | " ",
1221 | "(",
1222 | "\n",
1223 | " ",
1224 | " ",
1225 | " ",
1226 | " "
1227 | ]
1228 | }
1229 | ],
1230 | "from": {
1231 | "type": "from",
1232 | "value": {
1233 | "type": "table",
1234 | "schema": "sys",
1235 | "alias": null,
1236 | "table": "tasks",
1237 | "spacing": [
1238 | " "
1239 | ]
1240 | },
1241 | "spacing": [
1242 | "\n"
1243 | ],
1244 | "syntax": "FROM"
1245 | },
1246 | "where": null,
1247 | "groupby": null,
1248 | "having": null,
1249 | "orderBy": {
1250 | "type": "orderBy",
1251 | "orderByParts": [
1252 | {
1253 | "type": "orderByPart",
1254 | "expr": [
1255 | {
1256 | "type": "exprPart",
1257 | "value": {
1258 | "type": "variable",
1259 | "value": "rank",
1260 | "spacing": [],
1261 | "quote": "\""
1262 | },
1263 | "spacing": []
1264 | }
1265 | ],
1266 | "direction": {
1267 | "type": "direction",
1268 | "direction": "DESC",
1269 | "spacing": [
1270 | " "
1271 | ]
1272 | },
1273 | "spacing": [
1274 | " "
1275 | ]
1276 | },
1277 | {
1278 | "type": "orderByPart",
1279 | "expr": [
1280 | {
1281 | "type": "exprPart",
1282 | "value": {
1283 | "type": "variable",
1284 | "value": "created_time",
1285 | "spacing": [],
1286 | "quote": "\""
1287 | },
1288 | "spacing": []
1289 | }
1290 | ],
1291 | "direction": {
1292 | "type": "direction",
1293 | "direction": "DESC",
1294 | "spacing": [
1295 | " "
1296 | ]
1297 | },
1298 | "spacing": [
1299 | ",",
1300 | " "
1301 | ]
1302 | }
1303 | ],
1304 | "spacing": [
1305 | "\n"
1306 | ],
1307 | "syntax": "ORDER BY"
1308 | },
1309 | "limit": null,
1310 | "unionAll": null,
1311 | "syntax": "SELECT",
1312 | "spacing": [],
1313 | "endSpacing": []
1314 | }
1315 | assert.equal(true,Object.compare(ast, expected));
1316 | })
1317 | it('servers query', () => {
1318 | ast = parse.parse('SELECT\n' +
1319 | ' "server", "server_type", "tier", "host", "plaintext_port", "tls_port", "curr_size", "max_size",\n' +
1320 | ' (\n' +
1321 | ' CASE "server_type"\n' +
1322 | ' WHEN \'coordinator\' THEN 7\n' +
1323 | ' WHEN \'overlord\' THEN 6\n' +
1324 | ' WHEN \'router\' THEN 5\n' +
1325 | ' WHEN \'broker\' THEN 4\n' +
1326 | ' WHEN \'historical\' THEN 3\n' +
1327 | ' WHEN \'middle_manager\' THEN 2\n' +
1328 | ' WHEN \'peon\' THEN 1\n' +
1329 | ' ELSE 0\n' +
1330 | ' END\n' +
1331 | ' ) AS "rank"\n' +
1332 | 'FROM sys.servers\n' +
1333 | 'ORDER BY "rank" DESC, "server" DESC');
1334 | expected = {
1335 | "type": "query",
1336 | "queryType": "SELECT",
1337 | "selectParts": [
1338 | {
1339 | "type": "selectPart",
1340 | "distinct": null,
1341 | "expr": {
1342 | "type": "variable",
1343 | "value": "server",
1344 | "spacing": [],
1345 | "quote": "\""
1346 | },
1347 | "alias": null,
1348 | "spacing": [
1349 | "\n",
1350 | " ",
1351 | " "
1352 | ]
1353 | },
1354 | {
1355 | "type": "selectPart",
1356 | "distinct": null,
1357 | "expr": {
1358 | "type": "variable",
1359 | "value": "server_type",
1360 | "spacing": [],
1361 | "quote": "\""
1362 | },
1363 | "alias": null,
1364 | "spacing": [
1365 | ",",
1366 | " "
1367 | ]
1368 | },
1369 | {
1370 | "type": "selectPart",
1371 | "distinct": null,
1372 | "expr": {
1373 | "type": "variable",
1374 | "value": "tier",
1375 | "spacing": [],
1376 | "quote": "\""
1377 | },
1378 | "alias": null,
1379 | "spacing": [
1380 | ",",
1381 | " "
1382 | ]
1383 | },
1384 | {
1385 | "type": "selectPart",
1386 | "distinct": null,
1387 | "expr": {
1388 | "type": "variable",
1389 | "value": "host",
1390 | "spacing": [],
1391 | "quote": "\""
1392 | },
1393 | "alias": null,
1394 | "spacing": [
1395 | ",",
1396 | " "
1397 | ]
1398 | },
1399 | {
1400 | "type": "selectPart",
1401 | "distinct": null,
1402 | "expr": {
1403 | "type": "variable",
1404 | "value": "plaintext_port",
1405 | "spacing": [],
1406 | "quote": "\""
1407 | },
1408 | "alias": null,
1409 | "spacing": [
1410 | ",",
1411 | " "
1412 | ]
1413 | },
1414 | {
1415 | "type": "selectPart",
1416 | "distinct": null,
1417 | "expr": {
1418 | "type": "variable",
1419 | "value": "tls_port",
1420 | "spacing": [],
1421 | "quote": "\""
1422 | },
1423 | "alias": null,
1424 | "spacing": [
1425 | ",",
1426 | " "
1427 | ]
1428 | },
1429 | {
1430 | "type": "selectPart",
1431 | "distinct": null,
1432 | "expr": {
1433 | "type": "variable",
1434 | "value": "curr_size",
1435 | "spacing": [],
1436 | "quote": "\""
1437 | },
1438 | "alias": null,
1439 | "spacing": [
1440 | ",",
1441 | " "
1442 | ]
1443 | },
1444 | {
1445 | "type": "selectPart",
1446 | "distinct": null,
1447 | "expr": {
1448 | "type": "variable",
1449 | "value": "max_size",
1450 | "spacing": [],
1451 | "quote": "\""
1452 | },
1453 | "alias": null,
1454 | "spacing": [
1455 | ",",
1456 | " "
1457 | ]
1458 | },
1459 | {
1460 | "type": "selectPart",
1461 | "distinct": null,
1462 | "expr": {
1463 | "type": "case",
1464 | "caseValue": {
1465 | "type": "caseValue",
1466 | "caseValue": {
1467 | "type": "variable",
1468 | "value": "server_type",
1469 | "spacing": [],
1470 | "quote": "\""
1471 | },
1472 | "spacing": [
1473 | " "
1474 | ]
1475 | },
1476 | "when": [
1477 | {
1478 | "type": "when",
1479 | "when": {
1480 | "type": "variable",
1481 | "value": "coordinator",
1482 | "spacing": [
1483 | " "
1484 | ],
1485 | "quote": "'"
1486 | },
1487 | "then": {
1488 | "type": "then",
1489 | "syntax": "THEN",
1490 | "then": {
1491 | "type": "Integer",
1492 | "value": "7",
1493 | "spacing": [
1494 | " "
1495 | ]
1496 | },
1497 | "spacing": [
1498 | " "
1499 | ]
1500 | },
1501 | "syntax": "WHEN",
1502 | "spacing": [
1503 | "\n",
1504 | " ",
1505 | " ",
1506 | " ",
1507 | " "
1508 | ]
1509 | },
1510 | {
1511 | "type": "when",
1512 | "when": {
1513 | "type": "variable",
1514 | "value": "overlord",
1515 | "spacing": [
1516 | " "
1517 | ],
1518 | "quote": "'"
1519 | },
1520 | "then": {
1521 | "type": "then",
1522 | "syntax": "THEN",
1523 | "then": {
1524 | "type": "Integer",
1525 | "value": "6",
1526 | "spacing": [
1527 | " "
1528 | ]
1529 | },
1530 | "spacing": [
1531 | " "
1532 | ]
1533 | },
1534 | "syntax": "WHEN",
1535 | "spacing": [
1536 | "\n",
1537 | " ",
1538 | " ",
1539 | " ",
1540 | " "
1541 | ]
1542 | },
1543 | {
1544 | "type": "when",
1545 | "when": {
1546 | "type": "variable",
1547 | "value": "router",
1548 | "spacing": [
1549 | " "
1550 | ],
1551 | "quote": "'"
1552 | },
1553 | "then": {
1554 | "type": "then",
1555 | "syntax": "THEN",
1556 | "then": {
1557 | "type": "Integer",
1558 | "value": "5",
1559 | "spacing": [
1560 | " "
1561 | ]
1562 | },
1563 | "spacing": [
1564 | " "
1565 | ]
1566 | },
1567 | "syntax": "WHEN",
1568 | "spacing": [
1569 | "\n",
1570 | " ",
1571 | " ",
1572 | " ",
1573 | " "
1574 | ]
1575 | },
1576 | {
1577 | "type": "when",
1578 | "when": {
1579 | "type": "variable",
1580 | "value": "broker",
1581 | "spacing": [
1582 | " "
1583 | ],
1584 | "quote": "'"
1585 | },
1586 | "then": {
1587 | "type": "then",
1588 | "syntax": "THEN",
1589 | "then": {
1590 | "type": "Integer",
1591 | "value": "4",
1592 | "spacing": [
1593 | " "
1594 | ]
1595 | },
1596 | "spacing": [
1597 | " "
1598 | ]
1599 | },
1600 | "syntax": "WHEN",
1601 | "spacing": [
1602 | "\n",
1603 | " ",
1604 | " ",
1605 | " ",
1606 | " "
1607 | ]
1608 | },
1609 | {
1610 | "type": "when",
1611 | "when": {
1612 | "type": "variable",
1613 | "value": "historical",
1614 | "spacing": [
1615 | " "
1616 | ],
1617 | "quote": "'"
1618 | },
1619 | "then": {
1620 | "type": "then",
1621 | "syntax": "THEN",
1622 | "then": {
1623 | "type": "Integer",
1624 | "value": "3",
1625 | "spacing": [
1626 | " "
1627 | ]
1628 | },
1629 | "spacing": [
1630 | " "
1631 | ]
1632 | },
1633 | "syntax": "WHEN",
1634 | "spacing": [
1635 | "\n",
1636 | " ",
1637 | " ",
1638 | " ",
1639 | " "
1640 | ]
1641 | },
1642 | {
1643 | "type": "when",
1644 | "when": {
1645 | "type": "variable",
1646 | "value": "middle_manager",
1647 | "spacing": [
1648 | " "
1649 | ],
1650 | "quote": "'"
1651 | },
1652 | "then": {
1653 | "type": "then",
1654 | "syntax": "THEN",
1655 | "then": {
1656 | "type": "Integer",
1657 | "value": "2",
1658 | "spacing": [
1659 | " "
1660 | ]
1661 | },
1662 | "spacing": [
1663 | " "
1664 | ]
1665 | },
1666 | "syntax": "WHEN",
1667 | "spacing": [
1668 | "\n",
1669 | " ",
1670 | " ",
1671 | " ",
1672 | " "
1673 | ]
1674 | },
1675 | {
1676 | "type": "when",
1677 | "when": {
1678 | "type": "variable",
1679 | "value": "peon",
1680 | "spacing": [
1681 | " "
1682 | ],
1683 | "quote": "'"
1684 | },
1685 | "then": {
1686 | "type": "then",
1687 | "syntax": "THEN",
1688 | "then": {
1689 | "type": "Integer",
1690 | "value": "1",
1691 | "spacing": [
1692 | " "
1693 | ]
1694 | },
1695 | "spacing": [
1696 | " "
1697 | ]
1698 | },
1699 | "syntax": "WHEN",
1700 | "spacing": [
1701 | "\n",
1702 | " ",
1703 | " ",
1704 | " ",
1705 | " "
1706 | ]
1707 | }
1708 | ],
1709 | "elseValue": {
1710 | "type": "elseValue",
1711 | "elseValue": {
1712 | "type": "Integer",
1713 | "value": "0",
1714 | "spacing": [
1715 | " "
1716 | ]
1717 | },
1718 | "spacing": [
1719 | "\n",
1720 | " ",
1721 | " ",
1722 | " ",
1723 | " "
1724 | ],
1725 | "syntax": "ELSE"
1726 | },
1727 | "end": {
1728 | "type": "end",
1729 | "spacing": [
1730 | "\n",
1731 | " ",
1732 | " ",
1733 | " ",
1734 | " "
1735 | ],
1736 | "syntax": "END"
1737 | },
1738 | "spacing": [],
1739 | "syntax": "CASE"
1740 | },
1741 | "alias": {
1742 | "type": "alias",
1743 | "value": {
1744 | "type": "variable",
1745 | "value": "rank",
1746 | "spacing": [
1747 | " "
1748 | ],
1749 | "quote": "\""
1750 | },
1751 | "spacing": [
1752 | "\n",
1753 | " ",
1754 | " ",
1755 | ")",
1756 | " "
1757 | ],
1758 | "syntax": "AS"
1759 | },
1760 | "spacing": [
1761 | ",",
1762 | "\n",
1763 | " ",
1764 | " ",
1765 | "(",
1766 | "\n",
1767 | " ",
1768 | " ",
1769 | " ",
1770 | " "
1771 | ]
1772 | }
1773 | ],
1774 | "from": {
1775 | "type": "from",
1776 | "value": {
1777 | "type": "table",
1778 | "schema": "sys",
1779 | "alias": null,
1780 | "table": "servers",
1781 | "spacing": [
1782 | " "
1783 | ]
1784 | },
1785 | "spacing": [
1786 | "\n"
1787 | ],
1788 | "syntax": "FROM"
1789 | },
1790 | "where": null,
1791 | "groupby": null,
1792 | "having": null,
1793 | "orderBy": {
1794 | "type": "orderBy",
1795 | "orderByParts": [
1796 | {
1797 | "type": "orderByPart",
1798 | "expr": [
1799 | {
1800 | "type": "exprPart",
1801 | "value": {
1802 | "type": "variable",
1803 | "value": "rank",
1804 | "spacing": [],
1805 | "quote": "\""
1806 | },
1807 | "spacing": []
1808 | }
1809 | ],
1810 | "direction": {
1811 | "type": "direction",
1812 | "direction": "DESC",
1813 | "spacing": [
1814 | " "
1815 | ]
1816 | },
1817 | "spacing": [
1818 | " "
1819 | ]
1820 | },
1821 | {
1822 | "type": "orderByPart",
1823 | "expr": [
1824 | {
1825 | "type": "exprPart",
1826 | "value": {
1827 | "type": "variable",
1828 | "value": "server",
1829 | "spacing": [],
1830 | "quote": "\""
1831 | },
1832 | "spacing": []
1833 | }
1834 | ],
1835 | "direction": {
1836 | "type": "direction",
1837 | "direction": "DESC",
1838 | "spacing": [
1839 | " "
1840 | ]
1841 | },
1842 | "spacing": [
1843 | ",",
1844 | " "
1845 | ]
1846 | }
1847 | ],
1848 | "spacing": [
1849 | "\n"
1850 | ],
1851 | "syntax": "ORDER BY"
1852 | },
1853 | "limit": null,
1854 | "unionAll": null,
1855 | "syntax": "SELECT",
1856 | "spacing": [],
1857 | "endSpacing": []
1858 | }
1859 | assert.equal(true,Object.compare(ast, expected));
1860 | })
1861 |
1862 | });
1863 |
1864 | describe('Stringify Expression Tests', () =>
1865 | {
1866 | it('basic expression', () => {
1867 | const expression = '1 + 1'
1868 | const ast = parse.parse(expression);
1869 | assert.equal(expression, stringify.toSQL(ast));
1870 | })
1871 | it('expression with all operators', () => {
1872 | const expression = '1 + 1 / 1 * 1 - 1'
1873 | const ast = parse.parse(expression);
1874 | assert.equal(expression, stringify.toSQL(ast));
1875 | })
1876 | it('expression with brackets', () => {
1877 | const expression = '2 * (3 + 4)';
1878 | const ast = parse.parse(expression);
1879 | assert.equal(expression, stringify.toSQL(ast));
1880 | })
1881 | it('expression with string values', () => {
1882 | const expression = '\'column\' = "value"';
1883 | const ast = parse.parse(expression);
1884 | assert.equal(expression, stringify.toSQL(ast));
1885 | })
1886 | });
1887 |
1888 | describe('Stringify Druid Query Tests', () =>
1889 | {
1890 | it('data sources query', () => {
1891 | const expression = 'SELECT\n' +
1892 | ' datasource,\n' +
1893 | ' COUNT(*) AS num_segments,\n' +
1894 | ' SUM(is_available) AS num_available_segments,\n' +
1895 | ' SUM("size") AS size,\n' +
1896 | ' SUM("num_rows") AS num_rows\n' +
1897 | 'FROM sys.segments\n' +
1898 | 'GROUP BY 1';
1899 | const ast = parse.parse(expression);
1900 | assert.equal(expression, stringify.toSQL(ast));
1901 | })
1902 | it('segments query', () => {
1903 | const expression = 'SELECT "segment_id", "datasource", "start", "end", "size", "version", "partition_num", "num_replicas", "num_rows", "is_published", "is_available", "is_realtime", "is_overshadowed", "payload"\n' +
1904 | 'FROM sys.segments\n' +
1905 | 'ORDER BY "start" DESC\n' +
1906 | 'LIMIT 50';
1907 | const ast = parse.parse(expression);
1908 | assert.equal(expression, stringify.toSQL(ast));
1909 | })
1910 | it('task query', () => {
1911 | const expression = 'SELECT\n' +
1912 | ' "task_id", "type", "datasource", "created_time", "location", "duration", "error_msg",\n' +
1913 | ' CASE WHEN "status" = \'RUNNING\' THEN "runner_status" ELSE "status" END AS "status",\n' +
1914 | ' (\n' +
1915 | ' CASE WHEN "status" = \'RUNNING\' THEN\n' +
1916 | ' (CASE "runner_status" WHEN \'RUNNING\' THEN 4 WHEN \'PENDING\' THEN 3 ELSE 2 END)\n' +
1917 | ' ELSE 1\n' +
1918 | ' END\n' +
1919 | ' ) AS "rank"\n' +
1920 | 'FROM sys.tasks\n' +
1921 | 'ORDER BY "rank" DESC, "created_time" DESC';
1922 | const ast = parse.parse(expression);
1923 | assert.equal(expression, stringify.toSQL(ast));
1924 | })
1925 | it('servers query', () => {
1926 | const expression = 'SELECT\n' +
1927 | ' "server", "server_type", "tier", "host", "plaintext_port", "tls_port", "curr_size", "max_size",\n' +
1928 | ' (\n' +
1929 | ' CASE "server_type"\n' +
1930 | ' WHEN \'coordinator\' THEN 7\n' +
1931 | ' WHEN \'overlord\' THEN 6\n' +
1932 | ' WHEN \'router\' THEN 5\n' +
1933 | ' WHEN \'broker\' THEN 4\n' +
1934 | ' WHEN \'historical\' THEN 3\n' +
1935 | ' WHEN \'middle_manager\' THEN 2\n' +
1936 | ' WHEN \'peon\' THEN 1\n' +
1937 | ' ELSE 0\n' +
1938 | ' END\n' +
1939 | ' ) AS "rank"\n' +
1940 | 'FROM sys.servers\n' +
1941 | 'ORDER BY "rank" DESC, "server" DESC';
1942 | const ast = parse.parse(expression);
1943 | assert.equal(expression, stringify.toSQL(ast));
1944 | })
1945 |
1946 | });
1947 |
--------------------------------------------------------------------------------
/druidsql.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Generated by PEG.js 0.10.0.
3 | *
4 | * http://pegjs.org/
5 | */
6 |
7 | "use strict";
8 |
9 | function peg$subclass(child, parent) {
10 | function ctor() { this.constructor = child; }
11 | ctor.prototype = parent.prototype;
12 | child.prototype = new ctor();
13 | }
14 |
15 | function peg$SyntaxError(message, expected, found, location) {
16 | this.message = message;
17 | this.expected = expected;
18 | this.found = found;
19 | this.location = location;
20 | this.name = "SyntaxError";
21 |
22 | if (typeof Error.captureStackTrace === "function") {
23 | Error.captureStackTrace(this, peg$SyntaxError);
24 | }
25 | }
26 |
27 | peg$subclass(peg$SyntaxError, Error);
28 |
29 | peg$SyntaxError.buildMessage = function(expected, found) {
30 | var DESCRIBE_EXPECTATION_FNS = {
31 | literal: function(expectation) {
32 | return "\"" + literalEscape(expectation.text) + "\"";
33 | },
34 |
35 | "class": function(expectation) {
36 | var escapedParts = "",
37 | i;
38 |
39 | for (i = 0; i < expectation.parts.length; i++) {
40 | escapedParts += expectation.parts[i] instanceof Array
41 | ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])
42 | : classEscape(expectation.parts[i]);
43 | }
44 |
45 | return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
46 | },
47 |
48 | any: function(expectation) {
49 | return "any character";
50 | },
51 |
52 | end: function(expectation) {
53 | return "end of input";
54 | },
55 |
56 | other: function(expectation) {
57 | return expectation.description;
58 | }
59 | };
60 |
61 | function hex(ch) {
62 | return ch.charCodeAt(0).toString(16).toUpperCase();
63 | }
64 |
65 | function literalEscape(s) {
66 | return s
67 | .replace(/\\/g, '\\\\')
68 | .replace(/"/g, '\\"')
69 | .replace(/\0/g, '\\0')
70 | .replace(/\t/g, '\\t')
71 | .replace(/\n/g, '\\n')
72 | .replace(/\r/g, '\\r')
73 | .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
74 | .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); });
75 | }
76 |
77 | function classEscape(s) {
78 | return s
79 | .replace(/\\/g, '\\\\')
80 | .replace(/\]/g, '\\]')
81 | .replace(/\^/g, '\\^')
82 | .replace(/-/g, '\\-')
83 | .replace(/\0/g, '\\0')
84 | .replace(/\t/g, '\\t')
85 | .replace(/\n/g, '\\n')
86 | .replace(/\r/g, '\\r')
87 | .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
88 | .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); });
89 | }
90 |
91 | function describeExpectation(expectation) {
92 | return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
93 | }
94 |
95 | function describeExpected(expected) {
96 | var descriptions = new Array(expected.length),
97 | i, j;
98 |
99 | for (i = 0; i < expected.length; i++) {
100 | descriptions[i] = describeExpectation(expected[i]);
101 | }
102 |
103 | descriptions.sort();
104 |
105 | if (descriptions.length > 0) {
106 | for (i = 1, j = 1; i < descriptions.length; i++) {
107 | if (descriptions[i - 1] !== descriptions[i]) {
108 | descriptions[j] = descriptions[i];
109 | j++;
110 | }
111 | }
112 | descriptions.length = j;
113 | }
114 |
115 | switch (descriptions.length) {
116 | case 1:
117 | return descriptions[0];
118 |
119 | case 2:
120 | return descriptions[0] + " or " + descriptions[1];
121 |
122 | default:
123 | return descriptions.slice(0, -1).join(", ")
124 | + ", or "
125 | + descriptions[descriptions.length - 1];
126 | }
127 | }
128 |
129 | function describeFound(found) {
130 | return found ? "\"" + literalEscape(found) + "\"" : "end of input";
131 | }
132 |
133 | return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
134 | };
135 |
136 | function peg$parse(input, options) {
137 | options = options !== void 0 ? options : {};
138 |
139 | var peg$FAILED = {},
140 |
141 | peg$startRuleFunctions = { Start: peg$parseStart },
142 | peg$startRuleFunction = peg$parseStart,
143 |
144 | peg$c0 = function(spacing, expression, endspacing) {
145 | return {type: 'expressionOnly',
146 | spacing: spacing,
147 | expression: expression,
148 | endSpacing: endspacing }},
149 | peg$c1 = "select",
150 | peg$c2 = peg$literalExpectation("SELECT", true),
151 | peg$c3 = function(spacing, syntax, selectParts, from, where, groupby, having, orderBy, limit, unionAll, endSpacing) {
152 | return {
153 | type: 'query',
154 | queryType: "SELECT",
155 | selectParts: selectParts,
156 | from: from,
157 | where: where,
158 | groupby: groupby,
159 | having: having,
160 | orderBy: orderBy,
161 | limit: limit,
162 | unionAll: unionAll,
163 | syntax: syntax,
164 | spacing: spacing,
165 | endSpacing: endSpacing
166 | }
167 | },
168 | peg$c4 = function(SelectPart) {
169 | return SelectPart
170 | },
171 | peg$c5 = function(spacing, distinct, selectPart, alias) {
172 | return {
173 | type: "selectPart",
174 | distinct: distinct,
175 | expr: selectPart,
176 | alias: alias,
177 | spacing: spacing
178 | }
179 | },
180 | peg$c6 = "from",
181 | peg$c7 = peg$literalExpectation("FROM", true),
182 | peg$c8 = function(spacing, syntax, value) {
183 | return {
184 | type: 'from',
185 | value: value,
186 | spacing: spacing,
187 | syntax: syntax
188 | }
189 | },
190 | peg$c9 = "where",
191 | peg$c10 = peg$literalExpectation("WHERE", true),
192 | peg$c11 = function(spacing, syntax, expr) {
193 | return {
194 | type: "where",
195 | expr: expr,
196 | spacing: spacing,
197 | syntax: syntax
198 | }
199 | },
200 | peg$c12 = "group by",
201 | peg$c13 = peg$literalExpectation("GROUP BY", true),
202 | peg$c14 = function(spacing, syntax, groupByParts) {
203 | return {
204 | type: 'groupBy',
205 | groupByParts: groupByParts,
206 | spacing: spacing,
207 | syntax: syntax
208 | }
209 | },
210 | peg$c15 = function(groupByPart) {
211 | return groupByPart
212 | },
213 | peg$c16 = "having",
214 | peg$c17 = peg$literalExpectation("HAVING", true),
215 | peg$c18 = function(spacing, syntax, expr) {
216 | return {
217 | type: "having",
218 | expr: expr,
219 | spacing: spacing,
220 | syntax: syntax
221 | }
222 | },
223 | peg$c19 = "order by",
224 | peg$c20 = peg$literalExpectation("ORDER BY", true),
225 | peg$c21 = function(spacing, syntax, orderByParts) {
226 | return {
227 | type: 'orderBy',
228 | orderByParts: orderByParts,
229 | spacing: spacing,
230 | syntax: syntax
231 | }
232 | },
233 | peg$c22 = "limit",
234 | peg$c23 = peg$literalExpectation("LIMIT", true),
235 | peg$c24 = function(spacing, expr, direction) {
236 | return {
237 | type: "orderByPart",
238 | expr: expr,
239 | direction: direction,
240 | spacing: spacing
241 | }
242 | },
243 | peg$c25 = "desc",
244 | peg$c26 = peg$literalExpectation("DESC", true),
245 | peg$c27 = "asc",
246 | peg$c28 = peg$literalExpectation("ASC", true),
247 | peg$c29 = function(spacing, direction) {
248 | return {
249 | type: 'direction',
250 | direction: direction,
251 | spacing: spacing
252 | }
253 | },
254 | peg$c30 = function(spacing, value) {
255 | return {
256 | type: 'exprPart',
257 | value: value,
258 | spacing: spacing
259 | }
260 | },
261 | peg$c31 = function(spacing, syntax, value) {
262 | return {
263 | type: 'limit',
264 | value: value,
265 | spacing: spacing,
266 | syntax: syntax
267 | }
268 | },
269 | peg$c32 = "union all",
270 | peg$c33 = peg$literalExpectation("UNION ALL", true),
271 | peg$c34 = function(spacing, syntax, newQuery) {
272 | return {
273 | type: 'unionAll',
274 | expr: newQuery,
275 | spacing: spacing,
276 | syntax:syntax
277 | }
278 | },
279 | peg$c35 = "case",
280 | peg$c36 = peg$literalExpectation("CASE", true),
281 | peg$c37 = function(spacing, syntax, caseValue, whenClause, elseValue, end) {
282 | return {
283 | type: "case",
284 | caseValue: caseValue,
285 | when: whenClause,
286 | elseValue: elseValue,
287 | end: end,
288 | spacing: spacing,
289 | syntax: syntax
290 |
291 | }
292 | },
293 | peg$c38 = "when",
294 | peg$c39 = peg$literalExpectation("WHEN", true),
295 | peg$c40 = function(spacing, caseValue) {
296 | return {
297 | type: 'caseValue',
298 | caseValue: caseValue,
299 | spacing: spacing
300 | }
301 | },
302 | peg$c41 = "else",
303 | peg$c42 = peg$literalExpectation("ELSE", true),
304 | peg$c43 = function(spacing, syntax, elseValue) {
305 | return {
306 | type: 'elseValue',
307 | elseValue: elseValue,
308 | spacing: spacing,
309 | syntax: syntax
310 | }
311 | },
312 | peg$c44 = "end",
313 | peg$c45 = peg$literalExpectation("END", true),
314 | peg$c46 = function(spacing, syntax) {
315 | return {
316 | type:'end',
317 | spacing: spacing,
318 | syntax: syntax
319 | }
320 | },
321 | peg$c47 = function(spacing, syntax, when, then) {
322 | return {
323 | type:'when',
324 | when: when,
325 | then: then,
326 | syntax: syntax,
327 | spacing: spacing
328 | }
329 | },
330 | peg$c48 = "then",
331 | peg$c49 = peg$literalExpectation("THEN", true),
332 | peg$c50 = function(spacing, syntax, then) {
333 | return {
334 | type: 'then',
335 | syntax: syntax,
336 | then: then,
337 | spacing: spacing
338 | }
339 | },
340 | peg$c51 = function(spacing, lhs, operator, rhs) {
341 | return {
342 | type: "binaryExpression",
343 | operator: operator,
344 | lhs: lhs,
345 | rhs: rhs,
346 | spacing: spacing
347 | }
348 | },
349 | peg$c52 = function(spacing, lhs, operator, rhs) {
350 | return {
351 | type: "expression",
352 | operator: operator,
353 | lhs: lhs,
354 | rhs: rhs,
355 | spacing: spacing
356 | }
357 | },
358 | peg$c53 = function(spacing, functionCall, argument) {
359 | return {
360 | type: "function",
361 | functionCall: functionCall,
362 | arguments: argument,
363 | spacing: spacing
364 | }
365 | },
366 | peg$c54 = "distinct",
367 | peg$c55 = peg$literalExpectation("DISTINCT", true),
368 | peg$c56 = function(spacing, distinct) {
369 | return {
370 | type: 'distinct',
371 | distinct: distinct,
372 | spacing: spacing
373 | }
374 | },
375 | peg$c57 = function(distinct, argumentValue) {
376 | return {
377 | type: 'argument',
378 | distinct: distinct,
379 | argumentValue: argumentValue
380 | }
381 | },
382 | peg$c58 = /^[^(), ]/,
383 | peg$c59 = peg$classExpectation(["(", ")", ",", " "], true, false),
384 | peg$c60 = function(spacing, argument) {
385 | return {
386 | type:'argumentValue',
387 | spacing: spacing,
388 | argument: Array.isArray(argument) ? argument.join("") : argument
389 | }
390 | },
391 | peg$c61 = /^[^"'()]/,
392 | peg$c62 = peg$classExpectation(["\"", "'", "(", ")"], true, false),
393 | peg$c63 = function(spacing, quote, value) {
394 | return {
395 | type: "variable",
396 | value: value.join(""),
397 | spacing: spacing,
398 | quote: quote
399 | }
400 | },
401 | peg$c64 = /^[a-zA-Z_.]/,
402 | peg$c65 = peg$classExpectation([["a", "z"], ["A", "Z"], "_", "."], false, false),
403 | peg$c66 = function(spacing, value) {
404 | return {
405 | type: "Constant",
406 | value: value.join(""),
407 | spacing: spacing
408 | }
409 | },
410 | peg$c67 = /^[0-9]/,
411 | peg$c68 = peg$classExpectation([["0", "9"]], false, false),
412 | peg$c69 = function(spacing, value) {
413 | return {
414 | type: "Integer",
415 | value: value.join(""),
416 | spacing: spacing
417 | }
418 | },
419 | peg$c70 = "timestamp",
420 | peg$c71 = peg$literalExpectation("TIMESTAMP", true),
421 | peg$c72 = /^[0-9" ":\-]/,
422 | peg$c73 = peg$classExpectation([["0", "9"], "\"", " ", "\"", ":", "-"], false, false),
423 | peg$c74 = function(spacing, timeStamp) {
424 | return {
425 | type: "timestamp",
426 | value: timeStamp.join(""),
427 | spacing: spacing
428 | }
429 | },
430 | peg$c75 = "+",
431 | peg$c76 = peg$literalExpectation("+", false),
432 | peg$c77 = "-",
433 | peg$c78 = peg$literalExpectation("-", false),
434 | peg$c79 = "/",
435 | peg$c80 = peg$literalExpectation("/", false),
436 | peg$c81 = "*",
437 | peg$c82 = peg$literalExpectation("*", false),
438 | peg$c83 = "=",
439 | peg$c84 = peg$literalExpectation("=", false),
440 | peg$c85 = function(spacing, operator) {
441 | return {
442 | type: 'operator',
443 | spacing: spacing,
444 | operator: operator
445 | }
446 | },
447 | peg$c86 = "interval",
448 | peg$c87 = peg$literalExpectation("INTERVAL", true),
449 | peg$c88 = function(spacing, value, constant) {
450 | return {
451 | type: "interval",
452 | value: value,
453 | constant: constant,
454 | spacing: spacing
455 | }
456 | },
457 | peg$c89 = "as",
458 | peg$c90 = peg$literalExpectation("AS", true),
459 | peg$c91 = function(spacing, syntax, value) {
460 | return {type:'alias',
461 | value: value,
462 | spacing: spacing,
463 | syntax: syntax
464 | }
465 | },
466 | peg$c92 = function(Function) {
467 | if (functions.includes(Function)) {
468 | return Function
469 | }
470 | },
471 | peg$c93 = ">=",
472 | peg$c94 = peg$literalExpectation(">=", false),
473 | peg$c95 = ">",
474 | peg$c96 = peg$literalExpectation(">", false),
475 | peg$c97 = "=<",
476 | peg$c98 = peg$literalExpectation("=<", false),
477 | peg$c99 = "!=",
478 | peg$c100 = peg$literalExpectation("!=", false),
479 | peg$c101 = "<",
480 | peg$c102 = peg$literalExpectation("<", false),
481 | peg$c103 = "<>",
482 | peg$c104 = peg$literalExpectation("<>", false),
483 | peg$c105 = "between",
484 | peg$c106 = peg$literalExpectation("BETWEEN", true),
485 | peg$c107 = "not between",
486 | peg$c108 = peg$literalExpectation("NOT BETWEEN", true),
487 | peg$c109 = "not like",
488 | peg$c110 = peg$literalExpectation("NOT LIKE", true),
489 | peg$c111 = "like",
490 | peg$c112 = peg$literalExpectation("LIKE", true),
491 | peg$c113 = "is null",
492 | peg$c114 = peg$literalExpectation("IS NULL", true),
493 | peg$c115 = "is not null",
494 | peg$c116 = peg$literalExpectation("IS NOT NULL", true),
495 | peg$c117 = "is true",
496 | peg$c118 = peg$literalExpectation("IS TRUE", true),
497 | peg$c119 = "is not true",
498 | peg$c120 = peg$literalExpectation("IS NOT TRUE", true),
499 | peg$c121 = "is false",
500 | peg$c122 = peg$literalExpectation("IS FALSE", true),
501 | peg$c123 = "in",
502 | peg$c124 = peg$literalExpectation("IN", true),
503 | peg$c125 = "not in",
504 | peg$c126 = peg$literalExpectation("NOT IN", true),
505 | peg$c127 = "or",
506 | peg$c128 = peg$literalExpectation("OR", true),
507 | peg$c129 = "and",
508 | peg$c130 = peg$literalExpectation("AND", true),
509 | peg$c131 = "not",
510 | peg$c132 = peg$literalExpectation("NOT", true),
511 | peg$c133 = function(spacing, operator) {
512 | return {
513 | type: 'operator',
514 | operator: operator,
515 | spacing: spacing
516 | }
517 | },
518 | peg$c134 = /^[ \t\n\r(),;]/,
519 | peg$c135 = peg$classExpectation([" ", "\t", "\n", "\r", "(", ")", ",", ";"], false, false),
520 | peg$c136 = function(spacing, schema, table, alias) {
521 | return {
522 | type: 'table',
523 | schema: schema ? schema.join("").replace(/[,.]/g, ""): null,
524 | alias: alias,
525 | table: table,
526 | spacing: spacing
527 | }
528 | },
529 | peg$c137 = function() {
530 | return {
531 | type: "star",
532 | }
533 | },
534 | peg$c138 = "(",
535 | peg$c139 = peg$literalExpectation("(", false),
536 | peg$c140 = ")",
537 | peg$c141 = peg$literalExpectation(")", false),
538 | peg$c142 = "\"",
539 | peg$c143 = peg$literalExpectation("\"", false),
540 | peg$c144 = ",",
541 | peg$c145 = peg$literalExpectation(",", false),
542 | peg$c146 = ";",
543 | peg$c147 = peg$literalExpectation(";", false),
544 | peg$c148 = "'",
545 | peg$c149 = peg$literalExpectation("'", false),
546 | peg$c150 = ".",
547 | peg$c151 = peg$literalExpectation(".", false),
548 | peg$c152 = /^[a-z_]/i,
549 | peg$c153 = peg$classExpectation([["a", "z"], "_"], false, true),
550 | peg$c154 = function(part) {
551 | return part.join("")
552 | },
553 |
554 | peg$currPos = 0,
555 | peg$savedPos = 0,
556 | peg$posDetailsCache = [{ line: 1, column: 1 }],
557 | peg$maxFailPos = 0,
558 | peg$maxFailExpected = [],
559 | peg$silentFails = 0,
560 |
561 | peg$result;
562 |
563 | if ("startRule" in options) {
564 | if (!(options.startRule in peg$startRuleFunctions)) {
565 | throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
566 | }
567 |
568 | peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
569 | }
570 |
571 | function text() {
572 | return input.substring(peg$savedPos, peg$currPos);
573 | }
574 |
575 | function location() {
576 | return peg$computeLocation(peg$savedPos, peg$currPos);
577 | }
578 |
579 | function expected(description, location) {
580 | location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos)
581 |
582 | throw peg$buildStructuredError(
583 | [peg$otherExpectation(description)],
584 | input.substring(peg$savedPos, peg$currPos),
585 | location
586 | );
587 | }
588 |
589 | function error(message, location) {
590 | location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos)
591 |
592 | throw peg$buildSimpleError(message, location);
593 | }
594 |
595 | function peg$literalExpectation(text, ignoreCase) {
596 | return { type: "literal", text: text, ignoreCase: ignoreCase };
597 | }
598 |
599 | function peg$classExpectation(parts, inverted, ignoreCase) {
600 | return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase };
601 | }
602 |
603 | function peg$anyExpectation() {
604 | return { type: "any" };
605 | }
606 |
607 | function peg$endExpectation() {
608 | return { type: "end" };
609 | }
610 |
611 | function peg$otherExpectation(description) {
612 | return { type: "other", description: description };
613 | }
614 |
615 | function peg$computePosDetails(pos) {
616 | var details = peg$posDetailsCache[pos], p;
617 |
618 | if (details) {
619 | return details;
620 | } else {
621 | p = pos - 1;
622 | while (!peg$posDetailsCache[p]) {
623 | p--;
624 | }
625 |
626 | details = peg$posDetailsCache[p];
627 | details = {
628 | line: details.line,
629 | column: details.column
630 | };
631 |
632 | while (p < pos) {
633 | if (input.charCodeAt(p) === 10) {
634 | details.line++;
635 | details.column = 1;
636 | } else {
637 | details.column++;
638 | }
639 |
640 | p++;
641 | }
642 |
643 | peg$posDetailsCache[pos] = details;
644 | return details;
645 | }
646 | }
647 |
648 | function peg$computeLocation(startPos, endPos) {
649 | var startPosDetails = peg$computePosDetails(startPos),
650 | endPosDetails = peg$computePosDetails(endPos);
651 |
652 | return {
653 | start: {
654 | offset: startPos,
655 | line: startPosDetails.line,
656 | column: startPosDetails.column
657 | },
658 | end: {
659 | offset: endPos,
660 | line: endPosDetails.line,
661 | column: endPosDetails.column
662 | }
663 | };
664 | }
665 |
666 | function peg$fail(expected) {
667 | if (peg$currPos < peg$maxFailPos) { return; }
668 |
669 | if (peg$currPos > peg$maxFailPos) {
670 | peg$maxFailPos = peg$currPos;
671 | peg$maxFailExpected = [];
672 | }
673 |
674 | peg$maxFailExpected.push(expected);
675 | }
676 |
677 | function peg$buildSimpleError(message, location) {
678 | return new peg$SyntaxError(message, null, null, location);
679 | }
680 |
681 | function peg$buildStructuredError(expected, found, location) {
682 | return new peg$SyntaxError(
683 | peg$SyntaxError.buildMessage(expected, found),
684 | expected,
685 | found,
686 | location
687 | );
688 | }
689 |
690 | function peg$parseStart() {
691 | var s0, s1, s2, s3;
692 |
693 | s0 = peg$parseQuery();
694 | if (s0 === peg$FAILED) {
695 | s0 = peg$currPos;
696 | s1 = peg$parse_();
697 | if (s1 !== peg$FAILED) {
698 | s2 = peg$parseExpression();
699 | if (s2 !== peg$FAILED) {
700 | s3 = peg$parse_();
701 | if (s3 !== peg$FAILED) {
702 | peg$savedPos = s0;
703 | s1 = peg$c0(s1, s2, s3);
704 | s0 = s1;
705 | } else {
706 | peg$currPos = s0;
707 | s0 = peg$FAILED;
708 | }
709 | } else {
710 | peg$currPos = s0;
711 | s0 = peg$FAILED;
712 | }
713 | } else {
714 | peg$currPos = s0;
715 | s0 = peg$FAILED;
716 | }
717 | }
718 |
719 | return s0;
720 | }
721 |
722 | function peg$parseQuery() {
723 | var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11;
724 |
725 | s0 = peg$currPos;
726 | s1 = peg$parse_();
727 | if (s1 !== peg$FAILED) {
728 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c1) {
729 | s2 = input.substr(peg$currPos, 6);
730 | peg$currPos += 6;
731 | } else {
732 | s2 = peg$FAILED;
733 | if (peg$silentFails === 0) { peg$fail(peg$c2); }
734 | }
735 | if (s2 !== peg$FAILED) {
736 | s3 = peg$parseSelectParts();
737 | if (s3 !== peg$FAILED) {
738 | s4 = peg$parseFrom();
739 | if (s4 === peg$FAILED) {
740 | s4 = null;
741 | }
742 | if (s4 !== peg$FAILED) {
743 | s5 = peg$parseWhere();
744 | if (s5 === peg$FAILED) {
745 | s5 = null;
746 | }
747 | if (s5 !== peg$FAILED) {
748 | s6 = peg$parseGroupBy();
749 | if (s6 === peg$FAILED) {
750 | s6 = null;
751 | }
752 | if (s6 !== peg$FAILED) {
753 | s7 = peg$parseHaving();
754 | if (s7 === peg$FAILED) {
755 | s7 = null;
756 | }
757 | if (s7 !== peg$FAILED) {
758 | s8 = peg$parseOrderBy();
759 | if (s8 === peg$FAILED) {
760 | s8 = null;
761 | }
762 | if (s8 !== peg$FAILED) {
763 | s9 = peg$parseLimit();
764 | if (s9 === peg$FAILED) {
765 | s9 = null;
766 | }
767 | if (s9 !== peg$FAILED) {
768 | s10 = peg$parseUnionAll();
769 | if (s10 === peg$FAILED) {
770 | s10 = null;
771 | }
772 | if (s10 !== peg$FAILED) {
773 | s11 = peg$parse_();
774 | if (s11 !== peg$FAILED) {
775 | peg$savedPos = s0;
776 | s1 = peg$c3(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11);
777 | s0 = s1;
778 | } else {
779 | peg$currPos = s0;
780 | s0 = peg$FAILED;
781 | }
782 | } else {
783 | peg$currPos = s0;
784 | s0 = peg$FAILED;
785 | }
786 | } else {
787 | peg$currPos = s0;
788 | s0 = peg$FAILED;
789 | }
790 | } else {
791 | peg$currPos = s0;
792 | s0 = peg$FAILED;
793 | }
794 | } else {
795 | peg$currPos = s0;
796 | s0 = peg$FAILED;
797 | }
798 | } else {
799 | peg$currPos = s0;
800 | s0 = peg$FAILED;
801 | }
802 | } else {
803 | peg$currPos = s0;
804 | s0 = peg$FAILED;
805 | }
806 | } else {
807 | peg$currPos = s0;
808 | s0 = peg$FAILED;
809 | }
810 | } else {
811 | peg$currPos = s0;
812 | s0 = peg$FAILED;
813 | }
814 | } else {
815 | peg$currPos = s0;
816 | s0 = peg$FAILED;
817 | }
818 | } else {
819 | peg$currPos = s0;
820 | s0 = peg$FAILED;
821 | }
822 |
823 | return s0;
824 | }
825 |
826 | function peg$parseSelectParts() {
827 | var s0, s1, s2;
828 |
829 | s0 = peg$currPos;
830 | s1 = [];
831 | s2 = peg$parseSelectPart();
832 | if (s2 !== peg$FAILED) {
833 | while (s2 !== peg$FAILED) {
834 | s1.push(s2);
835 | s2 = peg$parseSelectPart();
836 | }
837 | } else {
838 | s1 = peg$FAILED;
839 | }
840 | if (s1 !== peg$FAILED) {
841 | peg$savedPos = s0;
842 | s1 = peg$c4(s1);
843 | }
844 | s0 = s1;
845 |
846 | return s0;
847 | }
848 |
849 | function peg$parseSelectPart() {
850 | var s0, s1, s2, s3, s4;
851 |
852 | s0 = peg$currPos;
853 | s1 = peg$parse_();
854 | if (s1 !== peg$FAILED) {
855 | s2 = peg$parseDistinct();
856 | if (s2 === peg$FAILED) {
857 | s2 = null;
858 | }
859 | if (s2 !== peg$FAILED) {
860 | s3 = peg$parseVariable();
861 | if (s3 === peg$FAILED) {
862 | s3 = peg$parseFunction();
863 | if (s3 === peg$FAILED) {
864 | s3 = peg$parseCase();
865 | if (s3 === peg$FAILED) {
866 | s3 = peg$parseConstant();
867 | if (s3 === peg$FAILED) {
868 | s3 = peg$parsestar();
869 | }
870 | }
871 | }
872 | }
873 | if (s3 !== peg$FAILED) {
874 | s4 = peg$parseAlias();
875 | if (s4 === peg$FAILED) {
876 | s4 = null;
877 | }
878 | if (s4 !== peg$FAILED) {
879 | peg$savedPos = s0;
880 | s1 = peg$c5(s1, s2, s3, s4);
881 | s0 = s1;
882 | } else {
883 | peg$currPos = s0;
884 | s0 = peg$FAILED;
885 | }
886 | } else {
887 | peg$currPos = s0;
888 | s0 = peg$FAILED;
889 | }
890 | } else {
891 | peg$currPos = s0;
892 | s0 = peg$FAILED;
893 | }
894 | } else {
895 | peg$currPos = s0;
896 | s0 = peg$FAILED;
897 | }
898 |
899 | return s0;
900 | }
901 |
902 | function peg$parseFrom() {
903 | var s0, s1, s2, s3;
904 |
905 | s0 = peg$currPos;
906 | s1 = peg$parse_();
907 | if (s1 !== peg$FAILED) {
908 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c6) {
909 | s2 = input.substr(peg$currPos, 4);
910 | peg$currPos += 4;
911 | } else {
912 | s2 = peg$FAILED;
913 | if (peg$silentFails === 0) { peg$fail(peg$c7); }
914 | }
915 | if (s2 !== peg$FAILED) {
916 | s3 = peg$parseQuery();
917 | if (s3 === peg$FAILED) {
918 | s3 = peg$parseTable();
919 | }
920 | if (s3 !== peg$FAILED) {
921 | peg$savedPos = s0;
922 | s1 = peg$c8(s1, s2, s3);
923 | s0 = s1;
924 | } else {
925 | peg$currPos = s0;
926 | s0 = peg$FAILED;
927 | }
928 | } else {
929 | peg$currPos = s0;
930 | s0 = peg$FAILED;
931 | }
932 | } else {
933 | peg$currPos = s0;
934 | s0 = peg$FAILED;
935 | }
936 |
937 | return s0;
938 | }
939 |
940 | function peg$parseWhere() {
941 | var s0, s1, s2, s3;
942 |
943 | s0 = peg$currPos;
944 | s1 = peg$parse_();
945 | if (s1 !== peg$FAILED) {
946 | if (input.substr(peg$currPos, 5).toLowerCase() === peg$c9) {
947 | s2 = input.substr(peg$currPos, 5);
948 | peg$currPos += 5;
949 | } else {
950 | s2 = peg$FAILED;
951 | if (peg$silentFails === 0) { peg$fail(peg$c10); }
952 | }
953 | if (s2 !== peg$FAILED) {
954 | s3 = peg$parseExpression();
955 | if (s3 !== peg$FAILED) {
956 | peg$savedPos = s0;
957 | s1 = peg$c11(s1, s2, s3);
958 | s0 = s1;
959 | } else {
960 | peg$currPos = s0;
961 | s0 = peg$FAILED;
962 | }
963 | } else {
964 | peg$currPos = s0;
965 | s0 = peg$FAILED;
966 | }
967 | } else {
968 | peg$currPos = s0;
969 | s0 = peg$FAILED;
970 | }
971 |
972 | return s0;
973 | }
974 |
975 | function peg$parseGroupBy() {
976 | var s0, s1, s2, s3, s4;
977 |
978 | s0 = peg$currPos;
979 | s1 = peg$parse_();
980 | if (s1 !== peg$FAILED) {
981 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c12) {
982 | s2 = input.substr(peg$currPos, 8);
983 | peg$currPos += 8;
984 | } else {
985 | s2 = peg$FAILED;
986 | if (peg$silentFails === 0) { peg$fail(peg$c13); }
987 | }
988 | if (s2 !== peg$FAILED) {
989 | s3 = [];
990 | s4 = peg$parseGroupByPart();
991 | if (s4 !== peg$FAILED) {
992 | while (s4 !== peg$FAILED) {
993 | s3.push(s4);
994 | s4 = peg$parseGroupByPart();
995 | }
996 | } else {
997 | s3 = peg$FAILED;
998 | }
999 | if (s3 !== peg$FAILED) {
1000 | peg$savedPos = s0;
1001 | s1 = peg$c14(s1, s2, s3);
1002 | s0 = s1;
1003 | } else {
1004 | peg$currPos = s0;
1005 | s0 = peg$FAILED;
1006 | }
1007 | } else {
1008 | peg$currPos = s0;
1009 | s0 = peg$FAILED;
1010 | }
1011 | } else {
1012 | peg$currPos = s0;
1013 | s0 = peg$FAILED;
1014 | }
1015 |
1016 | return s0;
1017 | }
1018 |
1019 | function peg$parseGroupByPart() {
1020 | var s0, s1, s2;
1021 |
1022 | s0 = peg$currPos;
1023 | s1 = peg$currPos;
1024 | peg$silentFails++;
1025 | s2 = peg$parseReserved();
1026 | peg$silentFails--;
1027 | if (s2 === peg$FAILED) {
1028 | s1 = void 0;
1029 | } else {
1030 | peg$currPos = s1;
1031 | s1 = peg$FAILED;
1032 | }
1033 | if (s1 !== peg$FAILED) {
1034 | s2 = peg$parseInteger();
1035 | if (s2 === peg$FAILED) {
1036 | s2 = peg$parseVariable();
1037 | if (s2 === peg$FAILED) {
1038 | s2 = peg$parseConstant();
1039 | }
1040 | }
1041 | if (s2 !== peg$FAILED) {
1042 | peg$savedPos = s0;
1043 | s1 = peg$c15(s2);
1044 | s0 = s1;
1045 | } else {
1046 | peg$currPos = s0;
1047 | s0 = peg$FAILED;
1048 | }
1049 | } else {
1050 | peg$currPos = s0;
1051 | s0 = peg$FAILED;
1052 | }
1053 |
1054 | return s0;
1055 | }
1056 |
1057 | function peg$parseHaving() {
1058 | var s0, s1, s2, s3;
1059 |
1060 | s0 = peg$currPos;
1061 | s1 = peg$parse_();
1062 | if (s1 !== peg$FAILED) {
1063 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c16) {
1064 | s2 = input.substr(peg$currPos, 6);
1065 | peg$currPos += 6;
1066 | } else {
1067 | s2 = peg$FAILED;
1068 | if (peg$silentFails === 0) { peg$fail(peg$c17); }
1069 | }
1070 | if (s2 !== peg$FAILED) {
1071 | s3 = peg$parseExpression();
1072 | if (s3 === peg$FAILED) {
1073 | s3 = peg$parseBinaryExpression();
1074 | }
1075 | if (s3 !== peg$FAILED) {
1076 | peg$savedPos = s0;
1077 | s1 = peg$c18(s1, s2, s3);
1078 | s0 = s1;
1079 | } else {
1080 | peg$currPos = s0;
1081 | s0 = peg$FAILED;
1082 | }
1083 | } else {
1084 | peg$currPos = s0;
1085 | s0 = peg$FAILED;
1086 | }
1087 | } else {
1088 | peg$currPos = s0;
1089 | s0 = peg$FAILED;
1090 | }
1091 |
1092 | return s0;
1093 | }
1094 |
1095 | function peg$parseOrderBy() {
1096 | var s0, s1, s2, s3, s4;
1097 |
1098 | s0 = peg$currPos;
1099 | s1 = peg$parse_();
1100 | if (s1 !== peg$FAILED) {
1101 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c19) {
1102 | s2 = input.substr(peg$currPos, 8);
1103 | peg$currPos += 8;
1104 | } else {
1105 | s2 = peg$FAILED;
1106 | if (peg$silentFails === 0) { peg$fail(peg$c20); }
1107 | }
1108 | if (s2 !== peg$FAILED) {
1109 | s3 = [];
1110 | s4 = peg$parseOrderByPart();
1111 | if (s4 !== peg$FAILED) {
1112 | while (s4 !== peg$FAILED) {
1113 | s3.push(s4);
1114 | s4 = peg$parseOrderByPart();
1115 | }
1116 | } else {
1117 | s3 = peg$FAILED;
1118 | }
1119 | if (s3 !== peg$FAILED) {
1120 | peg$savedPos = s0;
1121 | s1 = peg$c21(s1, s2, s3);
1122 | s0 = s1;
1123 | } else {
1124 | peg$currPos = s0;
1125 | s0 = peg$FAILED;
1126 | }
1127 | } else {
1128 | peg$currPos = s0;
1129 | s0 = peg$FAILED;
1130 | }
1131 | } else {
1132 | peg$currPos = s0;
1133 | s0 = peg$FAILED;
1134 | }
1135 |
1136 | return s0;
1137 | }
1138 |
1139 | function peg$parseOrderByPart() {
1140 | var s0, s1, s2, s3, s4;
1141 |
1142 | s0 = peg$currPos;
1143 | s1 = peg$parse_();
1144 | if (s1 !== peg$FAILED) {
1145 | s2 = peg$currPos;
1146 | peg$silentFails++;
1147 | if (input.substr(peg$currPos, 5).toLowerCase() === peg$c22) {
1148 | s3 = input.substr(peg$currPos, 5);
1149 | peg$currPos += 5;
1150 | } else {
1151 | s3 = peg$FAILED;
1152 | if (peg$silentFails === 0) { peg$fail(peg$c23); }
1153 | }
1154 | peg$silentFails--;
1155 | if (s3 === peg$FAILED) {
1156 | s2 = void 0;
1157 | } else {
1158 | peg$currPos = s2;
1159 | s2 = peg$FAILED;
1160 | }
1161 | if (s2 !== peg$FAILED) {
1162 | s3 = [];
1163 | s4 = peg$parseExprPart();
1164 | if (s4 !== peg$FAILED) {
1165 | while (s4 !== peg$FAILED) {
1166 | s3.push(s4);
1167 | s4 = peg$parseExprPart();
1168 | }
1169 | } else {
1170 | s3 = peg$FAILED;
1171 | }
1172 | if (s3 !== peg$FAILED) {
1173 | s4 = peg$parseDirection();
1174 | if (s4 === peg$FAILED) {
1175 | s4 = null;
1176 | }
1177 | if (s4 !== peg$FAILED) {
1178 | peg$savedPos = s0;
1179 | s1 = peg$c24(s1, s3, s4);
1180 | s0 = s1;
1181 | } else {
1182 | peg$currPos = s0;
1183 | s0 = peg$FAILED;
1184 | }
1185 | } else {
1186 | peg$currPos = s0;
1187 | s0 = peg$FAILED;
1188 | }
1189 | } else {
1190 | peg$currPos = s0;
1191 | s0 = peg$FAILED;
1192 | }
1193 | } else {
1194 | peg$currPos = s0;
1195 | s0 = peg$FAILED;
1196 | }
1197 |
1198 | return s0;
1199 | }
1200 |
1201 | function peg$parseDirection() {
1202 | var s0, s1, s2;
1203 |
1204 | s0 = peg$currPos;
1205 | s1 = peg$parse_();
1206 | if (s1 !== peg$FAILED) {
1207 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c25) {
1208 | s2 = input.substr(peg$currPos, 4);
1209 | peg$currPos += 4;
1210 | } else {
1211 | s2 = peg$FAILED;
1212 | if (peg$silentFails === 0) { peg$fail(peg$c26); }
1213 | }
1214 | if (s2 === peg$FAILED) {
1215 | if (input.substr(peg$currPos, 3).toLowerCase() === peg$c27) {
1216 | s2 = input.substr(peg$currPos, 3);
1217 | peg$currPos += 3;
1218 | } else {
1219 | s2 = peg$FAILED;
1220 | if (peg$silentFails === 0) { peg$fail(peg$c28); }
1221 | }
1222 | }
1223 | if (s2 !== peg$FAILED) {
1224 | peg$savedPos = s0;
1225 | s1 = peg$c29(s1, s2);
1226 | s0 = s1;
1227 | } else {
1228 | peg$currPos = s0;
1229 | s0 = peg$FAILED;
1230 | }
1231 | } else {
1232 | peg$currPos = s0;
1233 | s0 = peg$FAILED;
1234 | }
1235 |
1236 | return s0;
1237 | }
1238 |
1239 | function peg$parseExprPart() {
1240 | var s0, s1, s2, s3;
1241 |
1242 | s0 = peg$currPos;
1243 | s1 = peg$parse_();
1244 | if (s1 !== peg$FAILED) {
1245 | s2 = peg$currPos;
1246 | peg$silentFails++;
1247 | s3 = peg$parseDirection();
1248 | peg$silentFails--;
1249 | if (s3 === peg$FAILED) {
1250 | s2 = void 0;
1251 | } else {
1252 | peg$currPos = s2;
1253 | s2 = peg$FAILED;
1254 | }
1255 | if (s2 !== peg$FAILED) {
1256 | s3 = peg$parseFunction();
1257 | if (s3 === peg$FAILED) {
1258 | s3 = peg$parseVariable();
1259 | if (s3 === peg$FAILED) {
1260 | s3 = peg$parseConstant();
1261 | }
1262 | }
1263 | if (s3 !== peg$FAILED) {
1264 | peg$savedPos = s0;
1265 | s1 = peg$c30(s1, s3);
1266 | s0 = s1;
1267 | } else {
1268 | peg$currPos = s0;
1269 | s0 = peg$FAILED;
1270 | }
1271 | } else {
1272 | peg$currPos = s0;
1273 | s0 = peg$FAILED;
1274 | }
1275 | } else {
1276 | peg$currPos = s0;
1277 | s0 = peg$FAILED;
1278 | }
1279 |
1280 | return s0;
1281 | }
1282 |
1283 | function peg$parseLimit() {
1284 | var s0, s1, s2, s3;
1285 |
1286 | s0 = peg$currPos;
1287 | s1 = peg$parse_();
1288 | if (s1 !== peg$FAILED) {
1289 | if (input.substr(peg$currPos, 5).toLowerCase() === peg$c22) {
1290 | s2 = input.substr(peg$currPos, 5);
1291 | peg$currPos += 5;
1292 | } else {
1293 | s2 = peg$FAILED;
1294 | if (peg$silentFails === 0) { peg$fail(peg$c23); }
1295 | }
1296 | if (s2 !== peg$FAILED) {
1297 | s3 = peg$parseInteger();
1298 | if (s3 !== peg$FAILED) {
1299 | peg$savedPos = s0;
1300 | s1 = peg$c31(s1, s2, s3);
1301 | s0 = s1;
1302 | } else {
1303 | peg$currPos = s0;
1304 | s0 = peg$FAILED;
1305 | }
1306 | } else {
1307 | peg$currPos = s0;
1308 | s0 = peg$FAILED;
1309 | }
1310 | } else {
1311 | peg$currPos = s0;
1312 | s0 = peg$FAILED;
1313 | }
1314 |
1315 | return s0;
1316 | }
1317 |
1318 | function peg$parseUnionAll() {
1319 | var s0, s1, s2, s3;
1320 |
1321 | s0 = peg$currPos;
1322 | s1 = peg$parse_();
1323 | if (s1 !== peg$FAILED) {
1324 | if (input.substr(peg$currPos, 9).toLowerCase() === peg$c32) {
1325 | s2 = input.substr(peg$currPos, 9);
1326 | peg$currPos += 9;
1327 | } else {
1328 | s2 = peg$FAILED;
1329 | if (peg$silentFails === 0) { peg$fail(peg$c33); }
1330 | }
1331 | if (s2 !== peg$FAILED) {
1332 | s3 = peg$parseQuery();
1333 | if (s3 !== peg$FAILED) {
1334 | peg$savedPos = s0;
1335 | s1 = peg$c34(s1, s2, s3);
1336 | s0 = s1;
1337 | } else {
1338 | peg$currPos = s0;
1339 | s0 = peg$FAILED;
1340 | }
1341 | } else {
1342 | peg$currPos = s0;
1343 | s0 = peg$FAILED;
1344 | }
1345 | } else {
1346 | peg$currPos = s0;
1347 | s0 = peg$FAILED;
1348 | }
1349 |
1350 | return s0;
1351 | }
1352 |
1353 | function peg$parseCase() {
1354 | var s0, s1, s2, s3, s4, s5, s6;
1355 |
1356 | s0 = peg$currPos;
1357 | s1 = peg$parse_();
1358 | if (s1 !== peg$FAILED) {
1359 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c35) {
1360 | s2 = input.substr(peg$currPos, 4);
1361 | peg$currPos += 4;
1362 | } else {
1363 | s2 = peg$FAILED;
1364 | if (peg$silentFails === 0) { peg$fail(peg$c36); }
1365 | }
1366 | if (s2 !== peg$FAILED) {
1367 | s3 = peg$parseCaseValue();
1368 | if (s3 === peg$FAILED) {
1369 | s3 = null;
1370 | }
1371 | if (s3 !== peg$FAILED) {
1372 | s4 = [];
1373 | s5 = peg$parseWhenClause();
1374 | if (s5 !== peg$FAILED) {
1375 | while (s5 !== peg$FAILED) {
1376 | s4.push(s5);
1377 | s5 = peg$parseWhenClause();
1378 | }
1379 | } else {
1380 | s4 = peg$FAILED;
1381 | }
1382 | if (s4 !== peg$FAILED) {
1383 | s5 = peg$parseElseValue();
1384 | if (s5 !== peg$FAILED) {
1385 | s6 = peg$parseEnd();
1386 | if (s6 === peg$FAILED) {
1387 | s6 = null;
1388 | }
1389 | if (s6 !== peg$FAILED) {
1390 | peg$savedPos = s0;
1391 | s1 = peg$c37(s1, s2, s3, s4, s5, s6);
1392 | s0 = s1;
1393 | } else {
1394 | peg$currPos = s0;
1395 | s0 = peg$FAILED;
1396 | }
1397 | } else {
1398 | peg$currPos = s0;
1399 | s0 = peg$FAILED;
1400 | }
1401 | } else {
1402 | peg$currPos = s0;
1403 | s0 = peg$FAILED;
1404 | }
1405 | } else {
1406 | peg$currPos = s0;
1407 | s0 = peg$FAILED;
1408 | }
1409 | } else {
1410 | peg$currPos = s0;
1411 | s0 = peg$FAILED;
1412 | }
1413 | } else {
1414 | peg$currPos = s0;
1415 | s0 = peg$FAILED;
1416 | }
1417 |
1418 | return s0;
1419 | }
1420 |
1421 | function peg$parseCaseValue() {
1422 | var s0, s1, s2, s3;
1423 |
1424 | s0 = peg$currPos;
1425 | s1 = peg$parse_();
1426 | if (s1 !== peg$FAILED) {
1427 | s2 = peg$currPos;
1428 | peg$silentFails++;
1429 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c38) {
1430 | s3 = input.substr(peg$currPos, 4);
1431 | peg$currPos += 4;
1432 | } else {
1433 | s3 = peg$FAILED;
1434 | if (peg$silentFails === 0) { peg$fail(peg$c39); }
1435 | }
1436 | peg$silentFails--;
1437 | if (s3 === peg$FAILED) {
1438 | s2 = void 0;
1439 | } else {
1440 | peg$currPos = s2;
1441 | s2 = peg$FAILED;
1442 | }
1443 | if (s2 !== peg$FAILED) {
1444 | s3 = peg$parseVariable();
1445 | if (s3 === peg$FAILED) {
1446 | s3 = peg$parseConstant();
1447 | }
1448 | if (s3 !== peg$FAILED) {
1449 | peg$savedPos = s0;
1450 | s1 = peg$c40(s1, s3);
1451 | s0 = s1;
1452 | } else {
1453 | peg$currPos = s0;
1454 | s0 = peg$FAILED;
1455 | }
1456 | } else {
1457 | peg$currPos = s0;
1458 | s0 = peg$FAILED;
1459 | }
1460 | } else {
1461 | peg$currPos = s0;
1462 | s0 = peg$FAILED;
1463 | }
1464 |
1465 | return s0;
1466 | }
1467 |
1468 | function peg$parseElseValue() {
1469 | var s0, s1, s2, s3;
1470 |
1471 | s0 = peg$currPos;
1472 | s1 = peg$parse_();
1473 | if (s1 !== peg$FAILED) {
1474 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c41) {
1475 | s2 = input.substr(peg$currPos, 4);
1476 | peg$currPos += 4;
1477 | } else {
1478 | s2 = peg$FAILED;
1479 | if (peg$silentFails === 0) { peg$fail(peg$c42); }
1480 | }
1481 | if (s2 === peg$FAILED) {
1482 | s2 = null;
1483 | }
1484 | if (s2 !== peg$FAILED) {
1485 | s3 = peg$parseBinaryExpression();
1486 | if (s3 === peg$FAILED) {
1487 | s3 = peg$parseExpression();
1488 | if (s3 === peg$FAILED) {
1489 | s3 = peg$parseInteger();
1490 | if (s3 === peg$FAILED) {
1491 | s3 = peg$parseVariable();
1492 | if (s3 === peg$FAILED) {
1493 | s3 = peg$parseConstant();
1494 | }
1495 | }
1496 | }
1497 | }
1498 | if (s3 === peg$FAILED) {
1499 | s3 = null;
1500 | }
1501 | if (s3 !== peg$FAILED) {
1502 | peg$savedPos = s0;
1503 | s1 = peg$c43(s1, s2, s3);
1504 | s0 = s1;
1505 | } else {
1506 | peg$currPos = s0;
1507 | s0 = peg$FAILED;
1508 | }
1509 | } else {
1510 | peg$currPos = s0;
1511 | s0 = peg$FAILED;
1512 | }
1513 | } else {
1514 | peg$currPos = s0;
1515 | s0 = peg$FAILED;
1516 | }
1517 |
1518 | return s0;
1519 | }
1520 |
1521 | function peg$parseEnd() {
1522 | var s0, s1, s2;
1523 |
1524 | s0 = peg$currPos;
1525 | s1 = peg$parse_();
1526 | if (s1 !== peg$FAILED) {
1527 | if (input.substr(peg$currPos, 3).toLowerCase() === peg$c44) {
1528 | s2 = input.substr(peg$currPos, 3);
1529 | peg$currPos += 3;
1530 | } else {
1531 | s2 = peg$FAILED;
1532 | if (peg$silentFails === 0) { peg$fail(peg$c45); }
1533 | }
1534 | if (s2 !== peg$FAILED) {
1535 | peg$savedPos = s0;
1536 | s1 = peg$c46(s1, s2);
1537 | s0 = s1;
1538 | } else {
1539 | peg$currPos = s0;
1540 | s0 = peg$FAILED;
1541 | }
1542 | } else {
1543 | peg$currPos = s0;
1544 | s0 = peg$FAILED;
1545 | }
1546 |
1547 | return s0;
1548 | }
1549 |
1550 | function peg$parseWhenClause() {
1551 | var s0, s1, s2, s3, s4;
1552 |
1553 | s0 = peg$currPos;
1554 | s1 = peg$parse_();
1555 | if (s1 !== peg$FAILED) {
1556 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c38) {
1557 | s2 = input.substr(peg$currPos, 4);
1558 | peg$currPos += 4;
1559 | } else {
1560 | s2 = peg$FAILED;
1561 | if (peg$silentFails === 0) { peg$fail(peg$c39); }
1562 | }
1563 | if (s2 !== peg$FAILED) {
1564 | s3 = peg$parseBinaryExpression();
1565 | if (s3 === peg$FAILED) {
1566 | s3 = peg$parseExpression();
1567 | if (s3 === peg$FAILED) {
1568 | s3 = peg$parseVariable();
1569 | if (s3 === peg$FAILED) {
1570 | s3 = peg$parseConstant();
1571 | if (s3 === peg$FAILED) {
1572 | s3 = peg$parseInteger();
1573 | }
1574 | }
1575 | }
1576 | }
1577 | if (s3 !== peg$FAILED) {
1578 | s4 = peg$parseThen();
1579 | if (s4 !== peg$FAILED) {
1580 | peg$savedPos = s0;
1581 | s1 = peg$c47(s1, s2, s3, s4);
1582 | s0 = s1;
1583 | } else {
1584 | peg$currPos = s0;
1585 | s0 = peg$FAILED;
1586 | }
1587 | } else {
1588 | peg$currPos = s0;
1589 | s0 = peg$FAILED;
1590 | }
1591 | } else {
1592 | peg$currPos = s0;
1593 | s0 = peg$FAILED;
1594 | }
1595 | } else {
1596 | peg$currPos = s0;
1597 | s0 = peg$FAILED;
1598 | }
1599 |
1600 | return s0;
1601 | }
1602 |
1603 | function peg$parseThen() {
1604 | var s0, s1, s2, s3;
1605 |
1606 | s0 = peg$currPos;
1607 | s1 = peg$parse_();
1608 | if (s1 !== peg$FAILED) {
1609 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c48) {
1610 | s2 = input.substr(peg$currPos, 4);
1611 | peg$currPos += 4;
1612 | } else {
1613 | s2 = peg$FAILED;
1614 | if (peg$silentFails === 0) { peg$fail(peg$c49); }
1615 | }
1616 | if (s2 !== peg$FAILED) {
1617 | s3 = peg$parseInteger();
1618 | if (s3 === peg$FAILED) {
1619 | s3 = peg$parseCase();
1620 | if (s3 === peg$FAILED) {
1621 | s3 = peg$parseBinaryExpression();
1622 | if (s3 === peg$FAILED) {
1623 | s3 = peg$parseExpression();
1624 | if (s3 === peg$FAILED) {
1625 | s3 = peg$parseVariable();
1626 | }
1627 | }
1628 | }
1629 | }
1630 | if (s3 !== peg$FAILED) {
1631 | peg$savedPos = s0;
1632 | s1 = peg$c50(s1, s2, s3);
1633 | s0 = s1;
1634 | } else {
1635 | peg$currPos = s0;
1636 | s0 = peg$FAILED;
1637 | }
1638 | } else {
1639 | peg$currPos = s0;
1640 | s0 = peg$FAILED;
1641 | }
1642 | } else {
1643 | peg$currPos = s0;
1644 | s0 = peg$FAILED;
1645 | }
1646 |
1647 | return s0;
1648 | }
1649 |
1650 | function peg$parseBinaryExpression() {
1651 | var s0, s1, s2, s3, s4;
1652 |
1653 | s0 = peg$currPos;
1654 | s1 = peg$parse_();
1655 | if (s1 !== peg$FAILED) {
1656 | s2 = peg$parseExpression();
1657 | if (s2 === peg$FAILED) {
1658 | s2 = peg$parseFunction();
1659 | if (s2 === peg$FAILED) {
1660 | s2 = peg$parseTimeStamp();
1661 | if (s2 === peg$FAILED) {
1662 | s2 = peg$parseVariable();
1663 | if (s2 === peg$FAILED) {
1664 | s2 = peg$parseConstant();
1665 | if (s2 === peg$FAILED) {
1666 | s2 = peg$parseInteger();
1667 | }
1668 | }
1669 | }
1670 | }
1671 | }
1672 | if (s2 === peg$FAILED) {
1673 | s2 = null;
1674 | }
1675 | if (s2 !== peg$FAILED) {
1676 | s3 = peg$parseBinaryOperator();
1677 | if (s3 !== peg$FAILED) {
1678 | s4 = peg$parseBinaryExpression();
1679 | if (s4 === peg$FAILED) {
1680 | s4 = peg$parseFunction();
1681 | if (s4 === peg$FAILED) {
1682 | s4 = peg$parseTimeStamp();
1683 | if (s4 === peg$FAILED) {
1684 | s4 = peg$parseExpression();
1685 | if (s4 === peg$FAILED) {
1686 | s4 = peg$parseVariable();
1687 | if (s4 === peg$FAILED) {
1688 | s4 = peg$parseConstant();
1689 | if (s4 === peg$FAILED) {
1690 | s4 = peg$parseInteger();
1691 | }
1692 | }
1693 | }
1694 | }
1695 | }
1696 | }
1697 | if (s4 === peg$FAILED) {
1698 | s4 = null;
1699 | }
1700 | if (s4 !== peg$FAILED) {
1701 | peg$savedPos = s0;
1702 | s1 = peg$c51(s1, s2, s3, s4);
1703 | s0 = s1;
1704 | } else {
1705 | peg$currPos = s0;
1706 | s0 = peg$FAILED;
1707 | }
1708 | } else {
1709 | peg$currPos = s0;
1710 | s0 = peg$FAILED;
1711 | }
1712 | } else {
1713 | peg$currPos = s0;
1714 | s0 = peg$FAILED;
1715 | }
1716 | } else {
1717 | peg$currPos = s0;
1718 | s0 = peg$FAILED;
1719 | }
1720 |
1721 | return s0;
1722 | }
1723 |
1724 | function peg$parseExpression() {
1725 | var s0, s1, s2, s3, s4;
1726 |
1727 | s0 = peg$currPos;
1728 | s1 = peg$parse_();
1729 | if (s1 !== peg$FAILED) {
1730 | s2 = peg$parseFunction();
1731 | if (s2 === peg$FAILED) {
1732 | s2 = peg$parseTimeStamp();
1733 | if (s2 === peg$FAILED) {
1734 | s2 = peg$parseVariable();
1735 | if (s2 === peg$FAILED) {
1736 | s2 = peg$parseConstant();
1737 | if (s2 === peg$FAILED) {
1738 | s2 = peg$parseInteger();
1739 | }
1740 | }
1741 | }
1742 | }
1743 | if (s2 !== peg$FAILED) {
1744 | s3 = peg$parseOperator();
1745 | if (s3 === peg$FAILED) {
1746 | s3 = peg$parseBinaryOperator();
1747 | }
1748 | if (s3 !== peg$FAILED) {
1749 | s4 = peg$parseExpression();
1750 | if (s4 === peg$FAILED) {
1751 | s4 = peg$parseFunction();
1752 | if (s4 === peg$FAILED) {
1753 | s4 = peg$parseTimeStamp();
1754 | if (s4 === peg$FAILED) {
1755 | s4 = peg$parseInterval();
1756 | if (s4 === peg$FAILED) {
1757 | s4 = peg$parseVariable();
1758 | if (s4 === peg$FAILED) {
1759 | s4 = peg$parseConstant();
1760 | if (s4 === peg$FAILED) {
1761 | s4 = peg$parseInteger();
1762 | }
1763 | }
1764 | }
1765 | }
1766 | }
1767 | }
1768 | if (s4 !== peg$FAILED) {
1769 | peg$savedPos = s0;
1770 | s1 = peg$c52(s1, s2, s3, s4);
1771 | s0 = s1;
1772 | } else {
1773 | peg$currPos = s0;
1774 | s0 = peg$FAILED;
1775 | }
1776 | } else {
1777 | peg$currPos = s0;
1778 | s0 = peg$FAILED;
1779 | }
1780 | } else {
1781 | peg$currPos = s0;
1782 | s0 = peg$FAILED;
1783 | }
1784 | } else {
1785 | peg$currPos = s0;
1786 | s0 = peg$FAILED;
1787 | }
1788 |
1789 | return s0;
1790 | }
1791 |
1792 | function peg$parseFunction() {
1793 | var s0, s1, s2, s3, s4, s5;
1794 |
1795 | s0 = peg$currPos;
1796 | s1 = peg$parse_();
1797 | if (s1 !== peg$FAILED) {
1798 | s2 = peg$parseFunctions();
1799 | if (s2 !== peg$FAILED) {
1800 | s3 = peg$parseOpenParen();
1801 | if (s3 !== peg$FAILED) {
1802 | s4 = [];
1803 | s5 = peg$parseArgument();
1804 | if (s5 !== peg$FAILED) {
1805 | while (s5 !== peg$FAILED) {
1806 | s4.push(s5);
1807 | s5 = peg$parseArgument();
1808 | }
1809 | } else {
1810 | s4 = peg$FAILED;
1811 | }
1812 | if (s4 !== peg$FAILED) {
1813 | s5 = peg$parseCloseParen();
1814 | if (s5 !== peg$FAILED) {
1815 | peg$savedPos = s0;
1816 | s1 = peg$c53(s1, s2, s4);
1817 | s0 = s1;
1818 | } else {
1819 | peg$currPos = s0;
1820 | s0 = peg$FAILED;
1821 | }
1822 | } else {
1823 | peg$currPos = s0;
1824 | s0 = peg$FAILED;
1825 | }
1826 | } else {
1827 | peg$currPos = s0;
1828 | s0 = peg$FAILED;
1829 | }
1830 | } else {
1831 | peg$currPos = s0;
1832 | s0 = peg$FAILED;
1833 | }
1834 | } else {
1835 | peg$currPos = s0;
1836 | s0 = peg$FAILED;
1837 | }
1838 |
1839 | return s0;
1840 | }
1841 |
1842 | function peg$parseDistinct() {
1843 | var s0, s1, s2;
1844 |
1845 | s0 = peg$currPos;
1846 | s1 = peg$parse_();
1847 | if (s1 !== peg$FAILED) {
1848 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c54) {
1849 | s2 = input.substr(peg$currPos, 8);
1850 | peg$currPos += 8;
1851 | } else {
1852 | s2 = peg$FAILED;
1853 | if (peg$silentFails === 0) { peg$fail(peg$c55); }
1854 | }
1855 | if (s2 !== peg$FAILED) {
1856 | peg$savedPos = s0;
1857 | s1 = peg$c56(s1, s2);
1858 | s0 = s1;
1859 | } else {
1860 | peg$currPos = s0;
1861 | s0 = peg$FAILED;
1862 | }
1863 | } else {
1864 | peg$currPos = s0;
1865 | s0 = peg$FAILED;
1866 | }
1867 |
1868 | return s0;
1869 | }
1870 |
1871 | function peg$parseArgument() {
1872 | var s0, s1, s2;
1873 |
1874 | s0 = peg$currPos;
1875 | s1 = peg$parseDistinct();
1876 | if (s1 === peg$FAILED) {
1877 | s1 = null;
1878 | }
1879 | if (s1 !== peg$FAILED) {
1880 | s2 = peg$parseArgumentValue();
1881 | if (s2 !== peg$FAILED) {
1882 | peg$savedPos = s0;
1883 | s1 = peg$c57(s1, s2);
1884 | s0 = s1;
1885 | } else {
1886 | peg$currPos = s0;
1887 | s0 = peg$FAILED;
1888 | }
1889 | } else {
1890 | peg$currPos = s0;
1891 | s0 = peg$FAILED;
1892 | }
1893 |
1894 | return s0;
1895 | }
1896 |
1897 | function peg$parseArgumentValue() {
1898 | var s0, s1, s2, s3, s4;
1899 |
1900 | s0 = peg$currPos;
1901 | s1 = peg$parse_();
1902 | if (s1 !== peg$FAILED) {
1903 | s2 = peg$currPos;
1904 | peg$silentFails++;
1905 | s3 = peg$parseReserved();
1906 | peg$silentFails--;
1907 | if (s3 === peg$FAILED) {
1908 | s2 = void 0;
1909 | } else {
1910 | peg$currPos = s2;
1911 | s2 = peg$FAILED;
1912 | }
1913 | if (s2 !== peg$FAILED) {
1914 | s3 = peg$parseConstant();
1915 | if (s3 === peg$FAILED) {
1916 | s3 = peg$parseVariable();
1917 | if (s3 === peg$FAILED) {
1918 | s3 = peg$parsestar();
1919 | if (s3 === peg$FAILED) {
1920 | s3 = [];
1921 | if (peg$c58.test(input.charAt(peg$currPos))) {
1922 | s4 = input.charAt(peg$currPos);
1923 | peg$currPos++;
1924 | } else {
1925 | s4 = peg$FAILED;
1926 | if (peg$silentFails === 0) { peg$fail(peg$c59); }
1927 | }
1928 | if (s4 !== peg$FAILED) {
1929 | while (s4 !== peg$FAILED) {
1930 | s3.push(s4);
1931 | if (peg$c58.test(input.charAt(peg$currPos))) {
1932 | s4 = input.charAt(peg$currPos);
1933 | peg$currPos++;
1934 | } else {
1935 | s4 = peg$FAILED;
1936 | if (peg$silentFails === 0) { peg$fail(peg$c59); }
1937 | }
1938 | }
1939 | } else {
1940 | s3 = peg$FAILED;
1941 | }
1942 | }
1943 | }
1944 | }
1945 | if (s3 !== peg$FAILED) {
1946 | peg$savedPos = s0;
1947 | s1 = peg$c60(s1, s3);
1948 | s0 = s1;
1949 | } else {
1950 | peg$currPos = s0;
1951 | s0 = peg$FAILED;
1952 | }
1953 | } else {
1954 | peg$currPos = s0;
1955 | s0 = peg$FAILED;
1956 | }
1957 | } else {
1958 | peg$currPos = s0;
1959 | s0 = peg$FAILED;
1960 | }
1961 |
1962 | return s0;
1963 | }
1964 |
1965 | function peg$parseVariable() {
1966 | var s0, s1, s2, s3, s4;
1967 |
1968 | s0 = peg$currPos;
1969 | s1 = peg$parse_();
1970 | if (s1 !== peg$FAILED) {
1971 | s2 = peg$parseQuoteMark();
1972 | if (s2 === peg$FAILED) {
1973 | s2 = peg$parseApostrophe();
1974 | }
1975 | if (s2 !== peg$FAILED) {
1976 | s3 = [];
1977 | if (peg$c61.test(input.charAt(peg$currPos))) {
1978 | s4 = input.charAt(peg$currPos);
1979 | peg$currPos++;
1980 | } else {
1981 | s4 = peg$FAILED;
1982 | if (peg$silentFails === 0) { peg$fail(peg$c62); }
1983 | }
1984 | if (s4 !== peg$FAILED) {
1985 | while (s4 !== peg$FAILED) {
1986 | s3.push(s4);
1987 | if (peg$c61.test(input.charAt(peg$currPos))) {
1988 | s4 = input.charAt(peg$currPos);
1989 | peg$currPos++;
1990 | } else {
1991 | s4 = peg$FAILED;
1992 | if (peg$silentFails === 0) { peg$fail(peg$c62); }
1993 | }
1994 | }
1995 | } else {
1996 | s3 = peg$FAILED;
1997 | }
1998 | if (s3 !== peg$FAILED) {
1999 | s4 = peg$parseQuoteMark();
2000 | if (s4 === peg$FAILED) {
2001 | s4 = peg$parseApostrophe();
2002 | }
2003 | if (s4 !== peg$FAILED) {
2004 | peg$savedPos = s0;
2005 | s1 = peg$c63(s1, s2, s3);
2006 | s0 = s1;
2007 | } else {
2008 | peg$currPos = s0;
2009 | s0 = peg$FAILED;
2010 | }
2011 | } else {
2012 | peg$currPos = s0;
2013 | s0 = peg$FAILED;
2014 | }
2015 | } else {
2016 | peg$currPos = s0;
2017 | s0 = peg$FAILED;
2018 | }
2019 | } else {
2020 | peg$currPos = s0;
2021 | s0 = peg$FAILED;
2022 | }
2023 |
2024 | return s0;
2025 | }
2026 |
2027 | function peg$parseConstant() {
2028 | var s0, s1, s2, s3, s4;
2029 |
2030 | s0 = peg$currPos;
2031 | s1 = peg$parse_();
2032 | if (s1 !== peg$FAILED) {
2033 | s2 = peg$currPos;
2034 | peg$silentFails++;
2035 | s3 = peg$parseReserved();
2036 | peg$silentFails--;
2037 | if (s3 === peg$FAILED) {
2038 | s2 = void 0;
2039 | } else {
2040 | peg$currPos = s2;
2041 | s2 = peg$FAILED;
2042 | }
2043 | if (s2 !== peg$FAILED) {
2044 | s3 = [];
2045 | if (peg$c64.test(input.charAt(peg$currPos))) {
2046 | s4 = input.charAt(peg$currPos);
2047 | peg$currPos++;
2048 | } else {
2049 | s4 = peg$FAILED;
2050 | if (peg$silentFails === 0) { peg$fail(peg$c65); }
2051 | }
2052 | if (s4 !== peg$FAILED) {
2053 | while (s4 !== peg$FAILED) {
2054 | s3.push(s4);
2055 | if (peg$c64.test(input.charAt(peg$currPos))) {
2056 | s4 = input.charAt(peg$currPos);
2057 | peg$currPos++;
2058 | } else {
2059 | s4 = peg$FAILED;
2060 | if (peg$silentFails === 0) { peg$fail(peg$c65); }
2061 | }
2062 | }
2063 | } else {
2064 | s3 = peg$FAILED;
2065 | }
2066 | if (s3 !== peg$FAILED) {
2067 | peg$savedPos = s0;
2068 | s1 = peg$c66(s1, s3);
2069 | s0 = s1;
2070 | } else {
2071 | peg$currPos = s0;
2072 | s0 = peg$FAILED;
2073 | }
2074 | } else {
2075 | peg$currPos = s0;
2076 | s0 = peg$FAILED;
2077 | }
2078 | } else {
2079 | peg$currPos = s0;
2080 | s0 = peg$FAILED;
2081 | }
2082 |
2083 | return s0;
2084 | }
2085 |
2086 | function peg$parseInteger() {
2087 | var s0, s1, s2, s3;
2088 |
2089 | s0 = peg$currPos;
2090 | s1 = peg$parse_();
2091 | if (s1 !== peg$FAILED) {
2092 | s2 = [];
2093 | if (peg$c67.test(input.charAt(peg$currPos))) {
2094 | s3 = input.charAt(peg$currPos);
2095 | peg$currPos++;
2096 | } else {
2097 | s3 = peg$FAILED;
2098 | if (peg$silentFails === 0) { peg$fail(peg$c68); }
2099 | }
2100 | if (s3 !== peg$FAILED) {
2101 | while (s3 !== peg$FAILED) {
2102 | s2.push(s3);
2103 | if (peg$c67.test(input.charAt(peg$currPos))) {
2104 | s3 = input.charAt(peg$currPos);
2105 | peg$currPos++;
2106 | } else {
2107 | s3 = peg$FAILED;
2108 | if (peg$silentFails === 0) { peg$fail(peg$c68); }
2109 | }
2110 | }
2111 | } else {
2112 | s2 = peg$FAILED;
2113 | }
2114 | if (s2 !== peg$FAILED) {
2115 | peg$savedPos = s0;
2116 | s1 = peg$c69(s1, s2);
2117 | s0 = s1;
2118 | } else {
2119 | peg$currPos = s0;
2120 | s0 = peg$FAILED;
2121 | }
2122 | } else {
2123 | peg$currPos = s0;
2124 | s0 = peg$FAILED;
2125 | }
2126 |
2127 | return s0;
2128 | }
2129 |
2130 | function peg$parseTimeStamp() {
2131 | var s0, s1, s2, s3, s4, s5, s6;
2132 |
2133 | s0 = peg$currPos;
2134 | s1 = peg$parse_();
2135 | if (s1 !== peg$FAILED) {
2136 | if (input.substr(peg$currPos, 9).toLowerCase() === peg$c70) {
2137 | s2 = input.substr(peg$currPos, 9);
2138 | peg$currPos += 9;
2139 | } else {
2140 | s2 = peg$FAILED;
2141 | if (peg$silentFails === 0) { peg$fail(peg$c71); }
2142 | }
2143 | if (s2 !== peg$FAILED) {
2144 | s3 = peg$parse_();
2145 | if (s3 !== peg$FAILED) {
2146 | s4 = peg$parseApostrophe();
2147 | if (s4 !== peg$FAILED) {
2148 | s5 = [];
2149 | if (peg$c72.test(input.charAt(peg$currPos))) {
2150 | s6 = input.charAt(peg$currPos);
2151 | peg$currPos++;
2152 | } else {
2153 | s6 = peg$FAILED;
2154 | if (peg$silentFails === 0) { peg$fail(peg$c73); }
2155 | }
2156 | if (s6 !== peg$FAILED) {
2157 | while (s6 !== peg$FAILED) {
2158 | s5.push(s6);
2159 | if (peg$c72.test(input.charAt(peg$currPos))) {
2160 | s6 = input.charAt(peg$currPos);
2161 | peg$currPos++;
2162 | } else {
2163 | s6 = peg$FAILED;
2164 | if (peg$silentFails === 0) { peg$fail(peg$c73); }
2165 | }
2166 | }
2167 | } else {
2168 | s5 = peg$FAILED;
2169 | }
2170 | if (s5 !== peg$FAILED) {
2171 | s6 = peg$parseApostrophe();
2172 | if (s6 !== peg$FAILED) {
2173 | peg$savedPos = s0;
2174 | s1 = peg$c74(s1, s5);
2175 | s0 = s1;
2176 | } else {
2177 | peg$currPos = s0;
2178 | s0 = peg$FAILED;
2179 | }
2180 | } else {
2181 | peg$currPos = s0;
2182 | s0 = peg$FAILED;
2183 | }
2184 | } else {
2185 | peg$currPos = s0;
2186 | s0 = peg$FAILED;
2187 | }
2188 | } else {
2189 | peg$currPos = s0;
2190 | s0 = peg$FAILED;
2191 | }
2192 | } else {
2193 | peg$currPos = s0;
2194 | s0 = peg$FAILED;
2195 | }
2196 | } else {
2197 | peg$currPos = s0;
2198 | s0 = peg$FAILED;
2199 | }
2200 |
2201 | return s0;
2202 | }
2203 |
2204 | function peg$parseOperator() {
2205 | var s0, s1, s2;
2206 |
2207 | s0 = peg$currPos;
2208 | s1 = peg$parse_();
2209 | if (s1 !== peg$FAILED) {
2210 | if (input.charCodeAt(peg$currPos) === 43) {
2211 | s2 = peg$c75;
2212 | peg$currPos++;
2213 | } else {
2214 | s2 = peg$FAILED;
2215 | if (peg$silentFails === 0) { peg$fail(peg$c76); }
2216 | }
2217 | if (s2 === peg$FAILED) {
2218 | if (input.charCodeAt(peg$currPos) === 45) {
2219 | s2 = peg$c77;
2220 | peg$currPos++;
2221 | } else {
2222 | s2 = peg$FAILED;
2223 | if (peg$silentFails === 0) { peg$fail(peg$c78); }
2224 | }
2225 | if (s2 === peg$FAILED) {
2226 | if (input.charCodeAt(peg$currPos) === 47) {
2227 | s2 = peg$c79;
2228 | peg$currPos++;
2229 | } else {
2230 | s2 = peg$FAILED;
2231 | if (peg$silentFails === 0) { peg$fail(peg$c80); }
2232 | }
2233 | if (s2 === peg$FAILED) {
2234 | if (input.charCodeAt(peg$currPos) === 42) {
2235 | s2 = peg$c81;
2236 | peg$currPos++;
2237 | } else {
2238 | s2 = peg$FAILED;
2239 | if (peg$silentFails === 0) { peg$fail(peg$c82); }
2240 | }
2241 | if (s2 === peg$FAILED) {
2242 | if (input.charCodeAt(peg$currPos) === 61) {
2243 | s2 = peg$c83;
2244 | peg$currPos++;
2245 | } else {
2246 | s2 = peg$FAILED;
2247 | if (peg$silentFails === 0) { peg$fail(peg$c84); }
2248 | }
2249 | }
2250 | }
2251 | }
2252 | }
2253 | if (s2 !== peg$FAILED) {
2254 | peg$savedPos = s0;
2255 | s1 = peg$c85(s1, s2);
2256 | s0 = s1;
2257 | } else {
2258 | peg$currPos = s0;
2259 | s0 = peg$FAILED;
2260 | }
2261 | } else {
2262 | peg$currPos = s0;
2263 | s0 = peg$FAILED;
2264 | }
2265 |
2266 | return s0;
2267 | }
2268 |
2269 | function peg$parseInterval() {
2270 | var s0, s1, s2, s3, s4;
2271 |
2272 | s0 = peg$currPos;
2273 | s1 = peg$parse_();
2274 | if (s1 !== peg$FAILED) {
2275 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c86) {
2276 | s2 = input.substr(peg$currPos, 8);
2277 | peg$currPos += 8;
2278 | } else {
2279 | s2 = peg$FAILED;
2280 | if (peg$silentFails === 0) { peg$fail(peg$c87); }
2281 | }
2282 | if (s2 !== peg$FAILED) {
2283 | s3 = peg$parseApostrophe();
2284 | if (s3 === peg$FAILED) {
2285 | s3 = peg$parseInteger();
2286 | if (s3 === peg$FAILED) {
2287 | s3 = peg$parseApostrophe();
2288 | if (s3 === peg$FAILED) {
2289 | s3 = peg$parseVariable();
2290 | }
2291 | }
2292 | }
2293 | if (s3 !== peg$FAILED) {
2294 | s4 = peg$parseConstant();
2295 | if (s4 !== peg$FAILED) {
2296 | peg$savedPos = s0;
2297 | s1 = peg$c88(s1, s3, s4);
2298 | s0 = s1;
2299 | } else {
2300 | peg$currPos = s0;
2301 | s0 = peg$FAILED;
2302 | }
2303 | } else {
2304 | peg$currPos = s0;
2305 | s0 = peg$FAILED;
2306 | }
2307 | } else {
2308 | peg$currPos = s0;
2309 | s0 = peg$FAILED;
2310 | }
2311 | } else {
2312 | peg$currPos = s0;
2313 | s0 = peg$FAILED;
2314 | }
2315 |
2316 | return s0;
2317 | }
2318 |
2319 | function peg$parseAlias() {
2320 | var s0, s1, s2, s3;
2321 |
2322 | s0 = peg$currPos;
2323 | s1 = peg$parse_();
2324 | if (s1 !== peg$FAILED) {
2325 | if (input.substr(peg$currPos, 2).toLowerCase() === peg$c89) {
2326 | s2 = input.substr(peg$currPos, 2);
2327 | peg$currPos += 2;
2328 | } else {
2329 | s2 = peg$FAILED;
2330 | if (peg$silentFails === 0) { peg$fail(peg$c90); }
2331 | }
2332 | if (s2 !== peg$FAILED) {
2333 | s3 = peg$parseVariable();
2334 | if (s3 === peg$FAILED) {
2335 | s3 = peg$parseConstant();
2336 | if (s3 === peg$FAILED) {
2337 | s3 = peg$parseInteger();
2338 | }
2339 | }
2340 | if (s3 !== peg$FAILED) {
2341 | peg$savedPos = s0;
2342 | s1 = peg$c91(s1, s2, s3);
2343 | s0 = s1;
2344 | } else {
2345 | peg$currPos = s0;
2346 | s0 = peg$FAILED;
2347 | }
2348 | } else {
2349 | peg$currPos = s0;
2350 | s0 = peg$FAILED;
2351 | }
2352 | } else {
2353 | peg$currPos = s0;
2354 | s0 = peg$FAILED;
2355 | }
2356 |
2357 | return s0;
2358 | }
2359 |
2360 | function peg$parseFunctions() {
2361 | var s0, s1;
2362 |
2363 | s0 = peg$currPos;
2364 | s1 = peg$parseIdentifierPart();
2365 | if (s1 !== peg$FAILED) {
2366 | peg$savedPos = s0;
2367 | s1 = peg$c92(s1);
2368 | }
2369 | s0 = s1;
2370 |
2371 | return s0;
2372 | }
2373 |
2374 | function peg$parseBinaryOperator() {
2375 | var s0, s1, s2, s3;
2376 |
2377 | s0 = peg$currPos;
2378 | s1 = peg$parse_();
2379 | if (s1 !== peg$FAILED) {
2380 | s2 = peg$currPos;
2381 | peg$silentFails++;
2382 | s3 = peg$parseParts();
2383 | peg$silentFails--;
2384 | if (s3 === peg$FAILED) {
2385 | s2 = void 0;
2386 | } else {
2387 | peg$currPos = s2;
2388 | s2 = peg$FAILED;
2389 | }
2390 | if (s2 !== peg$FAILED) {
2391 | if (input.substr(peg$currPos, 2) === peg$c93) {
2392 | s3 = peg$c93;
2393 | peg$currPos += 2;
2394 | } else {
2395 | s3 = peg$FAILED;
2396 | if (peg$silentFails === 0) { peg$fail(peg$c94); }
2397 | }
2398 | if (s3 === peg$FAILED) {
2399 | if (input.charCodeAt(peg$currPos) === 62) {
2400 | s3 = peg$c95;
2401 | peg$currPos++;
2402 | } else {
2403 | s3 = peg$FAILED;
2404 | if (peg$silentFails === 0) { peg$fail(peg$c96); }
2405 | }
2406 | if (s3 === peg$FAILED) {
2407 | if (input.substr(peg$currPos, 2) === peg$c97) {
2408 | s3 = peg$c97;
2409 | peg$currPos += 2;
2410 | } else {
2411 | s3 = peg$FAILED;
2412 | if (peg$silentFails === 0) { peg$fail(peg$c98); }
2413 | }
2414 | if (s3 === peg$FAILED) {
2415 | if (input.charCodeAt(peg$currPos) === 61) {
2416 | s3 = peg$c83;
2417 | peg$currPos++;
2418 | } else {
2419 | s3 = peg$FAILED;
2420 | if (peg$silentFails === 0) { peg$fail(peg$c84); }
2421 | }
2422 | if (s3 === peg$FAILED) {
2423 | if (input.substr(peg$currPos, 2) === peg$c99) {
2424 | s3 = peg$c99;
2425 | peg$currPos += 2;
2426 | } else {
2427 | s3 = peg$FAILED;
2428 | if (peg$silentFails === 0) { peg$fail(peg$c100); }
2429 | }
2430 | if (s3 === peg$FAILED) {
2431 | if (input.charCodeAt(peg$currPos) === 60) {
2432 | s3 = peg$c101;
2433 | peg$currPos++;
2434 | } else {
2435 | s3 = peg$FAILED;
2436 | if (peg$silentFails === 0) { peg$fail(peg$c102); }
2437 | }
2438 | if (s3 === peg$FAILED) {
2439 | if (input.substr(peg$currPos, 2) === peg$c103) {
2440 | s3 = peg$c103;
2441 | peg$currPos += 2;
2442 | } else {
2443 | s3 = peg$FAILED;
2444 | if (peg$silentFails === 0) { peg$fail(peg$c104); }
2445 | }
2446 | if (s3 === peg$FAILED) {
2447 | if (input.substr(peg$currPos, 7).toLowerCase() === peg$c105) {
2448 | s3 = input.substr(peg$currPos, 7);
2449 | peg$currPos += 7;
2450 | } else {
2451 | s3 = peg$FAILED;
2452 | if (peg$silentFails === 0) { peg$fail(peg$c106); }
2453 | }
2454 | if (s3 === peg$FAILED) {
2455 | if (input.substr(peg$currPos, 11).toLowerCase() === peg$c107) {
2456 | s3 = input.substr(peg$currPos, 11);
2457 | peg$currPos += 11;
2458 | } else {
2459 | s3 = peg$FAILED;
2460 | if (peg$silentFails === 0) { peg$fail(peg$c108); }
2461 | }
2462 | if (s3 === peg$FAILED) {
2463 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c109) {
2464 | s3 = input.substr(peg$currPos, 8);
2465 | peg$currPos += 8;
2466 | } else {
2467 | s3 = peg$FAILED;
2468 | if (peg$silentFails === 0) { peg$fail(peg$c110); }
2469 | }
2470 | if (s3 === peg$FAILED) {
2471 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c111) {
2472 | s3 = input.substr(peg$currPos, 4);
2473 | peg$currPos += 4;
2474 | } else {
2475 | s3 = peg$FAILED;
2476 | if (peg$silentFails === 0) { peg$fail(peg$c112); }
2477 | }
2478 | if (s3 === peg$FAILED) {
2479 | if (input.substr(peg$currPos, 7).toLowerCase() === peg$c113) {
2480 | s3 = input.substr(peg$currPos, 7);
2481 | peg$currPos += 7;
2482 | } else {
2483 | s3 = peg$FAILED;
2484 | if (peg$silentFails === 0) { peg$fail(peg$c114); }
2485 | }
2486 | if (s3 === peg$FAILED) {
2487 | if (input.substr(peg$currPos, 11).toLowerCase() === peg$c115) {
2488 | s3 = input.substr(peg$currPos, 11);
2489 | peg$currPos += 11;
2490 | } else {
2491 | s3 = peg$FAILED;
2492 | if (peg$silentFails === 0) { peg$fail(peg$c116); }
2493 | }
2494 | if (s3 === peg$FAILED) {
2495 | if (input.substr(peg$currPos, 7).toLowerCase() === peg$c117) {
2496 | s3 = input.substr(peg$currPos, 7);
2497 | peg$currPos += 7;
2498 | } else {
2499 | s3 = peg$FAILED;
2500 | if (peg$silentFails === 0) { peg$fail(peg$c118); }
2501 | }
2502 | if (s3 === peg$FAILED) {
2503 | if (input.substr(peg$currPos, 11).toLowerCase() === peg$c119) {
2504 | s3 = input.substr(peg$currPos, 11);
2505 | peg$currPos += 11;
2506 | } else {
2507 | s3 = peg$FAILED;
2508 | if (peg$silentFails === 0) { peg$fail(peg$c120); }
2509 | }
2510 | if (s3 === peg$FAILED) {
2511 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c121) {
2512 | s3 = input.substr(peg$currPos, 8);
2513 | peg$currPos += 8;
2514 | } else {
2515 | s3 = peg$FAILED;
2516 | if (peg$silentFails === 0) { peg$fail(peg$c122); }
2517 | }
2518 | if (s3 === peg$FAILED) {
2519 | if (input.substr(peg$currPos, 2).toLowerCase() === peg$c123) {
2520 | s3 = input.substr(peg$currPos, 2);
2521 | peg$currPos += 2;
2522 | } else {
2523 | s3 = peg$FAILED;
2524 | if (peg$silentFails === 0) { peg$fail(peg$c124); }
2525 | }
2526 | if (s3 === peg$FAILED) {
2527 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c125) {
2528 | s3 = input.substr(peg$currPos, 6);
2529 | peg$currPos += 6;
2530 | } else {
2531 | s3 = peg$FAILED;
2532 | if (peg$silentFails === 0) { peg$fail(peg$c126); }
2533 | }
2534 | if (s3 === peg$FAILED) {
2535 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c125) {
2536 | s3 = input.substr(peg$currPos, 6);
2537 | peg$currPos += 6;
2538 | } else {
2539 | s3 = peg$FAILED;
2540 | if (peg$silentFails === 0) { peg$fail(peg$c126); }
2541 | }
2542 | if (s3 === peg$FAILED) {
2543 | if (input.substr(peg$currPos, 2).toLowerCase() === peg$c127) {
2544 | s3 = input.substr(peg$currPos, 2);
2545 | peg$currPos += 2;
2546 | } else {
2547 | s3 = peg$FAILED;
2548 | if (peg$silentFails === 0) { peg$fail(peg$c128); }
2549 | }
2550 | if (s3 === peg$FAILED) {
2551 | if (input.substr(peg$currPos, 3).toLowerCase() === peg$c129) {
2552 | s3 = input.substr(peg$currPos, 3);
2553 | peg$currPos += 3;
2554 | } else {
2555 | s3 = peg$FAILED;
2556 | if (peg$silentFails === 0) { peg$fail(peg$c130); }
2557 | }
2558 | if (s3 === peg$FAILED) {
2559 | if (input.substr(peg$currPos, 3).toLowerCase() === peg$c131) {
2560 | s3 = input.substr(peg$currPos, 3);
2561 | peg$currPos += 3;
2562 | } else {
2563 | s3 = peg$FAILED;
2564 | if (peg$silentFails === 0) { peg$fail(peg$c132); }
2565 | }
2566 | }
2567 | }
2568 | }
2569 | }
2570 | }
2571 | }
2572 | }
2573 | }
2574 | }
2575 | }
2576 | }
2577 | }
2578 | }
2579 | }
2580 | }
2581 | }
2582 | }
2583 | }
2584 | }
2585 | }
2586 | }
2587 | if (s3 !== peg$FAILED) {
2588 | peg$savedPos = s0;
2589 | s1 = peg$c133(s1, s3);
2590 | s0 = s1;
2591 | } else {
2592 | peg$currPos = s0;
2593 | s0 = peg$FAILED;
2594 | }
2595 | } else {
2596 | peg$currPos = s0;
2597 | s0 = peg$FAILED;
2598 | }
2599 | } else {
2600 | peg$currPos = s0;
2601 | s0 = peg$FAILED;
2602 | }
2603 |
2604 | return s0;
2605 | }
2606 |
2607 | function peg$parse_() {
2608 | var s0, s1;
2609 |
2610 | s0 = [];
2611 | if (peg$c134.test(input.charAt(peg$currPos))) {
2612 | s1 = input.charAt(peg$currPos);
2613 | peg$currPos++;
2614 | } else {
2615 | s1 = peg$FAILED;
2616 | if (peg$silentFails === 0) { peg$fail(peg$c135); }
2617 | }
2618 | while (s1 !== peg$FAILED) {
2619 | s0.push(s1);
2620 | if (peg$c134.test(input.charAt(peg$currPos))) {
2621 | s1 = input.charAt(peg$currPos);
2622 | peg$currPos++;
2623 | } else {
2624 | s1 = peg$FAILED;
2625 | if (peg$silentFails === 0) { peg$fail(peg$c135); }
2626 | }
2627 | }
2628 |
2629 | return s0;
2630 | }
2631 |
2632 | function peg$parseTable() {
2633 | var s0, s1, s2, s3, s4;
2634 |
2635 | s0 = peg$currPos;
2636 | s1 = peg$parse_();
2637 | if (s1 !== peg$FAILED) {
2638 | s2 = peg$currPos;
2639 | s3 = peg$parseIdentifierPart();
2640 | if (s3 !== peg$FAILED) {
2641 | s4 = peg$parseDot();
2642 | if (s4 !== peg$FAILED) {
2643 | s3 = [s3, s4];
2644 | s2 = s3;
2645 | } else {
2646 | peg$currPos = s2;
2647 | s2 = peg$FAILED;
2648 | }
2649 | } else {
2650 | peg$currPos = s2;
2651 | s2 = peg$FAILED;
2652 | }
2653 | if (s2 === peg$FAILED) {
2654 | s2 = null;
2655 | }
2656 | if (s2 !== peg$FAILED) {
2657 | s3 = peg$parseVariable();
2658 | if (s3 === peg$FAILED) {
2659 | s3 = peg$parseIdentifierPart();
2660 | }
2661 | if (s3 !== peg$FAILED) {
2662 | s4 = peg$parseAlias();
2663 | if (s4 === peg$FAILED) {
2664 | s4 = null;
2665 | }
2666 | if (s4 !== peg$FAILED) {
2667 | peg$savedPos = s0;
2668 | s1 = peg$c136(s1, s2, s3, s4);
2669 | s0 = s1;
2670 | } else {
2671 | peg$currPos = s0;
2672 | s0 = peg$FAILED;
2673 | }
2674 | } else {
2675 | peg$currPos = s0;
2676 | s0 = peg$FAILED;
2677 | }
2678 | } else {
2679 | peg$currPos = s0;
2680 | s0 = peg$FAILED;
2681 | }
2682 | } else {
2683 | peg$currPos = s0;
2684 | s0 = peg$FAILED;
2685 | }
2686 |
2687 | return s0;
2688 | }
2689 |
2690 | function peg$parsestar() {
2691 | var s0, s1;
2692 |
2693 | s0 = peg$currPos;
2694 | if (input.charCodeAt(peg$currPos) === 42) {
2695 | s1 = peg$c81;
2696 | peg$currPos++;
2697 | } else {
2698 | s1 = peg$FAILED;
2699 | if (peg$silentFails === 0) { peg$fail(peg$c82); }
2700 | }
2701 | if (s1 !== peg$FAILED) {
2702 | peg$savedPos = s0;
2703 | s1 = peg$c137();
2704 | }
2705 | s0 = s1;
2706 |
2707 | return s0;
2708 | }
2709 |
2710 | function peg$parseOpenParen() {
2711 | var s0;
2712 |
2713 | if (input.charCodeAt(peg$currPos) === 40) {
2714 | s0 = peg$c138;
2715 | peg$currPos++;
2716 | } else {
2717 | s0 = peg$FAILED;
2718 | if (peg$silentFails === 0) { peg$fail(peg$c139); }
2719 | }
2720 |
2721 | return s0;
2722 | }
2723 |
2724 | function peg$parseCloseParen() {
2725 | var s0;
2726 |
2727 | if (input.charCodeAt(peg$currPos) === 41) {
2728 | s0 = peg$c140;
2729 | peg$currPos++;
2730 | } else {
2731 | s0 = peg$FAILED;
2732 | if (peg$silentFails === 0) { peg$fail(peg$c141); }
2733 | }
2734 |
2735 | return s0;
2736 | }
2737 |
2738 | function peg$parseQuoteMark() {
2739 | var s0;
2740 |
2741 | if (input.charCodeAt(peg$currPos) === 34) {
2742 | s0 = peg$c142;
2743 | peg$currPos++;
2744 | } else {
2745 | s0 = peg$FAILED;
2746 | if (peg$silentFails === 0) { peg$fail(peg$c143); }
2747 | }
2748 |
2749 | return s0;
2750 | }
2751 |
2752 | function peg$parseComma() {
2753 | var s0;
2754 |
2755 | if (input.charCodeAt(peg$currPos) === 44) {
2756 | s0 = peg$c144;
2757 | peg$currPos++;
2758 | } else {
2759 | s0 = peg$FAILED;
2760 | if (peg$silentFails === 0) { peg$fail(peg$c145); }
2761 | }
2762 |
2763 | return s0;
2764 | }
2765 |
2766 | function peg$parseSemiColon() {
2767 | var s0;
2768 |
2769 | if (input.charCodeAt(peg$currPos) === 59) {
2770 | s0 = peg$c146;
2771 | peg$currPos++;
2772 | } else {
2773 | s0 = peg$FAILED;
2774 | if (peg$silentFails === 0) { peg$fail(peg$c147); }
2775 | }
2776 |
2777 | return s0;
2778 | }
2779 |
2780 | function peg$parseApostrophe() {
2781 | var s0;
2782 |
2783 | if (input.charCodeAt(peg$currPos) === 39) {
2784 | s0 = peg$c148;
2785 | peg$currPos++;
2786 | } else {
2787 | s0 = peg$FAILED;
2788 | if (peg$silentFails === 0) { peg$fail(peg$c149); }
2789 | }
2790 |
2791 | return s0;
2792 | }
2793 |
2794 | function peg$parseDot() {
2795 | var s0;
2796 |
2797 | if (input.charCodeAt(peg$currPos) === 46) {
2798 | s0 = peg$c150;
2799 | peg$currPos++;
2800 | } else {
2801 | s0 = peg$FAILED;
2802 | if (peg$silentFails === 0) { peg$fail(peg$c151); }
2803 | }
2804 |
2805 | return s0;
2806 | }
2807 |
2808 | function peg$parseIdentifierPart() {
2809 | var s0, s1, s2;
2810 |
2811 | s0 = peg$currPos;
2812 | s1 = [];
2813 | if (peg$c152.test(input.charAt(peg$currPos))) {
2814 | s2 = input.charAt(peg$currPos);
2815 | peg$currPos++;
2816 | } else {
2817 | s2 = peg$FAILED;
2818 | if (peg$silentFails === 0) { peg$fail(peg$c153); }
2819 | }
2820 | if (s2 !== peg$FAILED) {
2821 | while (s2 !== peg$FAILED) {
2822 | s1.push(s2);
2823 | if (peg$c152.test(input.charAt(peg$currPos))) {
2824 | s2 = input.charAt(peg$currPos);
2825 | peg$currPos++;
2826 | } else {
2827 | s2 = peg$FAILED;
2828 | if (peg$silentFails === 0) { peg$fail(peg$c153); }
2829 | }
2830 | }
2831 | } else {
2832 | s1 = peg$FAILED;
2833 | }
2834 | if (s1 !== peg$FAILED) {
2835 | peg$savedPos = s0;
2836 | s1 = peg$c154(s1);
2837 | }
2838 | s0 = s1;
2839 |
2840 | return s0;
2841 | }
2842 |
2843 | function peg$parseReserved() {
2844 | var s0;
2845 |
2846 | s0 = peg$parseBinaryOperator();
2847 | if (s0 === peg$FAILED) {
2848 | s0 = peg$parseOperator();
2849 | if (s0 === peg$FAILED) {
2850 | s0 = peg$parseFunction();
2851 | if (s0 === peg$FAILED) {
2852 | s0 = peg$parseParts();
2853 | }
2854 | }
2855 | }
2856 |
2857 | return s0;
2858 | }
2859 |
2860 | function peg$parseParts() {
2861 | var s0;
2862 |
2863 | if (input.substr(peg$currPos, 4).toLowerCase() === peg$c6) {
2864 | s0 = input.substr(peg$currPos, 4);
2865 | peg$currPos += 4;
2866 | } else {
2867 | s0 = peg$FAILED;
2868 | if (peg$silentFails === 0) { peg$fail(peg$c7); }
2869 | }
2870 | if (s0 === peg$FAILED) {
2871 | if (input.substr(peg$currPos, 5).toLowerCase() === peg$c9) {
2872 | s0 = input.substr(peg$currPos, 5);
2873 | peg$currPos += 5;
2874 | } else {
2875 | s0 = peg$FAILED;
2876 | if (peg$silentFails === 0) { peg$fail(peg$c10); }
2877 | }
2878 | if (s0 === peg$FAILED) {
2879 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c12) {
2880 | s0 = input.substr(peg$currPos, 8);
2881 | peg$currPos += 8;
2882 | } else {
2883 | s0 = peg$FAILED;
2884 | if (peg$silentFails === 0) { peg$fail(peg$c13); }
2885 | }
2886 | if (s0 === peg$FAILED) {
2887 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c16) {
2888 | s0 = input.substr(peg$currPos, 6);
2889 | peg$currPos += 6;
2890 | } else {
2891 | s0 = peg$FAILED;
2892 | if (peg$silentFails === 0) { peg$fail(peg$c17); }
2893 | }
2894 | if (s0 === peg$FAILED) {
2895 | if (input.substr(peg$currPos, 5).toLowerCase() === peg$c22) {
2896 | s0 = input.substr(peg$currPos, 5);
2897 | peg$currPos += 5;
2898 | } else {
2899 | s0 = peg$FAILED;
2900 | if (peg$silentFails === 0) { peg$fail(peg$c23); }
2901 | }
2902 | if (s0 === peg$FAILED) {
2903 | if (input.substr(peg$currPos, 9).toLowerCase() === peg$c32) {
2904 | s0 = input.substr(peg$currPos, 9);
2905 | peg$currPos += 9;
2906 | } else {
2907 | s0 = peg$FAILED;
2908 | if (peg$silentFails === 0) { peg$fail(peg$c33); }
2909 | }
2910 | if (s0 === peg$FAILED) {
2911 | if (input.substr(peg$currPos, 6).toLowerCase() === peg$c1) {
2912 | s0 = input.substr(peg$currPos, 6);
2913 | peg$currPos += 6;
2914 | } else {
2915 | s0 = peg$FAILED;
2916 | if (peg$silentFails === 0) { peg$fail(peg$c2); }
2917 | }
2918 | if (s0 === peg$FAILED) {
2919 | if (input.substr(peg$currPos, 2).toLowerCase() === peg$c89) {
2920 | s0 = input.substr(peg$currPos, 2);
2921 | peg$currPos += 2;
2922 | } else {
2923 | s0 = peg$FAILED;
2924 | if (peg$silentFails === 0) { peg$fail(peg$c90); }
2925 | }
2926 | if (s0 === peg$FAILED) {
2927 | if (input.substr(peg$currPos, 8).toLowerCase() === peg$c19) {
2928 | s0 = input.substr(peg$currPos, 8);
2929 | peg$currPos += 8;
2930 | } else {
2931 | s0 = peg$FAILED;
2932 | if (peg$silentFails === 0) { peg$fail(peg$c20); }
2933 | }
2934 | }
2935 | }
2936 | }
2937 | }
2938 | }
2939 | }
2940 | }
2941 | }
2942 |
2943 | return s0;
2944 | }
2945 |
2946 |
2947 | var functions = ["COUNT",
2948 | "SUM","MIN", "MAX","AVG","APPROX_COUNT_DISTINCT",
2949 | "APPROX_COUNT_DISTINCT_DS_HLL", "APPROX_COUNT_DISTINCT_DS_THETA",
2950 | "APPROX_QUANTILE", "APPROX_QUANTILE_DS", "APPROX_QUANTILE_FIXED_BUCKETS",
2951 | "BLOOM_FILTER", "ABS", "CEIL", "EXP", "FLOOR", "LN", "LOG10", "POWER", "SQRT",
2952 | "TRUNCATE", "TRUNC", "ROUND", "MOD", "SIN", "COS", "TAN", "COT", "ASIN", "ACOS",
2953 | "ATAN", "ATAN2", "DEGREES", "RADIANS", "CONCAT", "TEXTCAT", "STRING_FORMAT",
2954 | "LENGTH", "CHAR_LENGTH", "CHARARACTER_LENGTH", "STRLEN", "LOOKUP", "LOWER",
2955 | "PARSE_LONG", "POSITION", "REGEXP_EXTRACT", "REPLACE", "STRPOS", "SUBSTRING",
2956 | "RIGHT", "LEFT", "SUBSTR", "TRIM", "BTRIM", "LTRIM", "RTRIM", "UPPER", "REVERSE",
2957 | "REPEAT", "LPAD", "RPAD", "CURRENT_TIMESTAMP", "CURRENT_DATE", "DATE_TRUNC",
2958 | "TIME_FLOOR", "TIME_SHIFT", "TIME_EXTRACT", "TIME_PARSE", "TIME_FORMAT",
2959 | "MILLIS_TO_TIMESTAMP", "TIMESTAMP_TO_MILIS", "EXTRACT", "FLOOR", "CEIL", "TIMESTAMPADD",
2960 | "timestamp_expr", "CAST", "NULLIF", "COALESCE", "BLOOM_FILTER_TEST"];
2961 |
2962 |
2963 | peg$result = peg$startRuleFunction();
2964 |
2965 | if (peg$result !== peg$FAILED && peg$currPos === input.length) {
2966 | return peg$result;
2967 | } else {
2968 | if (peg$result !== peg$FAILED && peg$currPos < input.length) {
2969 | peg$fail(peg$endExpectation());
2970 | }
2971 |
2972 | throw peg$buildStructuredError(
2973 | peg$maxFailExpected,
2974 | peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null,
2975 | peg$maxFailPos < input.length
2976 | ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1)
2977 | : peg$computeLocation(peg$maxFailPos, peg$maxFailPos)
2978 | );
2979 | }
2980 | }
2981 |
2982 | module.exports = {
2983 | SyntaxError: peg$SyntaxError,
2984 | parse: peg$parse
2985 | };
2986 |
--------------------------------------------------------------------------------