Skip to content
Open
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
59 changes: 29 additions & 30 deletions iotdb-client/client-py/iotdb/Session.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from .tsfile.utils.date_utils import parse_date_to_int
from .utils import rpc_utils
from .utils.exception import IoTDBConnectionException, RedirectException
from .utils.IoTDBConstants import TSDataType

logger = logging.getLogger("IoTDB")
warnings.simplefilter("always", DeprecationWarning)
Expand Down Expand Up @@ -1723,71 +1724,69 @@ def value_to_bytes(data_types, values):
format_str_list = [">"]
values_tobe_packed = []
for data_type, value in zip(data_types, values):
# BOOLEAN
if data_type == 0:
if data_type == TSDataType.BOOLEAN:
format_str_list.append("c?")
values_tobe_packed.append(b"\x00")
values_tobe_packed.append(bytes([TSDataType.BOOLEAN]))
values_tobe_packed.append(value)
# INT32
elif data_type == 1:
elif data_type == TSDataType.INT32:
format_str_list.append("ci")
values_tobe_packed.append(b"\x01")
values_tobe_packed.append(bytes([TSDataType.INT32]))
values_tobe_packed.append(value)
# INT64
elif data_type == 2:
elif data_type == TSDataType.INT64:
format_str_list.append("cq")
values_tobe_packed.append(b"\x02")
values_tobe_packed.append(bytes([TSDataType.INT64]))
values_tobe_packed.append(value)
# FLOAT
elif data_type == 3:
elif data_type == TSDataType.FLOAT:
format_str_list.append("cf")
values_tobe_packed.append(b"\x03")
values_tobe_packed.append(bytes([TSDataType.FLOAT]))
values_tobe_packed.append(value)
# DOUBLE
elif data_type == 4:
elif data_type == TSDataType.DOUBLE:
format_str_list.append("cd")
values_tobe_packed.append(b"\x04")
values_tobe_packed.append(bytes([TSDataType.DOUBLE]))
values_tobe_packed.append(value)
# TEXT
elif data_type == 5:
elif data_type == TSDataType.TEXT:
if isinstance(value, str):
value_bytes = bytes(value, "utf-8")
else:
value_bytes = value
format_str_list.append("ci")
format_str_list.append(str(len(value_bytes)))
format_str_list.append("s")
values_tobe_packed.append(b"\x05")
values_tobe_packed.append(bytes([TSDataType.TEXT]))
values_tobe_packed.append(len(value_bytes))
values_tobe_packed.append(value_bytes)
# TIMESTAMP
elif data_type == 8:
elif data_type == TSDataType.TIMESTAMP:
format_str_list.append("cq")
values_tobe_packed.append(b"\x08")
values_tobe_packed.append(bytes([TSDataType.TIMESTAMP]))
values_tobe_packed.append(value)
# DATE
elif data_type == 9:
elif data_type == TSDataType.DATE:
format_str_list.append("ci")
values_tobe_packed.append(b"\x09")
values_tobe_packed.append(bytes([TSDataType.DATE]))
values_tobe_packed.append(parse_date_to_int(value))
# BLOB
elif data_type == 10:
elif data_type == TSDataType.BLOB:
format_str_list.append("ci")
format_str_list.append(str(len(value)))
format_str_list.append("s")
values_tobe_packed.append(b"\x0a")
values_tobe_packed.append(bytes([TSDataType.BLOB]))
values_tobe_packed.append(len(value))
values_tobe_packed.append(value)
# STRING
elif data_type == 11:
elif data_type == TSDataType.STRING:
if isinstance(value, str):
value_bytes = bytes(value, "utf-8")
else:
value_bytes = value
format_str_list.append("ci")
format_str_list.append(str(len(value_bytes)))
format_str_list.append("s")
values_tobe_packed.append(b"\x0b")
values_tobe_packed.append(bytes([TSDataType.STRING]))
values_tobe_packed.append(len(value_bytes))
values_tobe_packed.append(value_bytes)
elif data_type == TSDataType.OBJECT:
value_bytes = value if isinstance(value, bytes) else bytes(value)
format_str_list.append("ci")
format_str_list.append(str(len(value_bytes)))
format_str_list.append("s")
values_tobe_packed.append(bytes([TSDataType.OBJECT]))
values_tobe_packed.append(len(value_bytes))
values_tobe_packed.append(value_bytes)
else:
Expand Down
67 changes: 47 additions & 20 deletions iotdb-client/client-py/iotdb/tsfile/utils/tsblock_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
# under the License.
#

