Skip to content

Commit 2520895

Browse files
committed
chore(engine): Make literals simple types
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
1 parent 62d2be5 commit 2520895

27 files changed

+314
-355
lines changed

‎pkg/engine/engine_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func createRecord(t *testing.T, schema *arrow.Schema, data [][]interface{}) arro
5555
}
5656

5757
func TestConvertArrowRecordsToLokiResult(t *testing.T) {
58-
mdTypeLabel := datatype.ColumnMetadata(types.ColumnTypeLabel, datatype.String)
59-
mdTypeMetadata := datatype.ColumnMetadata(types.ColumnTypeMetadata, datatype.String)
58+
mdTypeLabel := datatype.ColumnMetadata(types.ColumnTypeLabel, datatype.Loki.String)
59+
mdTypeMetadata := datatype.ColumnMetadata(types.ColumnTypeMetadata, datatype.Loki.String)
6060

6161
t.Run("rows without log line, timestamp, or labels are ignored", func(t *testing.T) {
6262
schema := arrow.NewSchema(

‎pkg/engine/executor/dataobjscan_predicate.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (m *timestampPredicateMapper) verify(expr physical.Expression) error {
175175
if !okRHS {
176176
return fmt.Errorf("invalid RHS for comparison: expected literal timestamp, got %T", binop.Right)
177177
}
178-
if rhsLit.ValueType() != datatype.Timestamp {
178+
if rhsLit.ValueType() != datatype.Loki.Timestamp {
179179
return fmt.Errorf("unsupported literal type for RHS: %s, expected timestamp", rhsLit.ValueType())
180180
}
181181
return nil
@@ -231,11 +231,12 @@ func (m *timestampPredicateMapper) rebound(op types.BinaryOp, rightExpr physical
231231
return fmt.Errorf("internal error: rebound expected LiteralExpr, got %T for: %s", rightExpr, rightExpr.String())
232232
}
233233

234-
if literalExpr.ValueType() != datatype.Timestamp {
234+
if literalExpr.ValueType() != datatype.Loki.Timestamp {
235235
// Also should be caught by verify.
236236
return fmt.Errorf("internal error: unsupported literal type in rebound: %s, expected timestamp", literalExpr.ValueType())
237237
}
238-
val := literalExpr.Literal.(*datatype.TimestampLiteral).Value()
238+
v := literalExpr.Literal.(datatype.TimestampLiteral).Value()
239+
val := time.Unix(0, int64(v)).UTC()
239240

240241
switch op {
241242
case types.BinaryOpEq: // ts == val
@@ -334,10 +335,10 @@ func mapMetadataPredicate(expr physical.Expression) (logs.RowPredicate, error) {
334335
if !ok { // Should not happen
335336
return nil, fmt.Errorf("RHS of EQ metadata predicate failed to cast to LiteralExpr")
336337
}
337-
if rightLiteral.ValueType() != datatype.String {
338+
if rightLiteral.ValueType() != datatype.Loki.String {
338339
return nil, fmt.Errorf("unsupported RHS literal type (%v) for EQ metadata predicate, expected ValueTypeStr", rightLiteral.ValueType())
339340
}
340-
val := rightLiteral.Literal.(*datatype.StringLiteral).Value()
341+
val := rightLiteral.Literal.(datatype.StringLiteral).Value()
341342

342343
return logs.MetadataMatcherRowPredicate{
343344
Key: leftColumn.Ref.Column,
@@ -510,9 +511,9 @@ func rhsValue(e *physical.BinaryExpr) (string, error) {
510511
if !ok { // Should not happen
511512
return "", fmt.Errorf("RHS of %s message predicate failed to cast to LiteralExpr", op)
512513
}
513-
if rightLiteral.ValueType() != datatype.String {
514+
if rightLiteral.ValueType() != datatype.Loki.String {
514515
return "", fmt.Errorf("unsupported RHS literal type (%v) for %s message predicate, expected ValueTypeStr", rightLiteral.ValueType(), op)
515516
}
516517

517-
return rightLiteral.Literal.(*datatype.StringLiteral).Value(), nil
518+
return rightLiteral.Literal.(datatype.StringLiteral).Value(), nil
518519
}

‎pkg/engine/executor/dataobjscan_predicate_test.go

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/grafana/loki/v3/pkg/dataobj/sections/logs"
12+
"github.com/grafana/loki/v3/pkg/engine/internal/datatype"
1213
"github.com/grafana/loki/v3/pkg/engine/internal/types"
1314
"github.com/grafana/loki/v3/pkg/engine/planner/physical"
1415
)
@@ -31,6 +32,10 @@ func tsColExpr() *physical.ColumnExpr {
3132
return newColumnExpr(types.ColumnNameBuiltinTimestamp, types.ColumnTypeBuiltin)
3233
}
3334

35+
func ts(t time.Time) datatype.Timestamp {
36+
return datatype.Timestamp(t.UnixNano())
37+
}
38+
3439
func TestMapTimestampPredicate(t *testing.T) {
3540
time100 := time.Unix(0, 100).UTC()
3641
time200 := time.Unix(0, 200).UTC()
@@ -47,7 +52,7 @@ func TestMapTimestampPredicate(t *testing.T) {
4752
expr: &physical.BinaryExpr{
4853
Left: tsColExpr(),
4954
Op: types.BinaryOpEq,
50-
Right: physical.NewLiteral(time100),
55+
Right: physical.NewLiteral(ts(time100)),
5156
},
5257
want: logs.TimeRangeRowPredicate{
5358
StartTime: time100,
@@ -61,7 +66,7 @@ func TestMapTimestampPredicate(t *testing.T) {
6166
expr: &physical.BinaryExpr{
6267
Left: tsColExpr(),
6368
Op: types.BinaryOpGt,
64-
Right: physical.NewLiteral(time100),
69+
Right: physical.NewLiteral(ts(time100)),
6570
},
6671
want: logs.TimeRangeRowPredicate{
6772
StartTime: time100,
@@ -75,7 +80,7 @@ func TestMapTimestampPredicate(t *testing.T) {
7580
expr: &physical.BinaryExpr{
7681
Left: tsColExpr(),
7782
Op: types.BinaryOpGte,
78-
Right: physical.NewLiteral(time100),
83+
Right: physical.NewLiteral(ts(time100)),
7984
},
8085
want: logs.TimeRangeRowPredicate{
8186
StartTime: time100,
@@ -89,7 +94,7 @@ func TestMapTimestampPredicate(t *testing.T) {
8994
expr: &physical.BinaryExpr{
9095
Left: tsColExpr(),
9196
Op: types.BinaryOpLt,
92-
Right: physical.NewLiteral(time100),
97+
Right: physical.NewLiteral(ts(time100)),
9398
},
9499
want: logs.TimeRangeRowPredicate{
95100
StartTime: testOpenStart,
@@ -103,7 +108,7 @@ func TestMapTimestampPredicate(t *testing.T) {
103108
expr: &physical.BinaryExpr{
104109
Left: tsColExpr(),
105110
Op: types.BinaryOpLte,
106-
Right: physical.NewLiteral(time100),
111+
Right: physical.NewLiteral(ts(time100)),
107112
},
108113
want: logs.TimeRangeRowPredicate{
109114
StartTime: testOpenStart,
@@ -120,7 +125,7 @@ func TestMapTimestampPredicate(t *testing.T) {
120125
Right: &physical.BinaryExpr{
121126
Left: tsColExpr(),
122127
Op: types.BinaryOpLt,
123-
Right: physical.NewLiteral(time100),
128+
Right: physical.NewLiteral(ts(time100)),
124129
},
125130
},
126131
errMatch: "invalid RHS for comparison: expected literal timestamp, got *physical.BinaryExpr",
@@ -136,23 +141,23 @@ func TestMapTimestampPredicate(t *testing.T) {
136141
Right: &physical.BinaryExpr{
137142
Left: tsColExpr(),
138143
Op: types.BinaryOpLte,
139-
Right: physical.NewLiteral(time200),
144+
Right: physical.NewLiteral(ts(time200)),
140145
},
141146
},
142147
},
143148
errMatch: "invalid RHS for comparison: expected literal timestamp, got *physical.BinaryExpr",
144149
},
145150
{
146151
desc: "input is not BinaryExpr",
147-
expr: physical.NewLiteral(time100),
152+
expr: physical.NewLiteral(ts(time100)),
148153
errMatch: "unsupported expression type for timestamp predicate: *physical.LiteralExpr, expected *physical.BinaryExpr",
149154
},
150155
{
151156
desc: "LHS of BinaryExpr is not ColumnExpr",
152157
expr: &physical.BinaryExpr{
153-
Left: physical.NewLiteral(time100),
158+
Left: physical.NewLiteral(ts(time100)),
154159
Op: types.BinaryOpEq,
155-
Right: physical.NewLiteral(time200),
160+
Right: physical.NewLiteral(ts(time200)),
156161
},
157162
errMatch: "invalid LHS for comparison: expected timestamp column, got 1970-01-01T00:00:00.0000001Z",
158163
},
@@ -161,7 +166,7 @@ func TestMapTimestampPredicate(t *testing.T) {
161166
expr: &physical.BinaryExpr{
162167
Left: newColumnExpr("other_col", types.ColumnTypeBuiltin),
163168
Op: types.BinaryOpEq,
164-
Right: physical.NewLiteral(time100),
169+
Right: physical.NewLiteral(ts(time100)),
165170
},
166171
errMatch: "invalid LHS for comparison: expected timestamp column, got builtin.other_col",
167172
},
@@ -188,7 +193,7 @@ func TestMapTimestampPredicate(t *testing.T) {
188193
expr: &physical.BinaryExpr{
189194
Left: tsColExpr(),
190195
Op: types.BinaryOpAnd,
191-
Right: physical.NewLiteral(time100),
196+
Right: physical.NewLiteral(ts(time100)),
192197
},
193198
errMatch: "invalid left operand for AND: unsupported expression type for timestamp predicate: *physical.ColumnExpr, expected *physical.BinaryExpr",
194199
},
@@ -200,7 +205,7 @@ func TestMapTimestampPredicate(t *testing.T) {
200205
Right: &physical.BinaryExpr{
201206
Left: tsColExpr(),
202207
Op: types.BinaryOpGt,
203-
Right: physical.NewLiteral(time100),
208+
Right: physical.NewLiteral(ts(time100)),
204209
},
205210
},
206211
errMatch: "unsupported operator for timestamp predicate: OR",
@@ -211,13 +216,13 @@ func TestMapTimestampPredicate(t *testing.T) {
211216
Left: &physical.BinaryExpr{
212217
Left: tsColExpr(),
213218
Op: types.BinaryOpGt,
214-
Right: physical.NewLiteral(time100),
219+
Right: physical.NewLiteral(ts(time100)),
215220
},
216221
Op: types.BinaryOpAnd,
217222
Right: &physical.BinaryExpr{
218223
Left: tsColExpr(),
219224
Op: types.BinaryOpLt,
220-
Right: physical.NewLiteral(time200),
225+
Right: physical.NewLiteral(ts(time200)),
221226
},
222227
},
223228
want: logs.TimeRangeRowPredicate{
@@ -233,13 +238,13 @@ func TestMapTimestampPredicate(t *testing.T) {
233238
Left: &physical.BinaryExpr{
234239
Left: tsColExpr(),
235240
Op: types.BinaryOpGte,
236-
Right: physical.NewLiteral(time100),
241+
Right: physical.NewLiteral(ts(time100)),
237242
},
238243
Op: types.BinaryOpAnd,
239244
Right: &physical.BinaryExpr{
240245
Left: tsColExpr(),
241246
Op: types.BinaryOpLte,
242-
Right: physical.NewLiteral(time200),
247+
Right: physical.NewLiteral(ts(time200)),
243248
},
244249
},
245250
want: logs.TimeRangeRowPredicate{
@@ -255,13 +260,13 @@ func TestMapTimestampPredicate(t *testing.T) {
255260
Left: &physical.BinaryExpr{
256261
Left: tsColExpr(),
257262
Op: types.BinaryOpGte,
258-
Right: physical.NewLiteral(time100),
263+
Right: physical.NewLiteral(ts(time100)),
259264
},
260265
Op: types.BinaryOpAnd,
261266
Right: &physical.BinaryExpr{
262267
Left: tsColExpr(),
263268
Op: types.BinaryOpLt,
264-
Right: physical.NewLiteral(time100),
269+
Right: physical.NewLiteral(ts(time100)),
265270
},
266271
},
267272
errMatch: "impossible time range: start_time (1970-01-01 00:00:00.0000001 +0000 UTC) equals end_time (1970-01-01 00:00:00.0000001 +0000 UTC) but the range is exclusive",
@@ -272,13 +277,13 @@ func TestMapTimestampPredicate(t *testing.T) {
272277
Left: &physical.BinaryExpr{
273278
Left: tsColExpr(),
274279
Op: types.BinaryOpGt,
275-
Right: physical.NewLiteral(time100),
280+
Right: physical.NewLiteral(ts(time100)),
276281
},
277282
Op: types.BinaryOpAnd,
278283
Right: &physical.BinaryExpr{
279284
Left: tsColExpr(),
280285
Op: types.BinaryOpLte,
281-
Right: physical.NewLiteral(time100),
286+
Right: physical.NewLiteral(ts(time100)),
282287
},
283288
},
284289
errMatch: "impossible time range: start_time (1970-01-01 00:00:00.0000001 +0000 UTC) equals end_time (1970-01-01 00:00:00.0000001 +0000 UTC) but the range is exclusive",
@@ -289,13 +294,13 @@ func TestMapTimestampPredicate(t *testing.T) {
289294
Left: &physical.BinaryExpr{
290295
Left: tsColExpr(),
291296
Op: types.BinaryOpGte,
292-
Right: physical.NewLiteral(time100),
297+
Right: physical.NewLiteral(ts(time100)),
293298
},
294299
Op: types.BinaryOpAnd,
295300
Right: &physical.BinaryExpr{
296301
Left: tsColExpr(),
297302
Op: types.BinaryOpLte,
298-
Right: physical.NewLiteral(time100),
303+
Right: physical.NewLiteral(ts(time100)),
299304
},
300305
},
301306
want: logs.TimeRangeRowPredicate{
@@ -311,13 +316,13 @@ func TestMapTimestampPredicate(t *testing.T) {
311316
Left: &physical.BinaryExpr{
312317
Left: tsColExpr(),
313318
Op: types.BinaryOpEq,
314-
Right: physical.NewLiteral(time100),
319+
Right: physical.NewLiteral(ts(time100)),
315320
},
316321
Op: types.BinaryOpAnd,
317322
Right: &physical.BinaryExpr{
318323
Left: tsColExpr(),
319324
Op: types.BinaryOpEq,
320-
Right: physical.NewLiteral(time200),
325+
Right: physical.NewLiteral(ts(time200)),
321326
},
322327
},
323328
errMatch: "impossible time range: start_time (1970-01-01 00:00:00.0000002 +0000 UTC) is after end_time (1970-01-01 00:00:00.0000001 +0000 UTC)",
@@ -328,13 +333,13 @@ func TestMapTimestampPredicate(t *testing.T) {
328333
Left: &physical.BinaryExpr{
329334
Left: tsColExpr(),
330335
Op: types.BinaryOpLt,
331-
Right: physical.NewLiteral(time100),
336+
Right: physical.NewLiteral(ts(time100)),
332337
},
333338
Op: types.BinaryOpAnd,
334339
Right: &physical.BinaryExpr{
335340
Left: tsColExpr(),
336341
Op: types.BinaryOpGt,
337-
Right: physical.NewLiteral(time200),
342+
Right: physical.NewLiteral(ts(time200)),
338343
},
339344
},
340345
errMatch: "impossible time range: start_time (1970-01-01 00:00:00.0000002 +0000 UTC) is after end_time (1970-01-01 00:00:00.0000001 +0000 UTC)",
@@ -346,20 +351,20 @@ func TestMapTimestampPredicate(t *testing.T) {
346351
Left: &physical.BinaryExpr{
347352
Left: tsColExpr(),
348353
Op: types.BinaryOpGt,
349-
Right: physical.NewLiteral(time100),
354+
Right: physical.NewLiteral(ts(time100)),
350355
},
351356
Op: types.BinaryOpAnd,
352357
Right: &physical.BinaryExpr{
353358
Left: tsColExpr(),
354359
Op: types.BinaryOpLt,
355-
Right: physical.NewLiteral(time300),
360+
Right: physical.NewLiteral(ts(time300)),
356361
},
357362
},
358363
Op: types.BinaryOpAnd,
359364
Right: &physical.BinaryExpr{
360365
Left: tsColExpr(),
361366
Op: types.BinaryOpEq,
362-
Right: physical.NewLiteral(time200),
367+
Right: physical.NewLiteral(ts(time200)),
363368
},
364369
},
365370
want: logs.TimeRangeRowPredicate{
@@ -375,13 +380,13 @@ func TestMapTimestampPredicate(t *testing.T) {
375380
Left: &physical.BinaryExpr{ // Invalid: LHS not timestamp column
376381
Left: newColumnExpr("not_ts", types.ColumnTypeBuiltin),
377382
Op: types.BinaryOpGt,
378-
Right: physical.NewLiteral(time100),
383+
Right: physical.NewLiteral(ts(time100)),
379384
},
380385
Op: types.BinaryOpAnd,
381386
Right: &physical.BinaryExpr{
382387
Left: tsColExpr(),
383388
Op: types.BinaryOpLt,
384-
Right: physical.NewLiteral(time200),
389+
Right: physical.NewLiteral(ts(time200)),
385390
},
386391
},
387392
errMatch: "invalid left operand for AND: invalid LHS for comparison: expected timestamp column, got builtin.not_ts",
@@ -392,7 +397,7 @@ func TestMapTimestampPredicate(t *testing.T) {
392397
Left: &physical.BinaryExpr{
393398
Left: tsColExpr(),
394399
Op: types.BinaryOpGt,
395-
Right: physical.NewLiteral(time100),
400+
Right: physical.NewLiteral(ts(time100)),
396401
},
397402
Op: types.BinaryOpAnd,
398403
Right: &physical.BinaryExpr{ // Invalid: RHS literal not timestamp
@@ -409,13 +414,13 @@ func TestMapTimestampPredicate(t *testing.T) {
409414
Left: &physical.BinaryExpr{
410415
Left: tsColExpr(),
411416
Op: types.BinaryOpLt,
412-
Right: physical.NewLiteral(time100),
417+
Right: physical.NewLiteral(ts(time100)),
413418
},
414419
Op: types.BinaryOpAnd,
415420
Right: &physical.BinaryExpr{
416421
Left: tsColExpr(),
417422
Op: types.BinaryOpLt,
418-
Right: physical.NewLiteral(time200),
423+
Right: physical.NewLiteral(ts(time200)),
419424
},
420425
},
421426
want: logs.TimeRangeRowPredicate{
@@ -431,13 +436,13 @@ func TestMapTimestampPredicate(t *testing.T) {
431436
Left: &physical.BinaryExpr{
432437
Left: tsColExpr(),
433438
Op: types.BinaryOpLt,
434-
Right: physical.NewLiteral(time200),
439+
Right: physical.NewLiteral(ts(time200)),
435440
},
436441
Op: types.BinaryOpAnd,
437442
Right: &physical.BinaryExpr{
438443
Left: tsColExpr(),
439444
Op: types.BinaryOpLt,
440-
Right: physical.NewLiteral(time100),
445+
Right: physical.NewLiteral(ts(time100)),
441446
},
442447
},
443448
want: logs.TimeRangeRowPredicate{
@@ -620,7 +625,7 @@ func TestMapMetadataPredicate(t *testing.T) {
620625
name: "error: RHS literal not string for EQ",
621626
expr: &physical.BinaryExpr{
622627
Left: &physical.ColumnExpr{Ref: types.ColumnRef{Column: "foo", Type: types.ColumnTypeMetadata}},
623-
Right: physical.NewLiteral(123), // Not string
628+
Right: physical.NewLiteral(int64(123)), // Not string
624629
Op: types.BinaryOpEq,
625630
},
626631
expectedPred: nil,

0 commit comments

Comments
 (0)