-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest_tsfile_dataset.py
More file actions
977 lines (805 loc) · 34.5 KB
/
test_tsfile_dataset.py
File metadata and controls
977 lines (805 loc) · 34.5 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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# 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.
#
import numpy as np
import pandas as pd
import pytest
from tsfile.dataset import dataframe as dataframe_module
from tsfile import ColumnCategory, ColumnSchema, TSDataType, TableSchema, TsFileTableWriter
from tsfile import AlignedTimeseries, Timeseries, TsFileDataFrame
from tsfile.dataset.formatting import format_timestamp
from tsfile.dataset.metadata import MetadataCatalog, build_series_path, resolve_series_path
from tsfile.dataset.reader import TsFileSeriesReader, _build_exact_tag_filter
def _write_weather_file(path, start):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [start, start + 1, start + 2],
"device": ["device_a", "device_a", "device_a"],
"temperature": [20.0, 21.5, 23.0],
"humidity": [50.0, 52.0, 55.0],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_weather_rows_file(path, rows):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(rows)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_empty_weather_file(path):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
with TsFileTableWriter(str(path), schema):
pass
def _write_numeric_and_text_file(path):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("status", TSDataType.STRING, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [0, 1, 2],
"device": ["device_a", "device_a", "device_a"],
"temperature": [20.0, np.nan, 23.5],
"status": ["ok", "warn", "ok"],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_partial_numeric_rows_file(path):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [0, 1],
"device": ["device_a", "device_a"],
"temperature": [np.nan, 21.0],
"humidity": [50.0, 51.0],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_weather_with_extra_field_file(path, start):
schema = TableSchema(
"weather",
[
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("pressure", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [start, start + 1],
"device": ["device_a", "device_a"],
"temperature": [20.0, 21.0],
"humidity": [50.0, 51.0],
"pressure": [1000.0, 1001.0],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_multi_tag_file(path):
schema = TableSchema(
"weather",
[
ColumnSchema("city", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("humidity", TSDataType.DOUBLE, ColumnCategory.FIELD),
ColumnSchema("status", TSDataType.STRING, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [0, 1, 0, 1],
"city": ["beijing", "beijing", "shanghai", "shanghai"],
"device": ["device_a", "device_a", "device_b", "device_b"],
"temperature": [20.0, 21.0, 24.0, 25.0],
"humidity": [50.0, 51.0, 60.0, 61.0],
"status": ["ok", "ok", "warn", "warn"],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def _write_special_tag_file(path):
schema = TableSchema(
"weather",
[
ColumnSchema("city", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG),
ColumnSchema("temperature", TSDataType.DOUBLE, ColumnCategory.FIELD),
],
)
df = pd.DataFrame(
{
"time": [0, 1],
"city": ["bei.jing", "bei.jing"],
"device": [r"dev\1", r"dev\1"],
"temperature": [20.0, 21.0],
}
)
with TsFileTableWriter(str(path), schema) as writer:
writer.write_dataframe(df)
def test_dataset_top_level_imports():
assert TsFileDataFrame.__module__ == "tsfile.dataset.dataframe"
assert Timeseries.__module__ == "tsfile.dataset.timeseries"
assert AlignedTimeseries.__module__ == "tsfile.dataset.timeseries"
def test_format_timestamp_preserves_millisecond_precision():
assert "." not in format_timestamp(1000)
assert format_timestamp(1).endswith(".001")
def test_dataset_basic_access_patterns(tmp_path, capsys):
path1 = tmp_path / "part1.tsfile"
path2 = tmp_path / "part2.tsfile"
_write_weather_file(path1, 0)
_write_weather_file(path2, 3)
with TsFileDataFrame([str(path1), str(path2)], show_progress=False) as tsdf:
assert len(tsdf) == 2
first = tsdf[0]
assert isinstance(first, Timeseries)
assert first.name in tsdf.list_timeseries()
assert len(first) == 6
assert first[0] == 20.0
assert first[-1] == 23.0
assert "Timeseries(" in repr(first)
by_name = tsdf[first.name]
assert isinstance(by_name, Timeseries)
assert by_name.name == first.name
subset = tsdf[:1]
assert isinstance(subset, TsFileDataFrame)
assert len(subset) == 1
selected = tsdf[[0, 1]]
assert isinstance(selected, TsFileDataFrame)
assert len(selected) == 2
aligned = tsdf.loc[0:5, [0, 1]]
assert isinstance(aligned, AlignedTimeseries)
assert aligned.shape == (6, 2)
aligned_negative = tsdf.loc[0:5, [-1]]
assert isinstance(aligned_negative, AlignedTimeseries)
assert aligned_negative.shape == (6, 1)
assert list(tsdf["field"]) == ["temperature", "humidity"]
assert "TsFileDataFrame(2 time series, 2 files)" in repr(tsdf)
aligned.show(2)
assert "AlignedTimeseries(6 rows, 2 series)" in capsys.readouterr().out
def test_dataset_loc_aligns_timestamp_union_and_preserves_requested_order(tmp_path):
path = tmp_path / "weather_sparse.tsfile"
_write_weather_rows_file(
path,
{
"time": [0, 1, 2],
"device": ["device_a", "device_a", "device_a"],
"temperature": [10.0, np.nan, 30.0],
"humidity": [np.nan, 200.0, 300.0],
},
)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
aligned = tsdf.loc[
0:2,
[
"weather.device_a.humidity",
"weather.device_a.temperature",
],
]
assert isinstance(aligned, AlignedTimeseries)
assert aligned.series_names == [
"weather.device_a.humidity",
"weather.device_a.temperature",
]
np.testing.assert_array_equal(aligned.timestamps, np.array([0, 1, 2], dtype=np.int64))
assert aligned.shape == (3, 2)
assert np.isnan(aligned.values[0, 0])
assert aligned.values[0, 1] == 10.0
assert aligned.values[1, 0] == 200.0
assert np.isnan(aligned.values[1, 1])
assert aligned.values[2, 0] == 300.0
assert aligned.values[2, 1] == 30.0
def test_dataset_loc_supports_single_timestamp_and_mixed_series_specifiers(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
aligned = tsdf.loc[1, [0, "weather.device_a.humidity"]]
assert isinstance(aligned, AlignedTimeseries)
assert aligned.series_names == [
"weather.device_a.temperature",
"weather.device_a.humidity",
]
np.testing.assert_array_equal(aligned.timestamps, np.array([1], dtype=np.int64))
np.testing.assert_array_equal(aligned.values, np.array([[21.5, 52.0]]))
def test_dataset_loc_supports_open_ended_ranges_and_negative_series_index(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 100)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
aligned = tsdf.loc[:101, [-1]]
assert isinstance(aligned, AlignedTimeseries)
assert aligned.series_names == ["weather.device_a.humidity"]
np.testing.assert_array_equal(aligned.timestamps, np.array([100, 101], dtype=np.int64))
np.testing.assert_array_equal(aligned.values, np.array([[50.0], [52.0]]))
def test_dataset_loc_with_nulls_does_not_expand_beyond_requested_time_range(tmp_path):
path = tmp_path / "weather_sparse_range.tsfile"
_write_weather_rows_file(
path,
{
"time": [0, 1, 2, 3],
"device": ["device_a", "device_a", "device_a", "device_a"],
"temperature": [10.0, np.nan, np.nan, 40.0],
"humidity": [np.nan, 20.0, np.nan, 50.0],
},
)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
aligned = tsdf.loc[
1:2,
[
"weather.device_a.temperature",
"weather.device_a.humidity",
],
]
assert isinstance(aligned, AlignedTimeseries)
np.testing.assert_array_equal(aligned.timestamps, np.array([1, 2], dtype=np.int64))
assert aligned.shape == (2, 2)
assert np.isnan(aligned.values[0, 0])
assert aligned.values[0, 1] == 20.0
assert np.isnan(aligned.values[1, 0])
assert np.isnan(aligned.values[1, 1])
def test_dataset_loc_single_timestamp_with_nulls_keeps_exact_time_window(tmp_path):
path = tmp_path / "weather_sparse_point.tsfile"
_write_weather_rows_file(
path,
{
"time": [0, 1, 2],
"device": ["device_a", "device_a", "device_a"],
"temperature": [10.0, np.nan, 30.0],
"humidity": [np.nan, 20.0, 40.0],
},
)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
aligned = tsdf.loc[
1,
[
"weather.device_a.temperature",
"weather.device_a.humidity",
],
]
assert isinstance(aligned, AlignedTimeseries)
np.testing.assert_array_equal(aligned.timestamps, np.array([1], dtype=np.int64))
assert aligned.shape == (1, 2)
assert np.isnan(aligned.values[0, 0])
assert aligned.values[0, 1] == 20.0
def test_dataset_repr_only_builds_preview_rows(tmp_path, monkeypatch):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
tsdf._index.series_refs_ordered = [(0, 0)] * 1000
built_rows = []
def fake_build_series_info(series_ref):
built_rows.append(series_ref)
return {
"table_name": "weather",
"field": "temperature",
"tag_columns": ("device",),
"tag_values": {"device": "device_a"},
"min_time": 0,
"max_time": 2,
"count": 3,
}
def fail_build_series_name(_series_ref):
raise AssertionError("__repr__ should not build full series names for preview output")
monkeypatch.setattr(tsdf, "_build_series_info", fake_build_series_info)
monkeypatch.setattr(tsdf, "_build_series_name", fail_build_series_name)
rendered = repr(tsdf)
assert "TsFileDataFrame(1000 time series, 1 files)" in rendered
assert "..." in rendered
assert len(built_rows) == 20
def test_dataset_exposes_only_numeric_fields_and_keeps_nan(tmp_path):
path = tmp_path / "numeric_and_text.tsfile"
_write_numeric_and_text_file(path)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
assert tsdf.list_timeseries() == ["weather.device_a.temperature"]
series = tsdf[0]
assert series.name == "weather.device_a.temperature"
assert len(series) == 3
assert series.stats == {"start_time": 0, "end_time": 2, "count": 3}
assert np.isnan(series[1])
np.testing.assert_array_equal(series.timestamps, np.array([0, 1, 2], dtype=np.int64))
sliced = series[:]
assert sliced.shape == (3,)
assert np.isnan(sliced[1])
assert sliced[2] == 23.5
assert series[1:1].shape == (0,)
def test_dataset_timeseries_supports_negative_step_slices(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
series = tsdf[0]
np.testing.assert_array_equal(series[::-1], np.array([23.0, 21.5, 20.0]))
np.testing.assert_array_equal(series[::-2], np.array([23.0, 20.0]))
def test_dataset_metadata_discovery_uses_all_numeric_fields(tmp_path):
path = tmp_path / "partial_numeric_rows.tsfile"
_write_partial_numeric_rows_file(path)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
assert tsdf.list_timeseries() == [
"weather.device_a.temperature",
"weather.device_a.humidity",
]
assert list(tsdf["count"]) == [2, 2]
assert list(tsdf["start_time"]) == [0, 0]
assert list(tsdf["end_time"]) == [1, 1]
def test_dataset_rejects_duplicate_timestamps_across_shards(tmp_path):
path1 = tmp_path / "part1.tsfile"
path2 = tmp_path / "part2.tsfile"
_write_weather_file(path1, 0)
_write_weather_file(path2, 2)
with TsFileDataFrame([str(path1), str(path2)], show_progress=False) as tsdf:
series = tsdf["weather.device_a.temperature"]
with pytest.raises(ValueError, match="Duplicate timestamp"):
_ = series.timestamps
def test_dataset_overlap_position_access_avoids_full_timestamp_materialization(tmp_path, monkeypatch):
path1 = tmp_path / "part1.tsfile"
path2 = tmp_path / "part2.tsfile"
_write_weather_rows_file(
path1,
{
"time": [0, 2, 4],
"device": ["device_a", "device_a", "device_a"],
"temperature": [10.0, 30.0, 50.0],
"humidity": [100.0, 300.0, 500.0],
},
)
_write_weather_rows_file(
path2,
{
"time": [1, 3, 5],
"device": ["device_a", "device_a", "device_a"],
"temperature": [20.0, 40.0, 60.0],
"humidity": [200.0, 400.0, 600.0],
},
)
def fail_merge(*_args, **_kwargs):
raise AssertionError("full timestamp merge should not run for overlap position reads")
monkeypatch.setattr(dataframe_module, "_merge_field_timestamps", fail_merge)
with TsFileDataFrame([str(path1), str(path2)], show_progress=False) as tsdf:
series = tsdf["weather.device_a.temperature"]
assert series[0] == 10.0
assert series[1] == 20.0
assert series[4] == 50.0
np.testing.assert_array_equal(series[1:5], np.array([20.0, 30.0, 40.0, 50.0]))
def test_dataset_rejects_data_access_after_close(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
tsdf = TsFileDataFrame(str(path), show_progress=False)
series = tsdf[0]
tsdf.close()
with pytest.raises(RuntimeError, match="TsFileDataFrame is closed"):
_ = tsdf[0]
with pytest.raises(RuntimeError, match="TsFileDataFrame is closed"):
_ = series[0]
def test_subset_close_warns_and_does_not_close_root(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
subset = tsdf[:1]
with pytest.warns(RuntimeWarning, match="no-op"):
subset.close()
series = tsdf[0]
assert series[0] == 20.0
def test_dataset_rejects_incompatible_table_schemas_across_shards(tmp_path):
path1 = tmp_path / "part1.tsfile"
path2 = tmp_path / "part2.tsfile"
_write_weather_file(path1, 0)
_write_weather_with_extra_field_file(path2, 2)
with pytest.raises(ValueError, match="Incompatible schema for table 'weather'"):
TsFileDataFrame([str(path1), str(path2)], show_progress=False)
def test_dataset_skips_empty_tsfile_shards(tmp_path):
empty_path = tmp_path / "empty.tsfile"
data_path = tmp_path / "part.tsfile"
_write_empty_weather_file(empty_path)
_write_weather_file(data_path, 0)
with TsFileDataFrame([str(empty_path), str(data_path)], show_progress=False) as tsdf:
assert tsdf.list_timeseries() == [
"weather.device_a.temperature",
"weather.device_a.humidity",
]
def test_reader_allows_empty_tsfile(tmp_path):
path = tmp_path / "empty.tsfile"
_write_empty_weather_file(path)
reader = TsFileSeriesReader(str(path), show_progress=False)
try:
assert reader.series_paths == []
assert reader.catalog.series_count == 0
finally:
reader.close()
def test_dataset_multi_tag_metadata_discovery(tmp_path):
path = tmp_path / "multi_tag.tsfile"
_write_multi_tag_file(path)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
assert tsdf.list_timeseries() == [
"weather.beijing.device_a.temperature",
"weather.beijing.device_a.humidity",
"weather.shanghai.device_b.temperature",
"weather.shanghai.device_b.humidity",
]
summary = pd.DataFrame(
{
"series_path": tsdf.list_timeseries(),
"table": tsdf["table"],
"city": tsdf["city"],
"device": tsdf["device"],
"field": tsdf["field"],
"start_time": tsdf["start_time"],
"end_time": tsdf["end_time"],
"count": tsdf["count"],
}
).sort_values(["city", "device", "field"]).reset_index(drop=True)
assert list(summary.columns) == [
"series_path",
"table",
"city",
"device",
"field",
"start_time",
"end_time",
"count",
]
assert list(summary["city"]) == ["beijing", "beijing", "shanghai", "shanghai"]
assert list(summary["device"]) == ["device_a", "device_a", "device_b", "device_b"]
assert list(summary["field"]) == ["humidity", "temperature", "humidity", "temperature"]
assert list(summary["count"]) == [2, 2, 2, 2]
def test_dataset_series_paths_escape_special_tag_values(tmp_path):
path = tmp_path / "special_tag.tsfile"
_write_special_tag_file(path)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
expected_path = r"weather.bei\.jing.dev\\1.temperature"
assert tsdf.list_timeseries() == [expected_path]
series = tsdf[expected_path]
assert isinstance(series, Timeseries)
assert series.name == expected_path
assert list(tsdf["city"]) == ["bei.jing"]
assert list(tsdf["device"]) == [r"dev\1"]
def test_reader_series_paths_escape_special_tag_values(tmp_path):
path = tmp_path / "special_tag.tsfile"
_write_special_tag_file(path)
reader = TsFileSeriesReader(str(path), show_progress=False)
try:
expected_path = r"weather.bei\.jing.dev\\1.temperature"
assert reader.series_paths == [expected_path]
info = reader.get_series_info(expected_path)
assert info["tag_values"] == {"city": "bei.jing", "device": r"dev\1"}
finally:
reader.close()
def test_reader_catalog_shares_device_metadata_and_resolves_paths(tmp_path):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 100)
reader = TsFileSeriesReader(str(path), show_progress=False)
try:
assert reader.series_paths == [
"weather.device_a.temperature",
"weather.device_a.humidity",
]
assert len(reader.catalog.table_entries) == 1
assert len(reader.catalog.device_entries) == 1
assert reader.catalog.series_count == 2
by_path = reader.get_series_info("weather.device_a.temperature")
by_ref = reader.get_series_info_by_ref(0, 0)
assert by_ref == by_path
assert by_ref["tag_values"] == {"device": "device_a"}
ts_arr, values = reader.read_series_by_ref(0, 0, 100, 102)
np.testing.assert_array_equal(ts_arr, np.array([100, 101, 102]))
np.testing.assert_array_equal(values, np.array([20.0, 21.5, 23.0]))
finally:
reader.close()
def test_reader_read_series_by_row_retries_across_native_row_query_boundaries():
class _FakeResultSet:
def __init__(self, rows):
self._rows = rows
self._index = -1
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return False
def next(self):
self._index += 1
return self._index < len(self._rows)
def get_value_by_name(self, name):
return self._rows[self._index][name]
class _FakeNativeReader:
def __init__(self, timestamps, values, boundary):
self._timestamps = timestamps
self._values = values
self._boundary = boundary
def query_table_by_row(self, table_name, column_names, offset=0, limit=-1, tag_filter=None):
assert table_name == "pvf"
assert column_names == ["totalcloudcover"]
assert tag_filter is None
if limit < 0:
stop = len(self._timestamps)
else:
stop = min(offset + limit, len(self._timestamps))
# Simulate the current native bug: one row query cannot cross the
# next internal boundary, so callers must re-issue from the
# advanced offset to complete a large logical window.
chunk_stop = min(stop, ((offset // self._boundary) + 1) * self._boundary)
rows = [
{"time": int(self._timestamps[idx]), "totalcloudcover": float(self._values[idx])}
for idx in range(offset, chunk_stop)
]
return _FakeResultSet(rows)
reader = object.__new__(TsFileSeriesReader)
reader._reader = _FakeNativeReader(np.arange(30, dtype=np.int64), np.arange(30, dtype=np.float64), boundary=10)
reader._catalog = MetadataCatalog()
table_id = reader._catalog.add_table("pvf", (), (), ("totalcloudcover",))
device_id = reader._catalog.add_device(table_id, (), 0, 29)
ts_arr, values = reader.read_series_by_row(device_id, 0, 5, 12)
np.testing.assert_array_equal(ts_arr, np.arange(5, 17, dtype=np.int64))
np.testing.assert_array_equal(values, np.arange(5, 17, dtype=np.float64))
def test_series_path_resolution_allows_prefix_tag_values():
catalog = MetadataCatalog()
table_id = catalog.add_table(
"weather",
("site", "device", "region"),
(TSDataType.STRING, TSDataType.STRING, TSDataType.STRING),
("temperature",),
)
device_id = catalog.add_device(table_id, ("site_a", "device_a"), 0, 1)
catalog.series_stats_by_ref[(device_id, 0)] = {
"length": 1,
"min_time": 0,
"max_time": 0,
"timeline_length": 1,
"timeline_min_time": 0,
"timeline_max_time": 0,
}
series_path = build_series_path(catalog, device_id, 0)
assert series_path == "weather.site_a.device_a.temperature"
assert resolve_series_path(catalog, series_path) == (table_id, device_id, 0)
def test_series_path_resolution_allows_missing_trailing_tag_value():
catalog = MetadataCatalog()
table_id = catalog.add_table(
"weather",
("device",),
(TSDataType.STRING,),
("temperature",),
)
device_id = catalog.add_device(table_id, (), 0, 1)
catalog.series_stats_by_ref[(device_id, 0)] = {
"length": 1,
"min_time": 0,
"max_time": 0,
"timeline_length": 1,
"timeline_min_time": 0,
"timeline_max_time": 0,
}
series_path = build_series_path(catalog, device_id, 0)
assert series_path == "weather.temperature"
assert resolve_series_path(catalog, series_path) == (table_id, device_id, 0)
def test_series_path_resolution_uses_named_tags_for_sparse_non_prefix_values():
catalog = MetadataCatalog()
table_id = catalog.add_table(
"weather",
("city", "device", "region"),
(TSDataType.STRING, TSDataType.STRING, TSDataType.STRING),
("temperature",),
)
device_id = catalog.add_device(table_id, (None, "device_a", None), 0, 1)
catalog.series_stats_by_ref[(device_id, 0)] = {
"length": 1,
"min_time": 0,
"max_time": 0,
"timeline_length": 1,
"timeline_min_time": 0,
"timeline_max_time": 0,
}
series_path = build_series_path(catalog, device_id, 0)
assert series_path == "weather.device_a.temperature"
assert resolve_series_path(catalog, series_path) == (table_id, device_id, 0)
def test_reader_metadata_tag_values_trim_trailing_none():
class _Group:
segments = ("weather", "device_a", None, None)
assert TsFileSeriesReader._metadata_tag_values(_Group(), 3) == ("device_a",)
assert TsFileSeriesReader._metadata_tag_values(_Group(), 1) == ("device_a",)
def test_exact_tag_filter_rejects_none_tag_values():
with pytest.raises(NotImplementedError, match="IS NULL / IS NOT NULL"):
_build_exact_tag_filter({"device": None})
with pytest.raises(NotImplementedError, match="IS NULL / IS NOT NULL"):
_build_exact_tag_filter({"city": "beijing", "device": None})
def test_reader_exact_match_with_none_tag_values_fails_fast():
class _FakeNativeReader:
def query_table(self, *args, **kwargs):
raise AssertionError("query should not be issued when None-tag exact matching is unsupported")
def query_table_by_row(self, *args, **kwargs):
raise AssertionError("row query should not be issued when None-tag exact matching is unsupported")
reader = object.__new__(TsFileSeriesReader)
reader._reader = _FakeNativeReader()
reader._catalog = MetadataCatalog()
table_id = reader._catalog.add_table(
"weather",
("city", "device", "region"),
(TSDataType.STRING, TSDataType.STRING, TSDataType.STRING),
("temperature",),
)
device_id = reader._catalog.add_device(table_id, (None, "device_a", "north"), 0, 1)
reader._catalog.series_stats_by_ref[(device_id, 0)] = {
"length": 2,
"min_time": 0,
"max_time": 1,
"timeline_length": 2,
"timeline_min_time": 0,
"timeline_max_time": 1,
}
with pytest.raises(NotImplementedError, match="IS NULL / IS NOT NULL"):
reader.read_series_by_ref(device_id, 0, 0, 1)
with pytest.raises(NotImplementedError, match="IS NULL / IS NOT NULL"):
reader.read_series_by_row(device_id, 0, 0, 2)
def test_dataframe_rejects_null_tag_values_during_metadata_load(tmp_path, monkeypatch):
class _FakeSeriesReader:
def __init__(self, file_path, show_progress=True):
self.file_path = file_path
self.show_progress = show_progress
self._catalog = MetadataCatalog()
table_id = self._catalog.add_table(
"weather",
("city", "device"),
(TSDataType.STRING, TSDataType.STRING),
("temperature",),
)
self._catalog.add_device(table_id, ("beijing", None), 0, 1)
@property
def catalog(self):
return self._catalog
@property
def series_count(self):
return 1
def close(self):
pass
file_path = tmp_path / "null_tag.tsfile"
file_path.touch()
monkeypatch.setattr("tsfile.dataset.reader.TsFileSeriesReader", _FakeSeriesReader)
with pytest.raises(NotImplementedError) as exc_info:
TsFileDataFrame(str(file_path), show_progress=False)
message = str(exc_info.value)
assert "NULL TAG values" in message
assert "weather" in message
assert "device" in message
def test_dataframe_resolves_named_sparse_tag_series_path():
tsdf = object.__new__(TsFileDataFrame)
tsdf._index = dataframe_module._LogicalIndex()
tsdf._index.table_entries["weather"] = dataframe_module.TableEntry(
table_name="weather",
tag_columns=("city", "device", "region"),
tag_types=(TSDataType.STRING, TSDataType.STRING, TSDataType.STRING),
field_columns=("temperature",),
)
device_key = ("weather", (None, "device_a"))
tsdf._index.device_order = [device_key]
tsdf._index.device_index_by_key = {device_key: 0}
tsdf._index.tables_with_sparse_tag_values = {"weather"}
tsdf._index.sparse_device_indices_by_compressed_path = {("weather", ("device_a",)): [0]}
tsdf._index.device_refs = [[]]
tsdf._index.series_refs_ordered = [(0, 0)]
tsdf._index.series_ref_set = {(0, 0)}
tsdf._index.series_ref_map = {(0, 0): []}
assert tsdf.list_timeseries() == ["weather.device_a.temperature"]
assert tsdf._resolve_series_name("weather.device_a.temperature") == (0, 0)
def test_dataframe_list_timeseries_filters_named_sparse_tag_prefix():
tsdf = object.__new__(TsFileDataFrame)
tsdf._index = dataframe_module._LogicalIndex()
tsdf._index.table_entries["weather"] = dataframe_module.TableEntry(
table_name="weather",
tag_columns=("city", "device", "region"),
tag_types=(TSDataType.STRING, TSDataType.STRING, TSDataType.STRING),
field_columns=("temperature",),
)
tsdf._index.device_order = [
("weather", (None, "device_a")),
("weather", ("beijing", "device_b")),
]
tsdf._index.device_index_by_key = {
("weather", (None, "device_a")): 0,
("weather", ("beijing", "device_b")): 1,
}
tsdf._index.tables_with_sparse_tag_values = {"weather"}
tsdf._index.sparse_device_indices_by_compressed_path = {
("weather", ("device_a",)): [0],
("weather", ("beijing", "device_b")): [1],
}
tsdf._index.device_refs = [[], []]
tsdf._index.series_refs_ordered = [(0, 0), (1, 0)]
tsdf._index.series_ref_set = {(0, 0), (1, 0)}
tsdf._index.series_ref_map = {(0, 0): [], (1, 0): []}
assert tsdf.list_timeseries("weather.device_a") == ["weather.device_a.temperature"]
def test_dataframe_list_timeseries_prefix_can_skip_full_name_build(tmp_path, monkeypatch):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
with TsFileDataFrame(str(path), show_progress=False) as tsdf:
tsdf._index.series_refs_ordered = [(0, 0)] * 1000
def fail_build_series_name(_series_ref):
raise AssertionError("list_timeseries(prefix) should not build full names for non-matching series")
monkeypatch.setattr(tsdf, "_build_series_name", fail_build_series_name)
assert tsdf.list_timeseries("pvf") == []
def test_series_path_resolution_reports_ambiguous_sparse_path():
catalog = MetadataCatalog()
table_id = catalog.add_table(
"weather",
("city", "device"),
(TSDataType.STRING, TSDataType.STRING),
("temperature",),
)
first_id = catalog.add_device(table_id, ("beijing", None), 0, 1)
second_id = catalog.add_device(table_id, (None, "beijing"), 0, 1)
for device_id in (first_id, second_id):
catalog.series_stats_by_ref[(device_id, 0)] = {
"length": 1,
"min_time": 0,
"max_time": 0,
"timeline_length": 1,
"timeline_min_time": 0,
"timeline_max_time": 0,
}
assert build_series_path(catalog, first_id, 0) == "weather.beijing.temperature"
assert build_series_path(catalog, second_id, 0) == "weather.beijing.temperature"
with pytest.raises(ValueError, match="Ambiguous series path"):
resolve_series_path(catalog, "weather.beijing.temperature")
def test_reader_show_progress_reports_start_immediately(tmp_path, capsys):
path = tmp_path / "weather.tsfile"
_write_weather_file(path, 0)
reader = TsFileSeriesReader(str(path), show_progress=True)
try:
stderr = capsys.readouterr().err
assert "Reading TsFile metadata: 0/1" in stderr
assert "Reading TsFile metadata: 1 table(s), 2 series ... done" in stderr
finally:
reader.close()
def test_dataframe_parallel_show_progress_reports_start_immediately(tmp_path, capsys):
path1 = tmp_path / "part1.tsfile"
path2 = tmp_path / "part2.tsfile"
_write_weather_file(path1, 0)
_write_weather_file(path2, 3)
with TsFileDataFrame([str(path1), str(path2)], show_progress=True):
pass
stderr = capsys.readouterr().err
assert "Loading TsFile shards: 0/2" in stderr
assert "Loading TsFile shards: 2/2 (4 series) ... done" in stderr