from enum import IntEnum, unique

import numpy as np

from iotdb.utils.IoTDBConstants import TS_BLOCK_VALUE_TYPES, TSDataType

# Serialized tsBlock:
# +-------------+---------------+---------+------------+-----------+----------+
# | val col cnt | val col types | pos cnt | encodings | time col | val col |
Expand All @@ -26,6 +30,26 @@
# +-------------+---------------+---------+------------+-----------+----------+


@unique
class TsBlockColumnEncoding(IntEnum):
"""
TsBlock column serialization encoding id on the wire.
Not the same enum as TSEncoding in IoTDBConstants (series encoding in schema).
"""

PLAIN_BOOLEAN = 0
PLAIN_INT32 = 1
PLAIN_INT64 = 2
PLAIN_BINARY = 3
RUN_LENGTH = 4
DICTIONARY = 5


_TS_BLOCK_VALUE_TYPE_ARRAY = np.array(
[int(t) for t in TS_BLOCK_VALUE_TYPES], dtype=np.uint8
)


def deserialize(buffer):
value_column_count, buffer = read_int_from_buffer(buffer)
data_types, buffer = read_column_types(buffer, value_column_count)
Expand All @@ -34,7 +58,7 @@ def deserialize(buffer):
column_encodings, buffer = read_column_encoding(buffer, value_column_count + 1)

time_column_values, _, buffer = read_column(
column_encodings[0], buffer, 2, position_count
column_encodings[0], buffer, TSDataType.INT64, position_count
)
column_values = []
null_indicators = []
Expand Down Expand Up @@ -73,7 +97,7 @@ def read_from_buffer(buffer, size):
def read_column_types(buffer, value_column_count):
data_types = np.frombuffer(buffer, dtype=np.uint8, count=value_column_count)
new_buffer = buffer[value_column_count:]
if not np.all(np.isin(data_types, (0, 1, 2, 3, 4, 5, 8, 9, 10, 11))):
if not np.all(np.isin(data_types, _TS_BLOCK_VALUE_TYPE_ARRAY)):
raise Exception("Invalid data type encountered: " + str(data_types))
return data_types, new_buffer

Expand Down Expand Up @@ -113,9 +137,9 @@ def read_int64_column(buffer, data_type, position_count):
else:
size = np.count_nonzero(~null_indicators)

if data_type == 2:
if data_type == TSDataType.INT64:
dtype = ">i8"
elif data_type == 4:
elif data_type == TSDataType.DOUBLE:
dtype = ">f8"
else:
raise Exception("Invalid data type: " + str(data_type))
Expand All @@ -139,9 +163,9 @@ def read_int32_column(buffer, data_type, position_count):
else:
size = np.count_nonzero(~null_indicators)

if (data_type == 1) or (data_type == 9):
if data_type in (TSDataType.INT32, TSDataType.DATE):
dtype = ">i4"
elif data_type == 3:
elif data_type == TSDataType.FLOAT:
dtype = ">f4"
else:
raise Exception("Invalid data type: " + str(data_type))
Expand All @@ -159,8 +183,8 @@ def read_int32_column(buffer, data_type, position_count):


def read_byte_column(buffer, data_type, position_count):
if data_type != 0:
raise Exception("Invalid data type: " + data_type)
if data_type != TSDataType.BOOLEAN:
raise Exception("Invalid data type: " + str(data_type))
null_indicators, buffer = deserialize_null_indicators(buffer, position_count)
res, buffer = deserialize_from_boolean_array(buffer, position_count)
return res, null_indicators, buffer
Expand Down Expand Up @@ -190,8 +214,8 @@ def deserialize_from_boolean_array(buffer, size):


def read_binary_column(buffer, data_type, position_count):
if data_type != 5:
raise Exception("Invalid data type: " + data_type)
if data_type not in (TSDataType.TEXT, TSDataType.OBJECT):
raise Exception("Invalid data type: " + str(data_type))
null_indicators, buffer = deserialize_null_indicators(buffer, position_count)

if null_indicators is None:
Expand Down Expand Up @@ -229,10 +253,12 @@ def read_run_length_column(buffer, data_type, position_count):


