-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathexpr_json_test.go
More file actions
465 lines (429 loc) · 17.8 KB
/
Copy pathexpr_json_test.go
File metadata and controls
465 lines (429 loc) · 17.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package iceberg_test
import (
"encoding/json"
"math"
"testing"
"github.com/apache/iceberg-go"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestMarshalExpressionJSON checks that expressions serialize to the exact JSON
// that Java's ExpressionParser produces.
func TestMarshalExpressionJSON(t *testing.T) {
tests := []struct {
name string
expr iceberg.BooleanExpression
want string
}{
{
name: "always true",
expr: iceberg.AlwaysTrue{},
want: `true`,
},
{
name: "always false",
expr: iceberg.AlwaysFalse{},
want: `false`,
},
{
name: "is null",
expr: iceberg.IsNull(iceberg.Reference("a")),
want: `{"type":"is-null","term":"a"}`,
},
{
name: "not null",
expr: iceberg.NotNull(iceberg.Reference("a")),
want: `{"type":"not-null","term":"a"}`,
},
{
name: "is nan",
expr: iceberg.IsNaN(iceberg.Reference("f")),
want: `{"type":"is-nan","term":"f"}`,
},
{
name: "equal int",
expr: iceberg.EqualTo(iceberg.Reference("name"), int32(25)),
want: `{"type":"eq","term":"name","value":25}`,
},
{
name: "less than or equal long",
expr: iceberg.LessThanEqual(iceberg.Reference("c"), int64(50)),
want: `{"type":"lt-eq","term":"c","value":50}`,
},
{
name: "greater than double",
expr: iceberg.GreaterThan(iceberg.Reference("d"), float64(3.5)),
want: `{"type":"gt","term":"d","value":3.5}`,
},
{
name: "not equal string",
expr: iceberg.NotEqualTo(iceberg.Reference("s"), "abc"),
want: `{"type":"not-eq","term":"s","value":"abc"}`,
},
{
name: "starts with",
expr: iceberg.StartsWith(iceberg.Reference("s"), "ab"),
want: `{"type":"starts-with","term":"s","value":"ab"}`,
},
{
name: "not starts with",
expr: iceberg.NotStartsWith(iceberg.Reference("s"), "ab"),
want: `{"type":"not-starts-with","term":"s","value":"ab"}`,
},
{
name: "equal bool",
expr: iceberg.EqualTo(iceberg.Reference("b"), true),
want: `{"type":"eq","term":"b","value":true}`,
},
{
name: "in ints (sorted)",
expr: iceberg.IsIn(iceberg.Reference("c"), int32(52), int32(50), int32(51)),
want: `{"type":"in","term":"c","values":[50,51,52]}`,
},
{
name: "not in strings (sorted)",
expr: iceberg.NotIn(iceberg.Reference("c"), "two", "one"),
want: `{"type":"not-in","term":"c","values":["one","two"]}`,
},
{
name: "not",
expr: iceberg.NewNot(iceberg.GreaterThanEqual(iceberg.Reference("c"), int32(50))),
want: `{"type":"not","child":{"type":"gt-eq","term":"c","value":50}}`,
},
{
name: "and",
expr: iceberg.NewAnd(
iceberg.GreaterThanEqual(iceberg.Reference("c1"), int32(50)),
iceberg.IsIn(iceberg.Reference("c2"), "one", "two"),
),
want: `{"type":"and","left":{"type":"gt-eq","term":"c1","value":50},"right":{"type":"in","term":"c2","values":["one","two"]}}`,
},
{
name: "or with nested and",
expr: iceberg.NewOr(
iceberg.NewAnd(
iceberg.IsIn(iceberg.Reference("c1"), int32(50)),
iceberg.EqualTo(iceberg.Reference("c2"), "test"),
),
iceberg.IsNaN(iceberg.Reference("c3")),
),
// c1 IN (50) folds to c1 == 50
want: `{"type":"or","left":{"type":"and","left":{"type":"eq","term":"c1","value":50},"right":{"type":"eq","term":"c2","value":"test"}},"right":{"type":"is-nan","term":"c3"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.expr)
require.NoError(t, err)
assert.JSONEq(t, tt.want, string(got))
// JSONEq is order-insensitive; also assert exact bytes to lock field order.
assert.Equal(t, tt.want, string(got))
})
}
}
// TestMarshalExpressionTypedLiterals checks the wire encoding of the trickier
// literal types against SingleValueParser's format.
func TestMarshalExpressionTypedLiterals(t *testing.T) {
tests := []struct {
name string
lit any
want string
}{
{"date", mustLit(t, "2022-08-14", iceberg.PrimitiveTypes.Date), `{"type":"eq","term":"d","value":"2022-08-14"}`},
{"uuid", iceberg.UUIDLiteral(uuid.MustParse("f79c3e09-677c-4bbd-a479-3f349cb785e7")), `{"type":"eq","term":"d","value":"f79c3e09-677c-4bbd-a479-3f349cb785e7"}`},
{"binary", iceberg.BinaryLiteral([]byte{0x01, 0x02, 0x03}), `{"type":"eq","term":"d","value":"010203"}`},
{"decimal", mustLit(t, "3.14", iceberg.DecimalTypeOf(9, 2)), `{"type":"eq","term":"d","value":"3.14"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lit, ok := tt.lit.(iceberg.Literal)
require.True(t, ok)
expr := iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("d"), lit)
got, err := json.Marshal(expr)
require.NoError(t, err)
assert.Equal(t, tt.want, string(got))
})
}
}
// TestMarshalExpressionTimestamp checks the timestamp and timestamptz wire forms,
// which differ only by the UTC offset and so depend on the bound field type.
func TestMarshalExpressionTimestamp(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "ts", Type: iceberg.PrimitiveTypes.Timestamp},
iceberg.NestedField{ID: 2, Name: "tstz", Type: iceberg.PrimitiveTypes.TimestampTz},
)
lit := mustLit(t, "2022-08-14T10:00:00", iceberg.PrimitiveTypes.Timestamp)
tests := []struct {
field string
want string
}{
{"ts", `{"type":"eq","term":"ts","value":"2022-08-14T10:00:00"}`},
{"tstz", `{"type":"eq","term":"tstz","value":"2022-08-14T10:00:00+00:00"}`},
}
for _, tt := range tests {
t.Run(tt.field, func(t *testing.T) {
bound, err := iceberg.BindExpr(schema,
iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference(tt.field), lit), true)
require.NoError(t, err)
got, err := json.Marshal(bound)
require.NoError(t, err)
assert.Equal(t, tt.want, string(got))
// Parse back with the schema to resolve the value via the same type.
parsed, err := iceberg.ParseExpr(got, schema)
require.NoError(t, err)
rebound, err := iceberg.BindExpr(schema, parsed, true)
require.NoError(t, err)
assert.Truef(t, bound.Equals(rebound), "want %s, got %s", bound, rebound)
})
}
}
// TestMarshalExpressionTimestampSubsecond guards the sub-second round-trip:
// fractional seconds must survive marshal+parse, and the nanosecond types must
// keep all nine digits rather than truncating to microseconds.
func TestMarshalExpressionTimestampSubsecond(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "ts", Type: iceberg.PrimitiveTypes.Timestamp},
iceberg.NestedField{ID: 2, Name: "tstz", Type: iceberg.PrimitiveTypes.TimestampTz},
iceberg.NestedField{ID: 3, Name: "tsns", Type: iceberg.PrimitiveTypes.TimestampNs},
iceberg.NestedField{ID: 4, Name: "tstzns", Type: iceberg.PrimitiveTypes.TimestampTzNs},
)
tests := []struct {
field string
value string
typ iceberg.Type
want string
}{
{"ts", "2022-08-14T10:00:00.123456", iceberg.PrimitiveTypes.Timestamp, `{"type":"eq","term":"ts","value":"2022-08-14T10:00:00.123456"}`},
{"tstz", "2022-08-14T10:00:00.123456+00:00", iceberg.PrimitiveTypes.TimestampTz, `{"type":"eq","term":"tstz","value":"2022-08-14T10:00:00.123456+00:00"}`},
{"tsns", "2007-12-03T10:15:30.123456789", iceberg.PrimitiveTypes.TimestampNs, `{"type":"eq","term":"tsns","value":"2007-12-03T10:15:30.123456789"}`},
{"tstzns", "2007-12-03T10:15:30.123456789+00:00", iceberg.PrimitiveTypes.TimestampTzNs, `{"type":"eq","term":"tstzns","value":"2007-12-03T10:15:30.123456789+00:00"}`},
}
for _, tt := range tests {
t.Run(tt.field, func(t *testing.T) {
bound, err := iceberg.BindExpr(schema,
iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference(tt.field), mustLit(t, tt.value, tt.typ)), true)
require.NoError(t, err)
got, err := json.Marshal(bound)
require.NoError(t, err)
assert.Equal(t, tt.want, string(got))
parsed, err := iceberg.ParseExpr(got, schema)
require.NoError(t, err)
rebound, err := iceberg.BindExpr(schema, parsed, true)
require.NoError(t, err)
assert.Truef(t, bound.Equals(rebound), "want %s, got %s", bound, rebound)
})
}
}
// TestMarshalExpressionUnboundTimestamp covers the gap: an unbound timestamp
// predicate has no field type, so it can't be serialized.
func TestMarshalExpressionUnboundTimestamp(t *testing.T) {
expr := iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("ts"),
mustLit(t, "2022-08-14T10:00:00", iceberg.PrimitiveTypes.Timestamp))
_, err := json.Marshal(expr)
require.ErrorIs(t, err, iceberg.ErrInvalidArgument)
}
// TestMarshalBoundExpression guards against bound expressions falling through to
// "{}": their MarshalJSON must delegate to the same encoder as the unbound ones.
func TestMarshalBoundExpression(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "i", Type: iceberg.PrimitiveTypes.Int32},
)
bound, err := iceberg.BindExpr(schema, iceberg.EqualTo(iceberg.Reference("i"), int32(25)), true)
require.NoError(t, err)
got, err := json.Marshal(bound)
require.NoError(t, err)
assert.Equal(t, `{"type":"eq","term":"i","value":25}`, string(got))
}
// TestMarshalExpressionNonFiniteFloat checks that NaN/Inf bounds are rejected
// rather than producing invalid JSON.
func TestMarshalExpressionNonFiniteFloat(t *testing.T) {
for _, v := range []float64{math.NaN(), math.Inf(1), math.Inf(-1)} {
expr := iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("f"), iceberg.Float64Literal(v))
_, err := json.Marshal(expr)
require.ErrorIs(t, err, iceberg.ErrInvalidArgument)
}
}
func mustLit(t *testing.T, s string, typ iceberg.Type) iceberg.Literal {
t.Helper()
lit, err := iceberg.NewLiteral(s).To(typ)
require.NoError(t, err)
return lit
}
// TestExpressionRoundTripSchemaless verifies that parsing without a schema
// normalizes literals to their base JSON kind, as the reference does.
func TestExpressionRoundTripSchemaless(t *testing.T) {
tests := []struct {
name string
json string
want iceberg.BooleanExpression
}{
{"true", `true`, iceberg.AlwaysTrue{}},
{"false", `false`, iceberg.AlwaysFalse{}},
{"literal true", `{"type":"literal","value":true}`, iceberg.AlwaysTrue{}},
{"is null", `{"type":"is-null","term":"a"}`, iceberg.IsNull(iceberg.Reference("a"))},
{"eq long", `{"type":"eq","term":"name","value":25}`, iceberg.EqualTo(iceberg.Reference("name"), int64(25))},
{"eq double", `{"type":"eq","term":"d","value":3.5}`, iceberg.EqualTo(iceberg.Reference("d"), float64(3.5))},
{"eq string", `{"type":"not-eq","term":"s","value":"abc"}`, iceberg.NotEqualTo(iceberg.Reference("s"), "abc")},
{"eq bool", `{"type":"eq","term":"b","value":true}`, iceberg.EqualTo(iceberg.Reference("b"), true)},
{"in longs", `{"type":"in","term":"c","values":[50,51]}`, iceberg.IsIn(iceberg.Reference("c"), int64(50), int64(51))},
{
"and",
`{"type":"and","left":{"type":"gt-eq","term":"c1","value":50},"right":{"type":"is-nan","term":"c2"}}`,
iceberg.NewAnd(
iceberg.GreaterThanEqual(iceberg.Reference("c1"), int64(50)),
iceberg.IsNaN(iceberg.Reference("c2")),
),
},
{
"reference object term",
`{"type":"eq","term":{"type":"reference","term":"name"},"value":25}`,
iceberg.EqualTo(iceberg.Reference("name"), int64(25)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := iceberg.ParseExpr([]byte(tt.json), nil)
require.NoError(t, err)
assert.Truef(t, tt.want.Equals(got), "want %s, got %s", tt.want, got)
})
}
}
// TestExpressionRoundTripWithSchema verifies that with a schema, literals are
// parsed into the referenced field's type, giving an identity round-trip.
func TestExpressionRoundTripWithSchema(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "i", Type: iceberg.PrimitiveTypes.Int32},
iceberg.NestedField{ID: 2, Name: "l", Type: iceberg.PrimitiveTypes.Int64},
iceberg.NestedField{ID: 3, Name: "s", Type: iceberg.PrimitiveTypes.String},
iceberg.NestedField{ID: 4, Name: "b", Type: iceberg.PrimitiveTypes.Bool},
iceberg.NestedField{ID: 5, Name: "f", Type: iceberg.PrimitiveTypes.Float64},
iceberg.NestedField{ID: 6, Name: "d", Type: iceberg.PrimitiveTypes.Date},
iceberg.NestedField{ID: 7, Name: "u", Type: iceberg.PrimitiveTypes.UUID},
iceberg.NestedField{ID: 8, Name: "bin", Type: iceberg.PrimitiveTypes.Binary},
)
exprs := []iceberg.BooleanExpression{
iceberg.EqualTo(iceberg.Reference("i"), int32(25)),
iceberg.LessThan(iceberg.Reference("l"), int64(100)),
iceberg.NotEqualTo(iceberg.Reference("s"), "abc"),
iceberg.EqualTo(iceberg.Reference("b"), true),
iceberg.GreaterThanEqual(iceberg.Reference("f"), float64(1.5)),
iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("d"), mustLit(t, "2022-08-14", iceberg.PrimitiveTypes.Date)),
iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("u"), iceberg.UUIDLiteral(uuid.MustParse("f79c3e09-677c-4bbd-a479-3f349cb785e7"))),
iceberg.LiteralPredicate(iceberg.OpEQ, iceberg.Reference("bin"), iceberg.BinaryLiteral([]byte{0x01, 0x02, 0x03})),
iceberg.IsIn(iceberg.Reference("i"), int32(1), int32(2), int32(3)),
iceberg.NewAnd(
iceberg.EqualTo(iceberg.Reference("i"), int32(1)),
iceberg.NewNot(iceberg.IsNull(iceberg.Reference("s"))),
),
}
for _, want := range exprs {
t.Run(want.String(), func(t *testing.T) {
data, err := json.Marshal(want)
require.NoError(t, err)
got, err := iceberg.ParseExpr(data, schema)
require.NoError(t, err)
assert.Truef(t, want.Equals(got), "want %s, got %s", want, got)
})
}
}
func TestUnmarshalExpressionErrors(t *testing.T) {
tests := []struct {
name string
json string
}{
{"empty", ``},
{"unknown type", `{"type":"bogus","term":"a"}`},
{"unary with value", `{"type":"is-null","term":"a","value":1}`},
{"literal missing value", `{"type":"eq","term":"a"}`},
{"in missing values", `{"type":"in","term":"a"}`},
{"missing term", `{"type":"eq","value":1}`},
{"literal with stray values", `{"type":"eq","term":"a","value":1,"values":[1,2]}`},
{"set with stray value", `{"type":"in","term":"a","values":[1],"value":1}`},
{"literal null value", `{"type":"literal","value":null}`},
{"literal node missing value", `{"type":"literal"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := iceberg.ParseExpr([]byte(tt.json), nil)
require.Error(t, err)
})
}
}
// TestExpressionTransformTermRoundTrip covers a residual filter whose term is a
// partition transform, e.g. a Java server's bucket[100](id) <= 50.
func TestExpressionTransformTermRoundTrip(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "id", Type: iceberg.PrimitiveTypes.Int32},
)
data := []byte(`{"type":"lt-eq","term":{"type":"transform","transform":"bucket[100]","term":"id"},"value":50}`)
parsed, err := iceberg.ParseExpr(data, schema)
require.NoError(t, err)
// The parsed term resolves the transform's result type against the schema.
want := iceberg.LiteralPredicate(iceberg.OpLTEQ,
iceberg.NewUnboundTransform(iceberg.BucketTransform{NumBuckets: 100}, iceberg.Reference("id")),
iceberg.Int32Literal(50))
// And serializes back to the same wire form.
got, err := json.Marshal(want)
require.NoError(t, err)
assert.JSONEq(t, string(data), string(got))
reparsed, err := iceberg.ParseExpr(got, schema)
require.NoError(t, err)
assert.Truef(t, parsed.Equals(reparsed), "want %s, got %s", parsed, reparsed)
// Binding a predicate over a transform term isn't supported yet, but it must
// fail cleanly rather than panic.
_, err = iceberg.BindExpr(schema, parsed, true)
require.ErrorIs(t, err, iceberg.ErrNotImplemented)
}
// TestUnboundTransformBind checks the transform term itself binds to a
// BoundTransform with the transform's result type.
func TestUnboundTransformBind(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "id", Type: iceberg.PrimitiveTypes.Int32},
)
term := iceberg.NewUnboundTransform(iceberg.BucketTransform{NumBuckets: 100}, iceberg.Reference("id"))
bound, err := term.Bind(schema, true)
require.NoError(t, err)
assert.True(t, bound.Type().Equals(iceberg.PrimitiveTypes.Int32))
}
// TestUnmarshalExpressionTransformTermInvalid rejects an unparseable transform
// string rather than panicking or binding nonsense.
func TestUnmarshalExpressionTransformTermInvalid(t *testing.T) {
_, err := iceberg.ParseExpr(
[]byte(`{"type":"eq","term":{"type":"transform","transform":"bogus[16]","term":"id"},"value":1}`), nil)
require.ErrorIs(t, err, iceberg.ErrInvalidArgument)
}
// TestUnmarshalExpressionFixedLength rejects a fixed value whose decoded length
// doesn't match the column's declared length.
func TestUnmarshalExpressionFixedLength(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "f", Type: iceberg.FixedTypeOf(4)},
)
_, err := iceberg.ParseExpr([]byte(`{"type":"eq","term":"f","value":"0102"}`), schema)
require.ErrorIs(t, err, iceberg.ErrInvalidArgument)
}
// TestUnmarshalExpressionCaseSensitive checks that ParseExpr resolves field
// names case-sensitively, since REST field names are authoritative.
func TestUnmarshalExpressionCaseSensitive(t *testing.T) {
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "id", Type: iceberg.PrimitiveTypes.Int32},
)
_, err := iceberg.ParseExpr([]byte(`{"type":"eq","term":"ID","value":1}`), schema)
require.Error(t, err)
}