Skip to content

Commit 9c44ba8

Browse files
authored
BQL examples (#41)
1 parent b67bb44 commit 9c44ba8

File tree

2 files changed

+218
-31
lines changed

2 files changed

+218
-31
lines changed

docs/ws/api.md

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,115 @@ Bullet-BQL provides users with a friendly SQL-like API to submit queries to the
1212

1313
## Statement Syntax
1414

15-
SELECT select
16-
FROM stream
15+
`query` is one of
16+
17+
innerQuery
18+
outerQuery
19+
20+
where `innerQuery` is
21+
22+
SELECT select FROM stream
23+
( LATERAL VIEW lateralView )?
1724
( WHERE expression )?
18-
( GROUP BY expression ( , expression )* )?
25+
( GROUP BY expressions )?
1926
( HAVING expression )?
2027
( ORDER BY orderBy )?
2128
( WINDOWING window )?
2229
( LIMIT Integer )?
23-
';'?
24-
25-
where `select` is
30+
31+
and `outerQuery` is
2632

33+
SELECT select FROM ( innerQuery )
34+
( LATERAL VIEW lateralView )?
35+
( WHERE expression )?
36+
( GROUP BY expressions )?
37+
( HAVING expression )?
38+
( ORDER BY orderBy )?
39+
( LIMIT Integer )?
40+
41+
where `select` is
42+
2743
DISTINCT? selectItem ( , selectItem )*
28-
44+
2945
and `selectItem` is one of
3046

3147
expression ( AS? identifier )?
48+
tableFunction
3249
*
3350

3451
and `expression` is one of
3552

3653
valueExpression
37-
fieldExpression
54+
fieldExpression ( : fieldType )?
55+
subFieldExpression ( : fieldType )?
56+
subSubFieldExpression ( : fieldType )?
3857
listExpression
3958
expression IS NULL
4059
expression IS NOT NULL
4160
unaryExpression
42-
functionExpression
43-
expression NOT? IN expression
44-
expression RLIKE ANY? expression
45-
expression ( * | / ) expression
61+
functionExpression
62+
expression ( * | / | % ) expression
4663
expression ( + | - ) expression
4764
expression ( < | <= | > | >= ) ( ANY | ALL )? expression
48-
expression ( = | != ) ( ANY | ALL )? expression
65+
expression ( = | != ) ( ANY | ALL )? expression
66+
expression NOT? RLIKE ANY? expression
67+
expression NOT? IN expression
68+
expression NOT? IN ( expressions )
69+
expressioon NOT? BETWEEN ( expression, expression )
4970
expression AND expression
5071
expression XOR expression
5172
expression OR expression
5273
( expression )
5374

54-
where `valueExpression` is one of Null, Boolean, Integer, Long, Float, Double, or String
75+
and `expressions` is
76+
77+
expression ( , expression )*
5578

56-
and `fieldExpression` is one of
79+
where `valueExpression` is one of Null, Boolean, Integer, Long, Float, Double, String, or `NOW` - a keyword that is converted to the current unix time in milliseconds
5780

58-
identifier ( : fieldType )?
59-
identifier [ Integer ] ( : fieldType )?
60-
identifier [ Integer ] . identifier ( : fieldType )?
61-
identifier . identifier ( : fieldType )?
62-
identifier . identifier . identifier ( : fieldType )?
81+
and `fieldExpression` is
6382

83+
identifier
84+
85+
and `subFieldExpression` is one of
86+
87+
fieldExpression [ Integer ]
88+
fieldExpression [ String ]
89+
fieldExpression [ expression ]
90+
fieldExpression . identifier
91+
92+
and `subSubFieldExpression` is one of
93+
94+
subFieldExpression [ String ]
95+
subFieldExpression [ expression ]
96+
subFieldExpression . identifier
97+
6498
`fieldType` is one of
6599

66100
primitiveType
67101
LIST [ primitiveType ]
68102
MAP [ primitiveType ]
69103
LIST [ MAP [ primitiveType ] ]
70104
MAP [ MAP [ primitiveType ] ]
71-
105+
72106
and `primitiveType` is `INTEGER`, `LONG`, `FLOAT`, `DOUBLE`, `BOOLEAN`, or `STRING`
73107

74108
where `listExpression` is one of
75-
109+
76110
[]
77-
[ expression ( , expression )* ]
78-
79-
`unaryExpression` is
111+
[ expressions ]
80112

113+
`unaryExpression` is
114+
81115
( NOT | SIZEOF ) ( expression ) with optional parentheses
116+
( ABS | TRIM | LOWER | UPPER ) ( expression ) with non-optional parentheses
82117

83118
`functionExpression` is one of
84119

85-
( SIZEIS | CONTAINSKEY | CONTAINSVALUE | FILTER ) ( expression, expression )
86-
IF ( expression ( , expression )* ) three arguments
120+
( SIZEIS | CONTAINSKEY | CONTAINSVALUE | FILTER ) ( expression , expression )
121+
UNIXTIMESTAMP ( expressions? ) zero, one, or two arguments
122+
SUBSTRING ( expressions? ) two or three arguments
123+
( IF | BETWEEN ) ( expressions? ) three arguments
87124
aggregateExpression
88125
CAST ( expression AS primitiveType )
89126

@@ -104,23 +141,32 @@ and `inputMode` is one of
104141
MANUAL, Number ( , Number )* defined points
105142

106143

144+
and `tableFunction` is one of
145+
146+
OUTER? EXPLODE ( expression ) AS identifier explode a list to one column
147+
OUTER? EXPLODE ( expression ) AS ( identifier , identifier ) explode a map to a key and a value column
148+
107149
and `stream` is one of
108150

109151
STREAM() default time duration will be set from BQLConfig
110-
STREAM( ( Integer | MAX ), TIME ) time based duration control
152+
STREAM( ( Integer | MAX ), TIME ) time based duration control
111153

112154
`RECORD` will be supported in the future.
113155

114-
and `orderBy` is
156+
and `lateralView` is
157+
158+
tableFunction (LATERAL VIEW tableFunction)*
159+
160+
and `orderBy` is
115161

116162
expression ( ASC | DESC )? ( , expression ( ASC | DESC )? )*
117163

118-
and `window` is one of
164+
and `window` is one of
119165

120166
EVERY ( Integer, ( TIME | RECORD ), include )
121167
TUMBLING ( Integer, ( TIME | RECORD ) )
122168

123-
`include` is one of
169+
`include` is one of
124170

125171
ALL
126172
FIRST, Integer, ( TIME | RECORD )

docs/ws/examples.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,44 @@ WHERE NOT CONTAINSVALUE(data_map, 'btsg8l9b234ha')
156156
LIMIT 1;
157157
```
158158

159+
### Filtering with NOW Keyword
160+
161+
```SQL
162+
SELECT *
163+
FROM STREAM(30000, TIME)
164+
WHERE event_timestamp >= NOW
165+
LIMIT 10;
166+
```
167+
168+
### BETWEEN Filter
169+
170+
This query checks to see if the field ```heart_rate``` is in-between 70 and 100 inclusive and returns all records for which this is true. The ```BETWEEN``` operator can be written in two ways as shown below.
171+
172+
```SQL
173+
SELECT *
174+
FROM STREAM(30000, TIME)
175+
WHERE heart_rate BETWEEN (70, 100)
176+
LIMIT 10;
177+
```
178+
179+
```SQL
180+
SELECT *
181+
FROM STREAM(30000, TIME)
182+
WHERE BETWEEN(heart_rate, 70, 100)
183+
LIMIT 10;
184+
```
185+
186+
### IN Filter
187+
188+
This query checks to see if the field ```color``` is in the given list and returns all records for which is true.
189+
190+
```SQL
191+
SELECT *
192+
FROM STREAM(30000, TIME)
193+
WHERE color IN ('red', 'green', 'blue')
194+
LIMIT 10;
195+
```
196+
159197
### Relational Filter comparing to other fields
160198

161199
Instead of comparing to static, constant values, you may use the extended values notation and set ```kind``` to ```FIELD``` to compare to other fields within the same record. The following query returns the first record for which the ```id``` field is set to the ```uid``` field.
@@ -1004,6 +1042,109 @@ subtract 24 from it, you get the lower bound of the true count.
10041042
Note that this also means the order of the items could be off. If two items had ```Count``` within 24 of each other, it is possible that the higher one *may* actually have had a true count *lower* than
10051043
the second one and possibly be ranked higher. There is no such situation in this result set.
10061044

1045+
### Lateral View Explode
1046+
1047+
```SQL
1048+
SELECT student, score
1049+
FROM STREAM(30000, TIME)
1050+
LATERAL VIEW EXPLODE(test_scores) AS (student, score)
1051+
WHERE score >= 80
1052+
LIMIT 10;
1053+
```
1054+
1055+
This query explodes the map ```test_scores``` to the fields ```student``` and ```score```. This effectively generates a record with a key field and value field for each entry in the exploded map.
1056+
The lateral view means the generated records are appended to the original record, though in this query, only the exploded fields have been selected.
1057+
1058+
```javascript
1059+
{
1060+
"records":[
1061+
{
1062+
"student": "Roger",
1063+
"score": 90
1064+
},
1065+
{
1066+
"student": "Albert",
1067+
"score": 92
1068+
},
1069+
{
1070+
"student": "Emily",
1071+
"score": 90
1072+
},
1073+
{
1074+
"student": "Winston",
1075+
"score": 81
1076+
},
1077+
{
1078+
"student": "Jeff",
1079+
"score": 95
1080+
},
1081+
{
1082+
"student": "Kristen",
1083+
"score": 97
1084+
},
1085+
{
1086+
"student": "Percy",
1087+
"score": 85
1088+
},
1089+
{
1090+
"student": "Tyson",
1091+
"score": 80
1092+
},
1093+
{
1094+
"student": "Jackie",
1095+
"score": 89
1096+
},
1097+
{
1098+
"student": "Alice",
1099+
"score": 100
1100+
}
1101+
],
1102+
"meta": "<EDITED OUT>"
1103+
}
1104+
```
1105+
1106+
### Multiple Lateral View Explodes
1107+
1108+
Multiple lateral view explodes can also be chained in the same query. For instance, using the above example, instead of the map ```test_scores```, there is the list of maps ```tests```.
1109+
This list could be exploded into a field ```test_scores``` which could the be exploded into the fields ```student``` and ```score``` as before.
1110+
1111+
```SQL
1112+
SELECT student, score
1113+
FROM STREAM(30000, TIME)
1114+
LATERAL VIEW EXPLODE(tests) AS test_scores
1115+
LATERAL VIEW EXPLODE(test_scores) AS (student, score)
1116+
WHERE score >= 80
1117+
LIMIT 10;
1118+
```
1119+
1120+
### Outer Query
1121+
1122+
```SQL
1123+
SELECT COUNT(*)
1124+
FROM (
1125+
SELECT browser_name, COUNT(*)
1126+
FROM STREAM(30000, TIME)
1127+
GROUP BY browser_name
1128+
HAVING COUNT(*) > 10
1129+
)
1130+
```
1131+
1132+
This query has an inner query wrapped by an outer query. Note that the inner query selects from ```STREAM``` and is thus the main query while the outer query selects from the inner query.
1133+
Note also that the inner/main query can have a window while the outer query cannot.
1134+
1135+
The query above counts the number of browser names that appear more than 10 times in 30 seconds.
1136+
1137+
```javascript
1138+
{
1139+
"records":[
1140+
{
1141+
"COUNT(*)": 6
1142+
}
1143+
],
1144+
"meta": "<EDITED OUT>"
1145+
}
1146+
```
1147+
10071148
### Window - Tumbling Group-By
10081149

10091150
```SQL

0 commit comments

Comments
 (0)