This is an automated email from the ASF dual-hosted git repository.

colinlee pushed a commit to branch colin_fix_schema_check
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 1de81787e59e5144c8b49738f906db9b8217f5d7
Author: ColinLee <[email protected]>
AuthorDate: Sun May 11 21:57:10 2025 +0800

    add table/column schema check.
---
 python/tests/test_basic.py | 34 ++++++++++++++++++++++++++++++----
 python/tsfile/schema.py    |  8 ++++++++
 python/tsfile/tablet.py    |  3 ++-
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py
index e92836fc..842a8fb4 100644
--- a/python/tests/test_basic.py
+++ b/python/tests/test_basic.py
@@ -20,16 +20,17 @@ import pytest
 from tsfile import schema, Field
 from tsfile import Tablet
 from tsfile.constants import *
+from tsfile.schema import *
 
 
 def test_tablet():
-    column_names = ["temp1", "temp2", "value1", "value2"]
-    data_types = [TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, 
TSDataType.DOUBLE]
+    column_names = ["temp1", "temp2", "value1", "value2", "string1"]
+    data_types = [TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, 
TSDataType.DOUBLE, TSDataType.STRING]
     tablet = Tablet(column_names, data_types)
     tablet.set_table_name("test")
 
     assert "test" == tablet.get_target_name()
-    assert 4 == len(tablet.get_column_name_list())
+    assert 5 == len(tablet.get_column_name_list())
     assert TSDataType.INT32 == tablet.get_data_type_list()[0]
 
     tablet.add_timestamp(0, 10)
@@ -37,12 +38,14 @@ def test_tablet():
     tablet.add_value_by_name("temp2", 0, 100)
     tablet.add_value_by_index(2, 0, 0.1)
     tablet.add_value_by_index(3, 0, 0.1)
+    tablet.add_value_by_index(4, 0, "test")
+    tablet.add_value_by_name("string1", 0, "test")
     # Illegal column name
     with pytest.raises(ValueError):
         tablet.add_value_by_name("temp3", 0, 10)
     # Illegal exists column index
     with pytest.raises(IndexError):
-        tablet.add_value_by_index(4, 0, 10)
+        tablet.add_value_by_index(5, 0, 10)
     # Illegal row index
     with pytest.raises(IndexError):
         tablet.add_value_by_name("temp1", 2048, 10)
@@ -87,6 +90,29 @@ def test_field():
     with pytest.raises(OverflowError):
         field_int64.get_int_value()
 
+def test_schema():
+    column1 = ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG)
+    column2 = ColumnSchema("sensor", TSDataType.STRING, ColumnCategory.TAG)
+    # Default by FIELD.
+    column3 = ColumnSchema("value1", TSDataType.DOUBLE)
+    column4 = ColumnSchema("value2", TSDataType.INT32, ColumnCategory.FIELD)
+    table = TableSchema("test_table", [column1, column2, column3, column4])
+
+    assert column3.get_category() == ColumnCategory.FIELD
+    assert column4.__str__() == "ColumnSchema(value2, INT32, FIELD)"
+
+    with pytest.raises(ValueError):
+        tablet = TableSchema("", [column1, column2, column3, column4])
+
+    with pytest.raises(ValueError):
+        tablet = TableSchema("test_table", [])
+
+    with pytest.raises(ValueError):
+        column = ColumnSchema("test_column",None, ColumnCategory.TAG)
+
+    with pytest.raises(ValueError):
+        tablet = TableSchema("test_table", [ColumnSchema("", 
TSDataType.DOUBLE)])
+
 
 
 
diff --git a/python/tsfile/schema.py b/python/tsfile/schema.py
index 62d5d754..90484a28 100644
--- a/python/tsfile/schema.py
+++ b/python/tsfile/schema.py
@@ -74,7 +74,11 @@ class ColumnSchema:
     data_type = None
 
     def __init__(self, column_name: str, data_type: TSDataType, category: 
ColumnCategory = ColumnCategory.FIELD):
+        if column_name is None or len(column_name) == 0:
+            raise ValueError("Column name cannot be None")
         self.column_name = column_name.lower()
+        if data_type is None:
+            raise ValueError("Data type cannot be None")
         self.data_type = data_type
         self.category = category
 
@@ -97,7 +101,11 @@ class TableSchema:
     columns = None
 
     def __init__(self, table_name: str, columns: List[ColumnSchema]):
+        if table_name is None or len(table_name) == 0:
+            raise ValueError("Table name cannot be None")
         self.table_name = table_name.lower()
+        if len(columns) == 0 or len(columns) == 0:
+            raise ValueError("Columns cannot be empty")
         self.columns = columns
 
     def get_table_name(self):
diff --git a/python/tsfile/tablet.py b/python/tsfile/tablet.py
index 2935db09..52e7389c 100644
--- a/python/tsfile/tablet.py
+++ b/python/tsfile/tablet.py
@@ -137,7 +137,8 @@ class Tablet(object):
         if not isinstance(value, expected_type.to_py_type()):
             raise TypeError(f"Expected {expected_type.to_py_type()} got 
{type(value)}")
 
-        self._check_numeric_range(value, expected_type)
+        if expected_type in (TSDataType.INT32, TSDataType.INT64, 
TSDataType.FLOAT, TSDataType.DOUBLE):
+            self._check_numeric_range(value, expected_type)
 
         self.data_list[col_index][row_index] = value
 

Reply via email to