forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json.py
More file actions
569 lines (471 loc) · 17.9 KB
/
test_json.py
File metadata and controls
569 lines (471 loc) · 17.9 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import json
import tempfile
from pathlib import Path
import lance
import pyarrow as pa
def test_json_basic_write_read():
"""Test basic JSON type write and read functionality."""
# Create test data with JSON strings
json_data = [
{"name": "Alice", "age": 30, "city": "New York"},
{"name": "Bob", "age": 25, "city": "San Francisco"},
{"name": "Charlie", "age": 35, "city": "Chicago"},
None, # Test null handling
{"nested": {"key": "value", "list": [1, 2, 3]}},
]
json_strings = [json.dumps(d) if d is not None else None for d in json_data]
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table(
{
"id": pa.array([1, 2, 3, 4, 5], type=pa.int32()),
"data": json_arr,
}
)
# Write to Lance dataset
with tempfile.TemporaryDirectory() as tmpdir:
dataset_path = Path(tmpdir) / "json_test.lance"
# Write the dataset
lance.write_dataset(table, dataset_path)
# Read back the dataset
dataset = lance.dataset(dataset_path)
# Verify logical schema exposed to users
logical_schema = dataset.schema
assert len(logical_schema) == 2
assert logical_schema.field("id").type == pa.int32()
logical_field = logical_schema.field("data")
assert (
str(logical_field.type) == "extension<arrow.json>"
or logical_field.type == pa.utf8()
)
# Read data back
result_table = dataset.to_table()
# Check that data is returned as Arrow JSON for Python
result_field = result_table.schema.field("data")
# PyArrow extension types print as extension<arrow.json> but
# the storage type is utf8
assert (
str(result_field.type) == "extension<arrow.json>"
or result_field.type == pa.utf8()
)
# Verify data
assert result_table.num_rows == 5
assert result_table.column("id").to_pylist() == [1, 2, 3, 4, 5]
def test_json_with_other_types():
"""Test JSON type alongside other data types."""
# Create mixed type data
json_data = [
{"product": "laptop", "specs": {"cpu": "i7", "ram": 16}},
{"product": "phone", "specs": {"screen": "6.1", "battery": 4000}},
]
json_strings = [json.dumps(d) for d in json_data]
# Create JSON array using PyArrow's JSON type
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table(
{
"id": pa.array([1, 2], type=pa.int64()),
"name": pa.array(["Product A", "Product B"], type=pa.string()),
"price": pa.array([999.99, 599.99], type=pa.float64()),
"metadata": json_arr,
"in_stock": pa.array([True, False], type=pa.bool_()),
}
)
with tempfile.TemporaryDirectory() as tmpdir:
dataset_path = Path(tmpdir) / "mixed_types.lance"
# Write and read the dataset
lance.write_dataset(table, dataset_path)
dataset = lance.dataset(dataset_path)
# Verify all fields are preserved
assert len(dataset.schema) == 5
result = dataset.to_table()
assert result.num_rows == 2
assert result.column("id").to_pylist() == [1, 2]
assert result.column("name").to_pylist() == ["Product A", "Product B"]
assert result.column("in_stock").to_pylist() == [True, False]
def test_json_null_handling():
"""Test handling of null JSON values."""
# Mix of valid JSON and nulls
json_strings = [
json.dumps({"key": "value1"}),
None,
json.dumps({"key": "value2"}),
None,
json.dumps({"key": "value3"}),
]
# Create JSON array with nulls using PyArrow's JSON type
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table({"id": pa.array(range(5)), "optional_data": json_arr})
with tempfile.TemporaryDirectory() as tmpdir:
dataset_path = Path(tmpdir) / "null_test.lance"
lance.write_dataset(table, dataset_path)
dataset = lance.dataset(dataset_path)
result = dataset.to_table()
assert result.num_rows == 5
# Check null mask
data_column = result.column("optional_data")
assert data_column.null_count == 2
assert data_column.is_null().to_pylist() == [False, True, False, True, False]
def test_json_batch_operations():
"""Test batch operations with JSON data."""
# Create multiple batches
batch_size = 1000
num_batches = 5
with tempfile.TemporaryDirectory() as tmpdir:
dataset_path = Path(tmpdir) / "batch_test.lance"
for batch_num in range(num_batches):
json_data = [
json.dumps({"batch": batch_num, "item": i}) for i in range(batch_size)
]
# Create JSON array using PyArrow's JSON type
json_arr = pa.array(json_data, type=pa.json_())
table = pa.table(
{
"id": pa.array(
range(batch_num * batch_size, (batch_num + 1) * batch_size)
),
"batch_data": json_arr,
}
)
if batch_num == 0:
lance.write_dataset(table, dataset_path)
else:
lance.write_dataset(table, dataset_path, mode="append")
# Verify all batches were written
dataset = lance.dataset(dataset_path)
assert dataset.count_rows() == batch_size * num_batches
# Test batch reading
batches = list(dataset.to_batches(batch_size=batch_size))
assert len(batches) == num_batches
for batch in batches:
assert batch.num_rows == batch_size
def test_json_path_queries():
"""Test JSON path queries using json_extract and json_exists."""
# Create test data with JSON columns
json_data = [
{"user": {"name": "Alice", "age": 30}, "tags": ["python", "ml"]},
{"user": {"name": "Bob", "age": 25}, "tags": ["rust", "db"]},
{"user": {"name": "Charlie"}, "tags": []},
None,
]
json_strings = [json.dumps(d) if d is not None else None for d in json_data]
json_arr = pa.array(json_strings, type=pa.json_())
# Create a Lance dataset with JSON data
table = pa.table(
{
"id": [1, 2, 3, 4],
"data": json_arr,
}
)
with tempfile.TemporaryDirectory() as tmpdir:
ds_path = Path(tmpdir) / "json_test.lance"
lance.write_dataset(table, ds_path)
dataset = lance.dataset(ds_path)
# Test json_extract
result = dataset.to_table(
filter="json_extract(data, '$.user.name') = '\"Alice\"'"
)
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE "
"json_extract(data, '$.user.name') = '\"Alice\"'"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
# Test json_exists
result = dataset.to_table(filter="json_exists(data, '$.user.age')")
assert result.num_rows == 2 # Alice and Bob have age field
# Test json_array_contains
result = dataset.to_table(
filter="json_array_contains(data, '$.tags', 'python')"
)
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
def test_json_get_functions():
"""Test json_get_* functions for type-safe value extraction."""
# Create test data with various JSON types
json_data = [
{"name": "Alice", "age": 30, "active": True, "score": 95.5},
{"name": "Bob", "age": 25, "active": False, "score": 87.3},
{"name": "Charlie", "age": "35", "active": "true", "score": "92"},
{"name": "David"}, # Missing fields
]
json_strings = [json.dumps(d) for d in json_data]
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table(
{
"id": [1, 2, 3, 4],
"data": json_arr,
}
)
with tempfile.TemporaryDirectory() as tmpdir:
ds_path = Path(tmpdir) / "json_get_test.lance"
lance.write_dataset(table, ds_path)
dataset = lance.dataset(ds_path)
# Test json_get_string
result = dataset.to_table(filter="json_get_string(data, 'name') = 'Alice'")
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE json_get_string(data, 'name') = 'Alice'"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
# Test json_get_int with type coercion
result = dataset.to_table(filter="json_get_int(data, 'age') > 28")
sql = (
dataset.sql("SELECT * FROM dataset WHERE json_get_int(data, 'age') > 28")
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 2 # Alice (30) and Charlie ("35" -> 35)
# Test json_get_bool with type coercion
result = dataset.to_table(filter="json_get_bool(data, 'active') = true")
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE json_get_bool(data, 'active') = true"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 2 # Alice (true) and Charlie ("true" -> true)
# Test json_get_float
result = dataset.to_table(filter="json_get_float(data, 'score') > 90")
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE json_get_float(data, 'score') > 90"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 2 # Alice (95.5) and Charlie ("92" -> 92.0)
def test_nested_json_access():
"""Test accessing nested JSON structures."""
json_data = [
{"user": {"profile": {"name": "Alice", "settings": {"theme": "dark"}}}},
{"user": {"profile": {"name": "Bob", "settings": {"theme": "light"}}}},
]
json_strings = [json.dumps(d) for d in json_data]
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table(
{
"id": [1, 2],
"data": json_arr,
}
)
with tempfile.TemporaryDirectory() as tmpdir:
ds_path = Path(tmpdir) / "nested_json_test.lance"
lance.write_dataset(table, ds_path)
dataset = lance.dataset(ds_path)
# Access nested fields using json_get recursively
# First get user, then profile, then name
result = dataset.to_table(
filter="""
json_get_string(
json_get(
json_get(data, 'user'),
'profile'),
'name')
= 'Alice'"""
)
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE "
"json_get_string("
"json_get(json_get(data, 'user'), 'profile'), "
"'name') = 'Alice'"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
# Or use JSONPath for deep access
result = dataset.to_table(
filter="json_extract(data, '$.user.profile.settings.theme') = '\"dark\"'"
)
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE "
"json_extract(data, '$.user.profile.settings.theme') = '\"dark\"'"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
def test_json_array_operations():
"""Test JSON array operations."""
json_data = [
{"items": ["apple", "banana", "orange"], "counts": [1, 2, 3, 4, 5]},
{"items": ["grape", "melon"], "counts": [10, 20]},
{"items": [], "counts": []},
]
json_strings = [json.dumps(d) for d in json_data]
json_arr = pa.array(json_strings, type=pa.json_())
table = pa.table(
{
"id": [1, 2, 3],
"data": json_arr,
}
)
with tempfile.TemporaryDirectory() as tmpdir:
ds_path = Path(tmpdir) / "array_json_test.lance"
lance.write_dataset(table, ds_path)
dataset = lance.dataset(ds_path)
# Test array contains
result = dataset.to_table(
filter="json_array_contains(data, '$.items', 'apple')"
)
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE "
"json_array_contains(data, '$.items', 'apple')"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
# Test array length
result = dataset.to_table(filter="json_array_length(data, '$.counts') > 3")
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE json_array_length(data, '$.counts') > 3"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 1
# Test empty array
result = dataset.to_table(filter="json_array_length(data, '$.items') = 0")
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE json_array_length(data, '$.items') = 0"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 1
assert result["id"][0].as_py() == 3
def test_json_filter_append_missing_json_cast(tmp_path: Path):
"""Ensure appending via dataset.schema keeps JSON columns valid."""
dataset_path = tmp_path / "json_append_issue.lance"
initial_table = pa.table(
{
"article_metadata": pa.array(
[json.dumps({"article_journal": "Cell"})], type=pa.json_()
),
"article_journal": pa.array(["Cell"], type=pa.string()),
}
)
lance.write_dataset(initial_table, dataset_path)
dataset = lance.dataset(dataset_path)
schema = dataset.schema
field = schema.field("article_metadata")
assert str(field.type) == "extension<arrow.json>" or field.type == pa.utf8()
append_table = pa.table(
{
"article_metadata": pa.array(
[
json.dumps({"article_journal": "PLoS One"}),
json.dumps({"article_journal": "Nature"}),
],
type=pa.json_(),
),
"article_journal": pa.array(["PLoS One", "Nature"], type=pa.string()),
}
)
append_cast = append_table.cast(schema)
first_value = append_cast.column("article_metadata").to_pylist()[0]
assert isinstance(first_value, str)
lance.write_dataset(append_cast, dataset_path, mode="append")
dataset = lance.dataset(dataset_path)
assert dataset.count_rows() == 3
result = dataset.to_table(
filter="json_get(article_metadata, 'article_journal') IS NOT NULL"
)
sql = (
dataset.sql(
"SELECT * FROM dataset WHERE "
"json_get(article_metadata, 'article_journal') IS NOT NULL"
)
.build()
.to_batch_records()
)
sql_result = pa.Table.from_batches(sql)
assert result == sql_result
assert result.num_rows == 3
assert result.column("article_journal").to_pylist() == [
"Cell",
"PLoS One",
"Nature",
]
def test_json_limit_offset_batch_transfer_preserves_extension_metadata(tmp_path: Path):
"""Ensure JSON extension metadata survives limit/offset scans.
This covers recreating a table by reading a source dataset in chunks and
appending each chunk into a new dataset.
"""
source_path = tmp_path / "json_source.lance"
dest_path = tmp_path / "json_dest.lance"
num_rows = 25
batch_size = 10
table = pa.table(
{
"id": pa.array(range(num_rows), type=pa.int32()),
"meta": pa.array(
[json.dumps({"i": i}) for i in range(num_rows)], type=pa.json_()
),
}
)
lance.write_dataset(table, source_path)
source = lance.dataset(source_path)
first_batch = source.to_table(limit=batch_size)
meta_field = first_batch.schema.field("meta")
assert (
str(meta_field.type) == "extension<arrow.json>" or meta_field.type == pa.utf8()
)
lance.write_dataset(first_batch, dest_path, mode="overwrite")
offset = batch_size
while True:
batch = source.to_table(limit=batch_size, offset=offset)
if batch.num_rows == 0:
break
assert batch.schema == first_batch.schema
meta_field = batch.schema.field("meta")
assert (
str(meta_field.type) == "extension<arrow.json>"
or meta_field.type == pa.utf8()
)
lance.write_dataset(batch, dest_path, mode="append")
offset += batch_size
dest = lance.dataset(dest_path)
assert dest.count_rows() == num_rows
# Ensure JSON functions still recognize the column as JSON.
assert dest.to_table(filter="json_get(meta, 'i') IS NOT NULL").num_rows == num_rows