Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions python/tests/test_write_and_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,3 +1109,63 @@ def test_tree_all_datatype_query_to_dataframe_variants():
if __name__ == "__main__":
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pytest.main(["test_write_and_read.py::test_row_record_write_and_read", "-s", "-v"])


def test_float_nan_inf_write_and_read():
import struct

file_path = "float_nan_inf_write_and_read.tsfile"
specials = [float("nan"), float("inf"), float("-inf")]
expected_bits = [0x7FC00000, 0x7F800000, 0xFF800000]

def f32_bits(value):
return struct.unpack("<I", struct.pack("<f", float(value)))[0]

try:
if os.path.exists(file_path):
os.remove(file_path)

writer = TsFileWriter(file_path)
writer.register_timeseries(
"root.float_special",
TimeseriesSchema(
"f_alp", TSDataType.FLOAT, TSEncoding.ALP, Compressor.UNCOMPRESSED
),
)
writer.register_timeseries(
"root.float_special",
TimeseriesSchema("f_plain", TSDataType.FLOAT, TSEncoding.PLAIN),
)

for i, value in enumerate(specials):
writer.write_row_record(
RowRecord(
"root.float_special",
i,
[
Field("f_alp", value, TSDataType.FLOAT),
Field("f_plain", value, TSDataType.FLOAT),
],
)
)
writer.close()

reader = TsFileReader(file_path)
result = reader.query_timeseries(
"root.float_special", ["f_alp", "f_plain"], 0, 10
)
read_back = []
while result.next():
read_back.append(
(result.get_value_by_index(2), result.get_value_by_index(3))
)
result.close()
reader.close()

assert len(read_back) == len(specials)
for i, (alp, plain) in enumerate(read_back):
assert f32_bits(alp) == expected_bits[i]
assert f32_bits(plain) == expected_bits[i]
finally:
if os.path.exists(file_path):
os.remove(file_path)
3 changes: 3 additions & 0 deletions python/tsfile/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
from datetime import datetime

import math
import numpy as np

from tsfile.constants import TSDataType
Expand Down Expand Up @@ -140,6 +141,8 @@ def get_float_value(self):
raise TypeError(
f"Expected INT32/64 or DOUBLE/FLOAT data type, got {self.data_type}."
)
if not math.isfinite(self.value):
return np.float32(self.value)
min_float32, max_float32 = np.finfo(np.float32).min, np.finfo(np.float32).max
if not (min_float32 <= self.value <= max_float32):
raise OverflowError(
Expand Down
Loading