Skip to content

Commit

Permalink
finish cast list <-> fixedlist
Browse files Browse the repository at this point in the history
  • Loading branch information
AEsir777 committed Nov 17, 2023
1 parent a616abf commit ef90642
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 14 deletions.
8 changes: 8 additions & 0 deletions dataset/load-from-test/struct/struct_with_fixed_list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"{a: 2341, b: {[NULL, [NULL, [1], [2], [3], NULL, [4]], [[8], NULL, [7]], NULL, [NULL, [5]], [[6], NULL], NULL]=true}}"
"{a: 1999, b: {[[[1], [2], [3]]]=true}}"
"{a: 1999, b: {[NULL]=true}}"
"{a: 1999, b: {[[NULL]]=true}}"
"{a: -1, b: {[[[1, 2, 3], [4, 5, 6]]]=false}}"
"{a: -1, b: {[[[NULL]]]=false}}"
"{a: -1, b: {[[[9, 9]]]=false}}"
"{a: -1, b: {[[[4, NULL]]]=false}}"
2 changes: 1 addition & 1 deletion src/function/cast_from_string_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ struct SplitStringFixedListOperation {
T value;
auto str = std::string_view{start, (uint32_t)(end - start)};
if (str.empty() || isNull(str)) {
throw ConversionException("Cast failed. NULL is not allowed for FIXEDLIST.");
throw ConversionException("Cast failed. NULL is not allowed for FIXED_LIST.");
}
CastStringHelper::cast(start, str.length(), value);
resultVector->setValue(offset, value);
Expand Down
292 changes: 283 additions & 9 deletions src/function/vector_cast_functions.cpp

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions src/include/function/unary_function_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,31 @@ struct UnaryUDFFunctionWrapper {
}
};

struct CastFixedListToListFunctionExecutor {
template<typename OPERAND_TYPE, typename RESULT_TYPE, typename FUNC, typename OP_WRAPPER>
static void executeSwitch(
common::ValueVector& operand, common::ValueVector& result, void* dataPtr) {
auto numOfEntries = reinterpret_cast<CastFunctionBindData*>(dataPtr)->numOfEntries;
auto numValuesPerList = common::FixedListType::getNumValuesInList(&operand.dataType);

for (auto i = 0u; i < numOfEntries; i++) {
if (!operand.isNull(i)) {
for (auto j = 0u; j < numValuesPerList; j++) {
OP_WRAPPER::template operation<OPERAND_TYPE, RESULT_TYPE, FUNC>(
(void*)(&operand), i * numValuesPerList + j, (void*)(&result),
i * numValuesPerList + j, nullptr);
}
}
}
}
};

