-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPrimeQueryLanguage.sc
More file actions
369 lines (310 loc) · 10.8 KB
/
DataPrimeQueryLanguage.sc
File metadata and controls
369 lines (310 loc) · 10.8 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
213
214
215
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
365
366
367
368
369
// Parsing interesting bits of Coralogix DataPrime Query Language into an AST
// See https://coralogix.com/docs/dataprime-query-language/
import org.parboiled2.*
import org.parboiled2.Parser.DeliveryScheme.Either
import pprint.*
class QueryParser(val input: ParserInput) extends Parser {
def Parser: Rule1[Query] = rule {
QueryStatement ~ EOI
}
def WS: Rule0 = rule {
quiet(zeroOrMore(anyOf(" \n\r\t\f")))
}
def WS(char: Char): Rule0 = rule {
ch(char) ~ WS
}
def WS(s: String): Rule0 = rule {
str(s) ~ WS
}
def Keyword(s: String): Rule0 = rule {
atomic(ignoreCase(s.toLowerCase)) ~ oneOrMore(anyOf(" \n\r\t\f")).named("Space")
}
def Keyword(s1: String, s2: String): Rule0 = rule {
Keyword(s1) | Keyword(s2)
}
def Keyword(s1: String, s2: String, s3: String): Rule0 = rule {
Keyword(s1) | Keyword(s2) | Keyword(s3)
}
// Grammar
def QueryStatement: Rule1[Query] = rule {
WS ~ Source ~ zeroOrMore(Pipe ~ Operator) ~> Query.apply
}
def Source: Rule1[Query.Source] = rule {
Keyword("source") ~ capture(oneOrMore(CharPredicate.AlphaNum ++ "_-.")) ~ WS ~> Query.Source.fromString
}
def Pipe: Rule0 = rule {
WS ~ "|" ~ WS
}
def KeyPath: Rule1[String] = rule {
capture(oneOrMore(CharPredicate.AlphaNum)) ~ capture(zeroOrMore(CharPredicate.AlphaNum | '.')) ~ WS ~> (_ + _)
}
def Field: Rule1[Query.Field] = rule {
"$" ~ capture(CharPredicate.Alpha) ~ "." ~ KeyPath ~> Query.Field.apply
}
def StringLiteral: Rule1[Query.Literal.StringValue] = rule {
"'" ~ capture(zeroOrMore(noneOf("'"))) ~ "'" ~ WS ~> Query.Literal.StringValue.apply
}
def NumberLiteral: Rule1[Query.Literal.NumberValue] = {
import CharPredicate.{ Digit, Digit19 }
def Digits = rule { oneOrMore(Digit) }
def Integer = rule { optional('-') ~ (Digit19 ~ Digits | Digit) }
def Fraction = rule { "." ~ Digits }
def Exponent = rule { ignoreCase('e') ~ optional(anyOf("+-")) ~ Digits }
rule {
capture(Integer ~ optional(Fraction) ~ optional(Exponent)) ~ WS ~>
(str => Query.Literal.NumberValue(BigDecimal(str)))
}
}
def RegExpLiteral: Rule1[Query.Literal.RegExp] = rule {
"/" ~ capture(zeroOrMore(noneOf("/"))) ~ "/" ~ WS ~> Query.Literal.RegExp.apply
}
def KeyPathLiteral: Rule1[Query.Literal.KeyPath] = rule {
Field ~> Query.Literal.KeyPath.apply
}
def LogicalExpression: Rule1[Query.LogicalExpression] = rule {
oneOrMore(LogicalTerms).separatedBy(WS("||")) ~> {
case Seq(expr) => expr
case exprs => Query.LogicalExpression.Or(exprs)
}
}
def LogicalTerms: Rule1[Query.LogicalExpression] = rule {
oneOrMore(LogicalFactors).separatedBy(WS("&&")) ~> {
case Seq(expr) => expr
case exprs => Query.LogicalExpression.And(exprs)
}
}
def LogicalFactors: Rule1[Query.LogicalExpression] = rule {
WS("(") ~ LogicalExpression ~ WS(")") |
Expression ~ RelationOp ~ Expression ~> Query.LogicalExpression.Relation.apply
}
def RelationOp: Rule1[Query.RelationOp] = rule {
WS("==") ~ push(Query.RelationOp.Eq) |
WS("!=") ~ push(Query.RelationOp.Neq) |
WS("<") ~ push(Query.RelationOp.Lt) |
WS("<=") ~ push(Query.RelationOp.Leq) |
WS(">") ~ push(Query.RelationOp.Gt) |
WS(">=") ~ push(Query.RelationOp.Geq)
}
def Expression: Rule1[Query.Expression] = rule {
Term ~ zeroOrMore(
WS("+") ~ Term ~> (Query.ArithmeticExpression(_, Query.ArithmeticOp.Add, _)) |
WS("-") ~ Term ~> (Query.ArithmeticExpression(_, Query.ArithmeticOp.Sub, _))
)
}
def Term: Rule1[Query.Expression] = rule {
Factor ~ zeroOrMore(
WS("*") ~ Factor ~> (Query.ArithmeticExpression(_, Query.ArithmeticOp.Mul, _)) |
WS("/") ~ Factor ~> (Query.ArithmeticExpression(_, Query.ArithmeticOp.Div, _))
)
}
def Factor: Rule1[Query.Expression] = rule {
WS("(") ~ Expression ~ WS(")") |
StringLiteral ~> Cast.apply |
NumberLiteral ~> Cast.apply |
RegExpLiteral ~> Cast.apply |
KeyPathLiteral ~> Cast.apply
}
def Type: Rule1[Query.ValueType] = rule {
Keyword("boolean", "bool") ~ push(Query.ValueType.Boolean) |
Keyword("string", "str") ~ push(Query.ValueType.String) |
Keyword("number", "num") ~ push(Query.ValueType.Number) |
Keyword("timestamp") ~ push(Query.ValueType.Timestamp)
}
def Cast(expr: Query.Expression): Rule1[Query.Expression] = rule {
optional(':' ~ Type) ~> {
case Some(toType) => Query.Cast(expr, toType)
case None => expr
}
}
def Operator: Rule1[Query.Operator] = rule {
Filter |
Block |
Remove |
OrderBy |
Limit |
Choose
}
def Filter: Rule1[Query.Operator.Filter] = rule {
Keyword("filter", "f", "where") ~ LogicalExpression ~> Query.Operator.Filter.apply
}
def Block: Rule1[Query.Operator.Filter] = rule {
Keyword("block") ~ LogicalExpression ~> Query.LogicalExpression.Not.apply ~> Query.Operator.Filter.apply
}
def Remove: Rule1[Query.Operator.Remove] = rule {
Keyword("remove") ~ oneOrMore(KeyPathLiteral).separatedBy(WS(',')) ~> Query.Operator.Remove.apply
}
def OrderBy: Rule1[Query.Operator.OrderBy] = rule {
Keyword("orderby") ~ oneOrMore(KeyPathLiteral).separatedBy(WS(',')) ~ Sort ~> Query.Operator.OrderBy.apply
}
def Limit: Rule1[Query.Operator.Limit] = rule {
Keyword("limit") ~ capture(oneOrMore(CharPredicate.Digit)) ~ WS ~> (l => Query.Operator.Limit(l.toLong))
}
def Sort: Rule1[Query.Sort] =
rule {
Keyword("asc") ~ push(Query.Sort.Asc) |
Keyword("desc") ~ push(Query.Sort.Desc)
}
def Choose: Rule1[Query.Operator.Choose] = rule {
Keyword("choose") ~ oneOrMore(KeyPathLiteral).separatedBy(WS(',')) ~> Query.Operator.Choose.apply
}
}
case class Query(source: Query.Source, operators: Seq[Query.Operator])
object Query {
enum Source {
case Logs
case Spans
case Custom(enrichment: String)
}
object Source {
def fromString(str: String): Source =
str match {
case "logs" => Source.Logs
case "spans" => Source.Spans
case custom => Source.Custom(custom)
}
}
sealed trait Expression
enum Field {
case Metadata(field: String)
case Labels(field: String)
case UserData(field: String)
}
object Field {
def apply(`type`: String, field: String): Field =
`type` match {
case "m" | "metadata" => Field.Metadata(field)
case "l" | "labels" => Field.Labels(field)
case "d" | "data" => Field.UserData(field)
case _ => Field.UserData(field)
}
}
sealed trait Literal extends Expression
object Literal {
case class StringValue(value: String) extends Literal
case class NumberValue(value: BigDecimal) extends Literal
case class RegExp(value: String) extends Literal
case class KeyPath(field: Field) extends Literal
}
sealed trait LogicalExpression
object LogicalExpression {
case class Or(terms: Seq[LogicalExpression]) extends LogicalExpression
case class And(factors: Seq[LogicalExpression]) extends LogicalExpression
case class Not(expr: LogicalExpression) extends LogicalExpression
case class Relation(left: Expression, op: RelationOp, right: Expression) extends LogicalExpression
}
enum RelationOp {
case Eq, Neq, Lt, Leq, Gt, Geq
}
case class ArithmeticExpression(left: Expression, op: ArithmeticOp, right: Expression) extends Expression
enum ArithmeticOp {
case Add, Sub, Mul, Div
}
case class Cast(expr: Expression, toType: ValueType) extends Expression
enum ValueType {
case Boolean, Number, String, Timestamp
}
enum Sort {
case Asc, Desc
}
sealed trait Operator
object Operator {
case class Filter(condition: LogicalExpression) extends Operator
case class Remove(fields: Seq[Literal.KeyPath]) extends Operator
case class OrderBy(fields: Seq[Literal.KeyPath], sort: Sort) extends Operator
case class Limit(count: Long) extends Operator
case class Choose(fields: Seq[Literal.KeyPath]) extends Operator
}
}
var parser =
QueryParser(
"""|source logs |
| filter $d.result == 'success' && ($d.region != 'eu-west-1' || $d.region == 'us-east-1') |
| orderby $m.severity, $m.timestamp desc |
| limit 42
|""".stripMargin
)
parser.Parser.run() match {
case Left(cause) => println(parser.formatError(cause))
case Right(obtained) =>
import Query.*
import munit.Assertions.*
val expected =
Query(
Source.Logs,
Vector(
Operator.Filter(
LogicalExpression.And(
Vector(
LogicalExpression.Relation(
Literal.KeyPath(Field.UserData("result")),
RelationOp.Eq,
Literal.StringValue("success")
),
LogicalExpression.Or(
Vector(
LogicalExpression.Relation(
Literal.KeyPath(Field.UserData("region")),
RelationOp.Neq,
Literal.StringValue("eu-west-1")
),
LogicalExpression.Relation(
Literal.KeyPath(Field.UserData("region")),
RelationOp.Eq,
Literal.StringValue("us-east-1")
)
)
)
)
)
),
Operator.OrderBy(
Vector(
Literal.KeyPath(Field.Metadata("severity")),
Literal.KeyPath(Field.Metadata("timestamp"))
),
Sort.Desc
),
Operator.Limit(42L)
)
)
assertEquals(obtained, expected)
}
parser = QueryParser(
"""|source logs |
| filter $d.result == 1 + 42 * 13 + $d.constant:number |
| limit 42
|""".stripMargin
)
parser.Parser.run() match {
case Left(cause) => println(parser.formatError(cause))
case Right(obtained) =>
import Query.*
import munit.Assertions.*
val expected =
Query(
Source.Logs,
Vector(
Operator.Filter(
LogicalExpression.Relation(
Literal.KeyPath(Field.UserData("result")),
RelationOp.Eq,
ArithmeticExpression(
ArithmeticExpression(
Literal.NumberValue(1),
ArithmeticOp.Add,
ArithmeticExpression(
Literal.NumberValue(42),
ArithmeticOp.Mul,
Literal.NumberValue(13)
)
),
ArithmeticOp.Add,
Cast(Literal.KeyPath(Field.UserData("constant")), ValueType.Number)
)
)
),
Operator.Limit(42L)
)
)
assertEquals(obtained, expected)
}