def repeat(column, data_type, position_count):
if data_type in (0, 5):
if data_type in (TSDataType.BOOLEAN, TSDataType.TEXT, TSDataType.OBJECT):
if column.size == 1:
return np.full(
position_count, column[0], dtype=(bool if data_type == 0 else object)
position_count,
column[0],
dtype=(bool if data_type == TSDataType.BOOLEAN else object),
)
else:
return np.array(column * position_count, dtype=object)
Expand All @@ -248,18 +274,19 @@ def read_dictionary_column(buffer, data_type, position_count):


ENCODING_FUNC_MAP = {
0: read_byte_column,
1: read_int32_column,
2: read_int64_column,
3: read_binary_column,
4: read_run_length_column,
5: read_dictionary_column,
TsBlockColumnEncoding.PLAIN_BOOLEAN: read_byte_column,
TsBlockColumnEncoding.PLAIN_INT32: read_int32_column,
TsBlockColumnEncoding.PLAIN_INT64: read_int64_column,
TsBlockColumnEncoding.PLAIN_BINARY: read_binary_column,
TsBlockColumnEncoding.RUN_LENGTH: read_run_length_column,
TsBlockColumnEncoding.DICTIONARY: read_dictionary_column,
}


def read_column(encoding, buffer, data_type, position_count):
try:
func = ENCODING_FUNC_MAP[encoding]
except KeyError:
enc = TsBlockColumnEncoding(int(encoding))
func = ENCODING_FUNC_MAP[enc]
except (ValueError, KeyError):
raise Exception("Unsupported encoding: " + str(encoding))
return func(buffer, data_type, position_count)
35 changes: 20 additions & 15 deletions iotdb-client/client-py/iotdb/utils/Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def copy(field):
or output.get_data_type() == TSDataType.BLOB
):
output.set_binary_value(field.get_binary_value())
elif output.get_data_type() == TSDataType.OBJECT:
output.value = field.value
else:
raise Exception(
"unsupported data type {}".format(output.get_data_type())
Expand Down Expand Up @@ -150,6 +152,10 @@ def set_binary_value(self, value: bytes):
def get_binary_value(self):
if self.__data_type is None:
raise Exception("Null Field Exception!")
if self.__data_type == TSDataType.OBJECT:
raise RuntimeError(
"OBJECT type only supports get_string_value / get_object_value, not get_binary_value"
)
if (
self.__data_type != TSDataType.TEXT
and self.__data_type != TSDataType.STRING
Expand Down Expand Up @@ -185,14 +191,11 @@ def get_date_value(self):
def get_string_value(self):
if self.__data_type is None or self.value is None or self.value is pd.NA:
return "None"
# TEXT, STRING
if self.__data_type == 5 or self.__data_type == 11:
if self.__data_type in (TSDataType.TEXT, TSDataType.STRING):
return self.value.decode("utf-8")
# BLOB
elif self.__data_type == 10:
elif self.__data_type == TSDataType.BLOB:
return str(hex(int.from_bytes(self.value, byteorder="big")))
# TIMESTAMP
elif self.__data_type == 8:
elif self.__data_type == TSDataType.TIMESTAMP:
return isoformat(
convert_to_timestamp(self.value, self.__precision, self.__timezone),
self.__precision,
Expand All @@ -206,27 +209,29 @@ def __str__(self):

def get_object_value(self, data_type):
"""
OBJECT values are returned as raw query-result bytes.

:param data_type: TSDataType
"""
if self.__data_type is None or self.value is None or self.value is pd.NA:
return None
if data_type == 0:
if data_type == TSDataType.BOOLEAN:
return bool(self.value)
elif data_type == 1:
elif data_type == TSDataType.INT32:
return np.int32(self.value)
elif data_type == 2:
elif data_type == TSDataType.INT64:
return np.int64(self.value)
elif data_type == 3:
elif data_type == TSDataType.FLOAT:
return np.float32(self.value)
elif data_type == 4:
elif data_type == TSDataType.DOUBLE:
return np.float64(self.value)
elif data_type == 8:
elif data_type == TSDataType.TIMESTAMP:
return convert_to_timestamp(self.value, self.__precision, self.__timezone)
elif data_type == 9:
elif data_type == TSDataType.DATE:
return parse_int_to_date(self.value)
elif data_type == 5 or data_type == 11:
elif data_type in (TSDataType.TEXT, TSDataType.STRING):
return self.value.decode("utf-8")
elif data_type == 10:
elif data_type in (TSDataType.BLOB, TSDataType.OBJECT):
return self.value
else:
raise RuntimeError("Unsupported data type:" + str(data_type))
Expand Down
Loading
Loading