struct CastChildFunctionExecutor {
template<typename OPERAND_TYPE, typename RESULT_TYPE, typename FUNC, typename OP_WRAPPER>
static void executeSwitch(
common::ValueVector& operand, common::ValueVector& result, void* dataPtr) {
auto numOfEntry = reinterpret_cast<CastFunctionBindData*>(dataPtr)->numOfEntries;
for (auto i = 0u; i < numOfEntry; i++) {
auto numOfEntries = reinterpret_cast<CastFunctionBindData*>(dataPtr)->numOfEntries;
for (auto i = 0u; i < numOfEntries; i++) {
result.setNull(i, operand.isNull(i));
if (!result.isNull(i)) {
OP_WRAPPER::template operation<OPERAND_TYPE, RESULT_TYPE, FUNC>(
Expand Down
42 changes: 40 additions & 2 deletions test/test_files/tinysnb/cast/cast_error.test
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,10 @@ Binder exception: The number of elements in a fixed list must be greater than 0.
Unsupported data type: Function::castStringToFixedList
-STATEMENT LOAD WITH HEADERS (fixedList INT64[1]) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/fixed_list/fixed_list_null.csv" RETURN *;
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXEDLIST.
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT LOAD WITH HEADERS (fixedList INT64[2]) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/fixed_list/fixed_list_null2.csv" RETURN *;
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXEDLIST.
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT LOAD WITH HEADERS (a UNION(v1 INT64, v2 STRING)) FROM "${KUZU_ROOT_DIRECTORY}/dataset/copy-fault-tests/invalid-utf8/invalid-utf8.csv" RETURN *;
---- error
Copy exception: Invalid UTF8-encoded string.
Expand Down Expand Up @@ -747,3 +747,41 @@ Conversion exception: Unsupported casting function from STRUCT(a:STRING, b:STRIN
-STATEMENT RETURN cast(cast("{a: 12, b: 0}", "STRUCT(a STRING, b STRING)"), "STRUCT(a STRING, c STRING)");
---- error
Conversion exception: Unsupported casting function from STRUCT(a:STRING, b:STRING) to STRUCT(a:STRING, c:STRING).

-LOG InvalidFixedListToList
-STATEMENT RETURN cast(cast("[1, -1]", "INT64[2]"), "UINT8[]");
---- error
Overflow exception: Value -1 is not within UINT8 range
-STATEMENT RETURN cast(cast("[[1, 2], [3, 4]]", "INT64[2][]"), "INT8[]");
---- error
Unsupported casting function from FIXED_LIST to INT8.


-LOG InvalidListToFixedList
-STATEMENT RETURN cast([1000000000000000000], "INT16[1]");
---- error
Overflow exception: Value 1000000000000000000 is not within INT16 range
-STATEMENT RETURN cast([[1000000000000000000]], "INT64[1]");
---- error
Conversion exception: Unsupported casting function from VAR_LIST to numerical type.
-STATEMENT RETURN cast([NULL], "DOUBLE[1]");
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT RETURN cast([NULL, 2], "DOUBLE[2]");
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT RETURN cast([1, 2], "DOUBLE[100]");
---- error
Conversion exception: Unsupported casting VAR_LIST with incorrect list entry to FIXED_LIST. Expected: 100, Actual: 2.
-STATEMENT RETURN cast([[1, 2], [1, 4], [2, 4, 5], [1, 2]], "INT64[2][]");
---- error
Conversion exception: Unsupported casting VAR_LIST with incorrect list entry to FIXED_LIST. Expected: 2, Actual: 3.
-STATEMENT RETURN cast([[1, 2], [1, 4], [1, NULL], [1, 2]], "INT64[2][]");
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT RETURN cast({a: 12, b: {a: [[NULL]]}, c: 2132}, "STRUCT(a INT64, b STRUCT(a INT64[1][]), c INT64)");
---- error
Conversion exception: Cast failed. NULL is not allowed for FIXED_LIST.
-STATEMENT RETURN cast(cast("{3={}, 3= {a: 12}, 3={a:32, b:[1, 2, 3]}}", "MAP(STRING, STRUCT(a INT64, b INT64[]))"), "MAP(STRING, STRUCT(a INT64, b INT64[1]))");
---- error
Conversion exception: Unsupported casting VAR_LIST with incorrect list entry to FIXED_LIST. Expected: 1, Actual: 3.
58 changes: 58 additions & 0 deletions test/test_files/tinysnb/cast/cast_to_nested_types.test
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,61 @@ False|-4325|14|18446744073709551616.000000| dfsa
---- 1
{[,,]={a: -1, b: 1.000000, c: }}

-LOG CastFixedListToList
-STATEMENT RETURN cast(cast("[1,2,3]", "INT64[3]"), "INT128[]"), cast(cast("[1,2,3]", "INT64[3]"), "INT64[]"), cast(cast("[1,2,3]", "INT64[3]"), "INT32[]"), cast(cast("[1,2,3]", "INT64[3]"), "INT16[]"), cast(cast("[1,2,3]", "INT64[3]"), "INT8[]"), cast(cast("[1,2,3]", "INT64[3]"), "UINT64[]"), cast(cast("[1,2,3]", "INT64[3]"), "UINT32[]"), cast(cast("[1,2,3]", "INT64[3]"), "UINT16[]"), cast(cast("[1,2,3]", "INT64[3]"), "UINT8[]"), cast(cast("[1,2,3]", "INT64[3]"), "DOUBLE[]"), cast(cast("[1,2,3]", "INT64[3]"), "STRING[]"), cast(cast("[1,2,3]", "INT64[3]"), "FLOAT[]");
---- 1
[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1.000000,2.000000,3.000000]|[1,2,3]|[1.000000,2.000000,3.000000]
-STATEMENT LOAD WITH HEADERS (list INT64[][], a struct(num INT64, str STRING), fixedList INT16[3]) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/var_list/should_pass.csv" RETURN cast(cast(cast(fixedList, "DOUBLE[]"), "STRING[]"), "FLOAT[]");
---- 10


[0.000000,0.000000,0.000000]
[0.000000,0.000000,0.000000]
[0.000000,111.000000,12.000000]
[0.000000,134.000000,255.000000]
[111.000000,0.000000,0.000000]
[250.000000,249.000000,21.000000]
[255.000000,254.000000,43.000000]
[29.000000,21.000000,1.000000]

-LOG CastNestedFixedListToList
-STATEMENT RETURN cast(cast("[NULL, [0, -1, 1], NULL, [1, 2, -2], NULL]", "FLOAT[3][]"), "INT64[][]"), cast(cast("[NULL, NULL]", "FLOAT[3][]"), "INT32[][]"), cast(cast("[[1, 1, 1], NULL]", "FLOAT[3][]"), "INT16[][]"), cast(cast("[NULL, [1, 1, 1]]", "FLOAT[3][]"), "INT8[][]");
---- 1
[,[0,-1,1],,[1,2,-2],]|[,]|[[1,1,1],]|[,[1,1,1]]

-LOG CastListToFixedList
-STATEMENT RETURN cast([1], "INT64[1]"), cast([1], "INT32[1]"), cast([1], "INT16[1]"), cast([1], "DOUBLE[1]"), cast([1], "FLOAT[1]"), cast(NULL, "INT64[15]");
---- 1
[1]|[1]|[1]|[1.000000]|[1.000000]|
-STATEMENT LOAD WITH HEADERS (list INT64[][], a struct(num INT64, str STRING), fixedList STRING[]) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/var_list/should_pass.csv" RETURN cast(cast(cast(fixedList, "INT16[3]"), "STRING[]"), "FLOAT[3]");
---- 10


[0.000000,0.000000,0.000000]
[0.000000,0.000000,0.000000]
[0.000000,111.000000,12.000000]
[0.000000,134.000000,255.000000]
[111.000000,0.000000,0.000000]
[250.000000,249.000000,21.000000]
[255.000000,254.000000,43.000000]
[29.000000,21.000000,1.000000]
-STATEMENT LOAD WITH HEADERS (stockPrice STRING[]) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/fixed_list/fixed_list_with_null.csv" RETURN cast(stockPrice, "DOUBLE[3]");
---- 3
[3324.123000,342423.437500,432.123000]

[1.000000,4231.000000,432.123000]

-LOG CastNestedListToFixedList
-STATEMENT RETURN cast([[1,2], [3, 4], [5, 6]], "INT32[2][]");
---- 1
[[1,2],[3,4],[5,6]]
-STATEMENT RETURN cast([NULL, [1]], "INT64[1][]"), cast([NULL], "INT64[1][]"), cast([[1], NULL], "INT64[1][]"), cast([NULL], "INT64[1][][]"), cast([[NULL]], "INT64[1][][]"), cast([[[2], NULL], [NULL, [1]]], "INT64[1][][]"), cast([NULL, [NULL, [1]]], "INT64[1][][]");
---- 1
[,[1]]|[]|[[1],]|[]|[[]]|[[[2],],[,[1]]]|[,[,[1]]]
-STATEMENT LOAD WITH HEADERS (struct STRUCT(a INT64, b MAP(INT64[][][], STRING))) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/struct/struct_with_fixed_list.csv" where struct_extract(struct, 'a') > 0 RETURN cast(struct, "STRUCT(a INT64,b MAP(INT64[1][][], STRING))");
---- 4
{a: 1999, b: {[[[1],[2],[3]]]=true}}
{a: 1999, b: {[[]]=true}}
{a: 1999, b: {[]=true}}
{a: 2341, b: {[,[,[1],[2],[3],,[4]],[[8],,[7]],,[,[5]],[[6],],]=true}}

30 changes: 30 additions & 0 deletions test/test_files/tinysnb/function/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff
[4,5]
[10,11,12,3,4,5,6,7]

-LOG CastListOfIntsToFixedList
-STATEMENT MATCH (p:person) WHERE size(p.workedHours) = 2 RETURN cast(p.workedHours, "INT64[2]"), cast(p.workedHours, "INT32[2]"), cast(p.workedHours, "INT16[2]"), cast(p.workedHours, "DOUBLE[2]"), cast(p.workedHours, "FLOAT[2]");
---- 4
[1,9]|[1,9]|[1,9]|[1.000000,9.000000]|[1.000000,9.000000]
[10,5]|[10,5]|[10,5]|[10.000000,5.000000]|[10.000000,5.000000]
[12,8]|[12,8]|[12,8]|[12.000000,8.000000]|[12.000000,8.000000]
[4,5]|[4,5]|[4,5]|[4.000000,5.000000]|[4.000000,5.000000]
-STATEMENT MATCH(p:person) WHERE size(p.workedHours) = 8 RETURN cast(cast(cast(p.workedHours, "INT64[8]"), "INT64[]"), "DOUBLE[8]");
---- 1
[10.000000,11.000000,12.000000,3.000000,4.000000,5.000000,6.000000,7.000000]

-LOG CastListOfListOfIntsToString
-STATEMENT MATCH (p:person) RETURN string(p.courseScoresPerTerm)
---- 9
Expand All @@ -379,6 +390,12 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff
[[10]]
[[7],[10],[6,7]]

-STATEMENT MATCH (p:person) WHERE 5 < p.ID and p.ID < 10 RETURN cast(p.courseScoresPerTerm, "INT16[1][]")
---- 3
[[10]]
[[6],[7],[8]]
[[8]]

-LOG CastListOfListOfIntsToListOfLists
-STATEMENT MATCH (p:person) RETURN cast(p.courseScoresPerTerm, "INT128[][]"), cast(p.courseScoresPerTerm, "INT64[][]"), cast(p.courseScoresPerTerm, "INT32[][]"), cast(p.courseScoresPerTerm, "INT16[][]"), cast(p.courseScoresPerTerm, "INT8[][]"), cast(p.courseScoresPerTerm, "UINT8[][]"), cast(p.courseScoresPerTerm, "UINT16[][]"), cast(p.courseScoresPerTerm, "UINT32[][]"), cast(p.courseScoresPerTerm, "UINT64[][]"), cast(p.courseScoresPerTerm, "DOUBLE[][]"), cast(p.courseScoresPerTerm, "FLOAT[][]")
---- 9
Expand All @@ -402,6 +419,9 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff
[[6],[7],[8]]
[[7],[10],[6,7]]

-LOG CastListOfListOfIntsToListOfFixedList


-LOG CastFixedListToString
-STATEMENT MATCH (p:person) where p.ID > 1 RETURN string(p.grades)
---- 8
Expand All @@ -424,6 +444,16 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff
[77,64,100,54]


-LOG CastFixedListToFixedList
-STATEMENT MATCH(p:person) where p.ID > 4 RETURN cast(p.grades, "UINT64[]"), cast(p.grades, "UINT32[]"), cast(p.grades, "UINT16[]"), cast(p.grades, "UINT8[]"), cast(p.grades, "INT8[]"), cast(p.grades, "INT16[]"), cast(p.grades, "INT32[]"), cast(p.grades, "INT64[]"), cast(p.grades, "INT128[]"), cast(p.grades, "STRING[]"), cast(p.grades, "DOUBLE[]"), cast(p.grades, "FLOAT[]");
---- 6
[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43,83,67,43]|[43.000000,83.000000,67.000000,43.000000]|[43.000000,83.000000,67.000000,43.000000]
[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76,88,99,89]|[76.000000,88.000000,99.000000,89.000000]|[76.000000,88.000000,99.000000,89.000000]
[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77,64,100,54]|[77.000000,64.000000,100.000000,54.000000]|[77.000000,64.000000,100.000000,54.000000]
[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80,78,34,83]|[80.000000,78.000000,34.000000,83.000000]|[80.000000,78.000000,34.000000,83.000000]
[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96,59,65,88]|[96.000000,59.000000,65.000000,88.000000]|[96.000000,59.000000,65.000000,88.000000]
|||||||||||

-LOG CastMapToString
-STATEMENT MATCH (m:movies) RETURN string(m.audience)
---- 3
Expand Down

0 comments on commit ef90642

Please sign in to comment.