diff --git a/src/function/list/operations/include/list_extract_operation.h b/src/function/list/operations/include/list_extract_operation.h index 2d7c71b4d8..a2287df590 100644 --- a/src/function/list/operations/include/list_extract_operation.h +++ b/src/function/list/operations/include/list_extract_operation.h @@ -36,8 +36,12 @@ struct ListExtract { } static inline void operation(ku_string_t& str, int64_t& idx, ku_string_t& result) { - auto pos = idx > 0 ? min(idx, (int64_t)str.len) : max(str.len + idx, (int64_t)0) + 1; - result.set((char*)(str.getData() + pos - 1), 1 /* length */); + if (str.len < idx) { + result.set("", 0); + } else { + auto pos = idx > 0 ? min(idx, (int64_t)str.len) : max(str.len + idx, (int64_t)0) + 1; + result.set((char*)(str.getData() + pos - 1), 1 /* length */); + } } static inline void operation(Value& item, int64_t& pos, Value& result) { diff --git a/src/function/string/operations/include/left_operation.h b/src/function/string/operations/include/left_operation.h index 58f98e5d0e..75acf03b64 100644 --- a/src/function/string/operations/include/left_operation.h +++ b/src/function/string/operations/include/left_operation.h @@ -18,8 +18,8 @@ struct Left { public: static inline void operation( ku_string_t& left, int64_t& right, ku_string_t& result, ValueVector& resultValueVector) { - auto len = right > 0 ? min(left.len, (uint32_t)right) : - max(left.len + (uint32_t)right, (uint32_t)0u); + auto len = right >= 0 ? min(left.len, (uint32_t)right) : + ((uint32_t)max(left.len + right, (int64_t)0)); SubStr::operation(left, 1, len, result, resultValueVector); } }; diff --git a/src/function/string/operations/include/pad_operation.h b/src/function/string/operations/include/pad_operation.h index 6818f0d444..63cf3d4a9a 100644 --- a/src/function/string/operations/include/pad_operation.h +++ b/src/function/string/operations/include/pad_operation.h @@ -17,6 +17,10 @@ struct PadOperation { static inline void operation(ku_string_t& src, int64_t count, ku_string_t& characterToPad, ku_string_t& result, ValueVector& resultValueVector, void (*padOperation)(ku_string_t& result, ku_string_t& src, ku_string_t& characterToPad)) { + if (count <= 0) { + result.set("", 0); + return; + } assert(characterToPad.len == 1); result.len = count; if (!ku_string_t::isShortString(result.len)) { diff --git a/src/function/string/operations/include/right_operation.h b/src/function/string/operations/include/right_operation.h index 5a0f0ec4bd..e3ef73df44 100644 --- a/src/function/string/operations/include/right_operation.h +++ b/src/function/string/operations/include/right_operation.h @@ -18,8 +18,8 @@ struct Right { public: static inline void operation( ku_string_t& left, int64_t& right, ku_string_t& result, ValueVector& resultValueVector) { - auto len = right > 0 ? min(left.len, (uint32_t)right) : - max((uint32_t)0u, left.len + (uint32_t)right); + auto len = right >= 0 ? min(left.len, (uint32_t)right) : + ((uint32_t)max((int64_t)0, left.len + right)); SubStr::operation(left, left.len - len + 1, len, result, resultValueVector); } }; diff --git a/test/test_files/tinySNB/function/string.test b/test/test_files/tinySNB/function/string.test index 84a1805836..5ad894f9a5 100644 --- a/test/test_files/tinySNB/function/string.test +++ b/test/test_files/tinySNB/function/string.test @@ -343,6 +343,18 @@ Huber # # +-NAME LpadOutOfRange +-QUERY MATCH (p:person) RETURN lpad(p.fName, -10, "t") +---- 8 + + + + + + + + + -NAME RpadStructuredStrAndLiteralInt -QUERY MATCH (p:person) RETURN rpad(p.fName, 15, ">") ---- 8 @@ -380,6 +392,13 @@ Hubert Bla # # +-NAME RpadOutOfRange +-QUERY MATCH (o:organisation) RETURN rpad(o.name, -8, "y") +---- 3 + + + + -NAME SubStrStructuredStrAndLiteralInt -QUERY MATCH (p:person) RETURN substr(p.fName, 2, 12) ---- 8 @@ -424,16 +443,16 @@ CsWo DEsWor -NAME LeftNegativeIdxStructuredStrAndLiteralInt --QUERY MATCH (p:person) RETURN left(p.fName, -3) +-QUERY MATCH (p:person) RETURN left(p.fName, -4) ---- 8 -Al +A + +C -Ca +Eliza +Fa -Elizab -Far -G -Hubert Blaine Wolfeschlegelsteinhausenbergerdo +Hubert Blaine Wolfeschlegelsteinhausenbergerd # TODO(Semih): Uncomment when enabling ad-hoc properties #-NAME LeftUnstructuredStrAndLiteralInt @@ -468,16 +487,16 @@ Work EsWork -NAME RightNegativeIdxStructuredStrAndLiteralInt --QUERY MATCH (p:person) RETURN right(p.fName, -2) +-QUERY MATCH (p:person) RETURN right(p.fName, -4) ---- 8 -ice -b -rol -n -izabeth -rooq -eg -bert Blaine Wolfeschlegelsteinhausenbergerdorff +e + +l + +abeth +oq + +rt Blaine Wolfeschlegelsteinhausenbergerdorff # TODO(Semih): Uncomment when enabling ad-hoc properties #-NAME RightUnstructuredStrAndLiteralInt @@ -513,6 +532,25 @@ B s E +-NAME ListExtractOutOfRange +-QUERY MATCH (a:person) RETURN a.fName[8] +---- 8 + + + + +t + + +B + +-NAME ListExtractNegativeIndex +-QUERY MATCH (o:organisation) RETURN o.name[-2] +---- 3 +n +r +r + # TODO(Semih): Uncomment when enabling ad-hoc properties #-NAME ListExtractUnstructuredString #-QUERY MATCH (o:organisation) RETURN o.unstrStr[4]