|
| 1 | +import pyarrow as pa |
| 2 | +import pytest |
| 3 | +from databend_udf.udf import ( |
| 4 | + _type_str_to_arrow_field, |
| 5 | + _field_type_to_string, |
| 6 | + _input_process_func, |
| 7 | + _output_process_func, |
| 8 | + _arrow_field_to_string, |
| 9 | +) |
| 10 | + |
| 11 | +def test_vector_sql_generation(): |
| 12 | + # Test nullable VECTOR (default) |
| 13 | + field = _type_str_to_arrow_field("VECTOR(1024)") |
| 14 | + sql_type = _arrow_field_to_string(field) |
| 15 | + assert sql_type == "VECTOR(1024)" |
| 16 | + |
| 17 | + # Test NOT NULL VECTOR |
| 18 | + field_not_null = _type_str_to_arrow_field("VECTOR(1024) NOT NULL") |
| 19 | + sql_type_not_null = _arrow_field_to_string(field_not_null) |
| 20 | + assert sql_type_not_null == "VECTOR(1024) NOT NULL" |
| 21 | + |
| 22 | +def test_vector_type_parsing(): |
| 23 | + field = _type_str_to_arrow_field("VECTOR(1024)") |
| 24 | + assert pa.types.is_fixed_size_list(field.type) |
| 25 | + assert field.type.list_size == 1024 |
| 26 | + assert pa.types.is_float32(field.type.value_type) |
| 27 | + assert field.nullable is True |
| 28 | + |
| 29 | +def test_vector_type_formatting(): |
| 30 | + field = pa.field("", pa.list_(pa.float32(), 1024), nullable=True) |
| 31 | + type_str = _field_type_to_string(field) |
| 32 | + assert type_str == "VECTOR(1024)" |
| 33 | + |
| 34 | +def test_vector_input_processing(): |
| 35 | + field = pa.field("", pa.list_(pa.float32(), 3), nullable=True) |
| 36 | + func = _input_process_func(field) |
| 37 | + |
| 38 | + # Input is a list of floats |
| 39 | + input_data = [1.0, 2.0, 3.0] |
| 40 | + result = func(input_data) |
| 41 | + assert result == [1.0, 2.0, 3.0] |
| 42 | + |
| 43 | + # Input is None |
| 44 | + assert func(None) is None |
| 45 | + |
| 46 | +def test_vector_output_processing(): |
| 47 | + field = pa.field("", pa.list_(pa.float32(), 3), nullable=True) |
| 48 | + func = _output_process_func(field) |
| 49 | + |
| 50 | + # Output is a list of floats |
| 51 | + output_data = [1.0, 2.0, 3.0] |
| 52 | + result = func(output_data) |
| 53 | + assert result == [1.0, 2.0, 3.0] |
| 54 | + |
| 55 | + # Output is None |
| 56 | + assert func(None) is None |
0 commit comments