Skip to content

Commit

Permalink
Merge pull request #247 from taosdata/feat/TD-25871
Browse files Browse the repository at this point in the history
Feat/td 25871
  • Loading branch information
menshibin authored Apr 12, 2024
2 parents 433377c + 7cfc954 commit e59b5ae
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ jobs:
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
run: |
poetry install --no-interaction --no-root
#----------------------------------------------
# install your root project, if required
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-ubuntu-2204.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ jobs:
uses: codecov/codecov-action@v3
with:
files: coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false

- name: Build Artifacts
run: |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ packages = [
"taosws" = "taos.sqlalchemy:TaosWsDialect"

[tool.poetry.dependencies]
python = ">=3.6.2,<4.0"
python = ">=3.6.2,<3.12"
pytz = "*"
iso8601 = "1.0.2"
requests = ">=2.27.1"
Expand Down
21 changes: 19 additions & 2 deletions taos/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def double(self, values):
self.num = len(values)
self.is_null = cast((c_char * len(values))(*[1 if value is None else 0 for value in values]), c_char_p)

def _str_to_buffer(self, values):
def _str_to_buffer(self, values, encode=True):
self.num = len(values)
is_null = [1 if v is None else 0 for v in values]
self.is_null = cast((c_byte * self.num)(*is_null), c_char_p)
Expand All @@ -392,7 +392,12 @@ def _str_to_buffer(self, values):
buffer_all = b"".join(v[:] for v in buffers)
self.buffer = cast(c_char_p(buffer_all), c_void_p)
else:
_bytes = [value.encode("utf-8") if value is not None else None for value in values]
_bytes = []
if encode:
_bytes = [value.encode("utf-8") if value is not None else None for value in values]
else:
_bytes = [bytes(value) if value is not None else None for value in values]

buffer_length = max(len(b) for b in _bytes if b is not None)
self.buffer = cast(
c_char_p(
Expand Down Expand Up @@ -523,6 +528,18 @@ def varchar(self, values):
self.buffer_type = FieldType.C_VARCHAR
self._str_to_buffer(values)

def varbinary(self, values):
if type(values) is not tuple and type(values) is not list:
values = tuple([values])
self.buffer_type = FieldType.C_VARBINARY
self._str_to_buffer(values, False)

def geometry(self, values):
if type(values) is not tuple and type(values) is not list:
values = tuple([values])
self.buffer_type = FieldType.C_GEOMETRY
self._str_to_buffer(values, False)


def new_bind_param():
# type: () -> TaosBind
Expand Down
2 changes: 1 addition & 1 deletion taos/cinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def taos_fetch_block_v3(result, fields=None, field_count=None, decode_binary=Tru
raise DatabaseError("Invalid data type returned from database")
offsets = []
is_null = []
if fields[i]["type"] in (FieldType.C_VARCHAR, FieldType.C_NCHAR, FieldType.C_JSON, FieldType.C_VARBINARY):
if fields[i]["type"] in (FieldType.C_VARCHAR, FieldType.C_NCHAR, FieldType.C_JSON, FieldType.C_VARBINARY, FieldType.C_GEOMETRY):
offsets = taos_get_column_data_offset(result, i, num_of_rows)
f = convert_block_func_v3(fields[i]["type"], decode_binary=decode_binary)
blocks[i] = f(data, is_null, num_of_rows, offsets, precision)
Expand Down
1 change: 1 addition & 0 deletions taos/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class FieldType(object):
C_BIGINT_UNSIGNED = 14
C_JSON = 15
C_VARBINARY = 16
C_GEOMETRY = 20
# NULL value definition
# NOTE: These values should change according to C definition in tsdb.h
C_BOOL_NULL = 0x02
Expand Down
2 changes: 2 additions & 0 deletions taos/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def convert_block_func(field_type: FieldType, decode_binary=True):
FieldType.C_BIGINT_UNSIGNED: _crow_bigint_unsigned_to_python,
FieldType.C_JSON: _crow_nchar_to_python,
FieldType.C_VARBINARY: _crow_varbinary_to_python,
FieldType.C_GEOMETRY: _crow_varbinary_to_python,
}

CONVERT_FUNC_BLOCK = {
Expand All @@ -302,6 +303,7 @@ def convert_block_func(field_type: FieldType, decode_binary=True):
FieldType.C_BIGINT_UNSIGNED: _crow_bigint_unsigned_to_python,
FieldType.C_JSON: _crow_nchar_to_python_block,
FieldType.C_VARBINARY: _crow_varbinary_to_python_block,
FieldType.C_GEOMETRY: _crow_varbinary_to_python_block,
}


Expand Down
1 change: 1 addition & 0 deletions taos/field_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def convert_block_func_v3(field_type: FieldType, decode_binary=True):
FieldType.C_NCHAR: _crow_nchar_to_python_block_v3,
FieldType.C_JSON: _crow_nchar_to_python_block_v3,
FieldType.C_VARBINARY: _crow_varbinary_to_python_block_v3,
FieldType.C_GEOMETRY: _crow_varbinary_to_python_block_v3,
}


Expand Down
2 changes: 1 addition & 1 deletion taos/tmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def value(self):

block_data = ctypes.cast(block, ctypes.POINTER(ctypes.c_void_p))[i]
if fields[i]["type"] in (
FieldType.C_VARCHAR, FieldType.C_NCHAR, FieldType.C_JSON, FieldType.C_VARBINARY):
FieldType.C_VARCHAR, FieldType.C_NCHAR, FieldType.C_JSON, FieldType.C_VARBINARY, FieldType.C_GEOMETRY):
f = convert_block_func_v3(fields[i]["type"], self.decode_binary)
offsets = taos_get_column_data_offset(self.msg, i, num_rows)
blocks[i] = f(block_data, [], num_rows, offsets, precision)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ def test_varbinary():
conn.execute("create database if not exists test_varbinary_py")
conn.execute("use test_varbinary_py")
conn.execute(
"create stable if not exists stb1 (ts timestamp, v1 int, v2 varchar(50), v3 varbinary(50)) tags(t1 int)"
"create stable if not exists stb1 (ts timestamp, v1 int, v2 varchar(50), v3 varbinary(50), v4 geometry(512)) tags(t1 int)"
)
conn.execute(
"insert into tb1 using stb1 tags(1) values(now, 1, 'varchar\\x8f4e3e', '\\x8f4e3e') "
"(now + 1s, 2, 'varchar value 2', 'binary value_1')"
"insert into tb1 using stb1 tags(1) values(now, 1, 'varchar\\x8f4e3e', '\\x8f4e3e', 'POINT (4.0 8.0)') "
"(now + 1s, 2, 'varchar value 2', 'binary value_1', 'POINT (3.0 5.0)')"
)
conn.execute(
"insert into tb2 using stb1 tags(2) values(now, 1, 'varchar value 3', '\\x8f4e3e') "
"(now + 1s, 2, 'varchar value 4', 'binary value_2')"
"insert into tb2 using stb1 tags(2) values(now, 1, 'varchar value 3', '\\x8f4e3e', 'LINESTRING (1.000000 1.000000, 2.000000 2.000000, 5.000000 5.000000)') "
"(now + 1s, 2, 'varchar value 4', 'binary value_2', 'POLYGON ((3.000000 6.000000, 5.000000 6.000000, 5.000000 8.000000, 3.000000 8.000000, 3.000000 6.000000))')"
)
result = conn.query("select * from stb1")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,4 @@ def test_stmt_null(conn):
test_stmt_insert(connect())
test_stmt_insert_multi(connect())
test_stmt_set_tbname_tag(connect())
test_stmt_null(connect())
test_stmt_null(connect())
Loading

0 comments on commit e59b5ae

Please sign in to comment.