Skip to content

Commit

Permalink
Remove unnecessary argument from executors
Browse files Browse the repository at this point in the history
  • Loading branch information
PhictionalOne committed Jun 4, 2024
1 parent 736ee11 commit c4ff8cd
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 68 deletions.
15 changes: 7 additions & 8 deletions extension/json/include/json_executors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ struct JSONExecutors {
}

//! Single-argument JSON functions that (partially) exposes yyjson functionality
static void
UnaryMutExecute(DataChunk &args, ExpressionState &state, Vector &result,
std::function<yyjson_mut_val *(yyjson_mut_val *, yyjson_mut_doc *, yyjson_alc *, Vector &)> fun) {
static void UnaryMutExecute(DataChunk &args, ExpressionState &state, Vector &result,
std::function<yyjson_mut_val *(yyjson_mut_val *, yyjson_alc *, Vector &)> fun) {
auto &lstate = JSONFunctionLocalState::ResetAndGet(state);
auto alc = lstate.json_allocator.GetYYAlc();

Expand All @@ -45,7 +44,7 @@ struct JSONExecutors {
auto mut_doc = yyjson_doc_mut_copy(doc, alc);

// compute mutable result
auto new_val = fun(mut_doc->root, mut_doc, alc, result);
auto new_val = fun(mut_doc->root, alc, result);

// Convert mutable value back to immutable document
doc = yyjson_mut_val_imut_copy(new_val, alc);
Expand Down Expand Up @@ -165,9 +164,9 @@ struct JSONExecutors {

//! Three-argument JSON manipulation function
template <class T1, class T2>
static void TernaryMutExecute(
DataChunk &args, ExpressionState &state, Vector &result,
std::function<yyjson_mut_val *(yyjson_mut_val *, yyjson_mut_doc *, T1, T2, yyjson_alc *, Vector &)> fun) {
static void
TernaryMutExecute(DataChunk &args, ExpressionState &state, Vector &result,
std::function<yyjson_mut_val *(yyjson_mut_val *, T1, T2, yyjson_alc *, Vector &)> fun) {
auto &lstate = JSONFunctionLocalState::ResetAndGet(state);
auto alc = lstate.json_allocator.GetYYAlc();

Expand All @@ -184,7 +183,7 @@ struct JSONExecutors {
auto mut_doc = yyjson_doc_mut_copy(doc, alc);

// Compute mutable value
auto new_val = fun(mut_doc->root, mut_doc, first, second, alc, result);
auto new_val = fun(mut_doc->root, first, second, alc, result);

// mutable value back to immutable document
doc = yyjson_mut_val_imut_copy(new_val, alc);
Expand Down
15 changes: 7 additions & 8 deletions extension/json/json_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,20 +336,19 @@ void JSONCommon::GetWildcardPath(yyjson_val *val, const char *ptr, const idx_t &

//! Determine Array index (>=0 from start, <0 from end)
size_t DetermineArrayIndex(yyjson_mut_val *arr, int64_t idx) {
// Determine index
auto array_size = yyjson_mut_arr_size(arr);
size_t index = static_cast<size_t>(idx);
size_t array_size = yyjson_mut_arr_size(arr);

// negative indexes address from the rear
if (idx < 0) {
index = static_cast<size_t>(static_cast<int64_t>(array_size) + idx);
idx += array_size;
}
// In case of |idx| > array_size clamp to 0
if (static_cast<int64_t>(index) < 0) {
index = 0;

// clamp to minimum 0
if (idx < 0) {
idx = 0;
}

return index;
return static_cast<size_t>(idx);
}

} // namespace duckdb
3 changes: 1 addition & 2 deletions extension/json/json_functions/json_array_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace duckdb {

//! Append String or JSON value to an array
yyjson_mut_val *ArrayAppendJSON(yyjson_mut_val *arr, yyjson_mut_doc *doc, string_t element, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ArrayAppendJSON(yyjson_mut_val *arr, string_t element, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}
Expand Down
4 changes: 2 additions & 2 deletions extension/json/json_functions/json_array_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace duckdb {

//! Insert String or JSON value to an array
yyjson_mut_val *ArrayInsertJSON(yyjson_mut_val *arr, yyjson_mut_doc *doc, string_t element, int64_t idx,
yyjson_alc *alc, Vector &result) {
yyjson_mut_val *ArrayInsertJSON(yyjson_mut_val *arr, string_t element, int64_t idx, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}

size_t index = DetermineArrayIndex(arr, idx);

// Fill remaining indeces with null until element index
auto doc = JSONCommon::CreateDocument(alc);
for (size_t entries = yyjson_mut_arr_size(arr); entries < index; ++entries) {
yyjson_mut_arr_add_null(doc, arr);
}
Expand Down
47 changes: 18 additions & 29 deletions extension/json/json_functions/json_array_remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,45 @@
namespace duckdb {

//! Remove element by its index (allows for negative indeces from the back)
yyjson_mut_val *ArrayRemoveElement(yyjson_mut_val *arr, yyjson_mut_doc *doc, int64_t idx, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ArrayRemoveElement(yyjson_mut_val *arr, int64_t idx, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
throw InvalidInputException("JSON input not a JSON Array");
}

size_t index = DetermineArrayIndex(arr, idx);

if (index == 0) {
yyjson_mut_arr_remove_first(arr);
return arr;
size_t arr_size = yyjson_mut_arr_size(arr);

if (index < arr_size) {
if (index == 0) {
yyjson_mut_arr_remove_first(arr);
} else {
yyjson_mut_arr_remove(arr, index);
}
}

if (index >= yyjson_mut_arr_size(arr)) {
return arr;
}

yyjson_mut_arr_remove(arr, index);
return arr;
}

//! Delete entry range from json array
yyjson_mut_val *ArrayRemoveRange(yyjson_mut_val *arr, yyjson_mut_doc *doc, int64_t start, int64_t end, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ArrayRemoveRange(yyjson_mut_val *arr, int64_t start, int64_t end, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}

if (start < 0 || end < 0) {
// TODO: Maybe introduce negative indices similar to list slicing
throw InvalidInputException("Only positives indices in range deletion");
}

if (start > end) {
throw InvalidInputException("Start index not smaller or equal to last index");
if (start < 0 || end < 0 || start > end) {
throw InvalidInputException("Invalid range indices");
}

size_t array_length = yyjson_mut_arr_size(arr);
size_t index = static_cast<size_t>(start);
size_t length = static_cast<size_t>(end - start + 1);

if (index > array_length) {
return arr;
}

if ((index + length) > array_length) {
length = array_length - index;
if (index < array_length) {
if (index + length > array_length) {
length = array_length - index;
}
yyjson_mut_arr_remove_range(arr, index, length);
}

yyjson_mut_arr_remove_range(arr, index, length);
return arr;
}

Expand Down
3 changes: 1 addition & 2 deletions extension/json/json_functions/json_array_rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace duckdb {

//! Rotate String or JSON value to an array
template <class AMOUNT_TYPE>
yyjson_mut_val *ArrayRotate(yyjson_mut_val *arr, yyjson_mut_doc *doc, AMOUNT_TYPE amount, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ArrayRotate(yyjson_mut_val *arr, AMOUNT_TYPE amount, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}
Expand Down
4 changes: 3 additions & 1 deletion extension/json/json_functions/json_array_tail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace duckdb {

yyjson_mut_val *JSONArrayTail(yyjson_mut_val *arr, yyjson_mut_doc *doc, yyjson_alc *alc, Vector &result) {
// yyjson_mut_val *JSONArrayTail(yyjson_mut_val *arr, yyjson_mut_doc *doc, yyjson_alc *alc, Vector &result) {
yyjson_mut_val *JSONArrayTail(yyjson_mut_val *arr, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}

if (yyjson_mut_arr_size(arr) == 0) {
auto doc = JSONCommon::CreateDocument(alc);
return yyjson_mut_arr(doc);
}

Expand Down
16 changes: 6 additions & 10 deletions extension/json/json_functions/json_array_take.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace duckdb {

//! Take n elements.
yyjson_mut_val *ArrayTakeElement(yyjson_mut_val *arr, yyjson_mut_doc *doc, int64_t n, yyjson_alc *alc, Vector &result) {
yyjson_mut_val *ArrayTakeElement(yyjson_mut_val *arr, int64_t n, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}

if (n <= 0) {
// TODO: Maybe introduce negative space to take from the end?
auto doc = JSONCommon::CreateDocument(alc);
return yyjson_mut_arr(doc);
}

Expand All @@ -25,26 +26,21 @@ yyjson_mut_val *ArrayTakeElement(yyjson_mut_val *arr, yyjson_mut_doc *doc, int64
}

//! Take entry range from array
yyjson_mut_val *ArrayTakeRange(yyjson_mut_val *arr, yyjson_mut_doc *doc, int64_t start, int64_t end, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ArrayTakeRange(yyjson_mut_val *arr, int64_t start, int64_t end, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_arr(arr)) {
throw InvalidInputException("JSON input not an JSON Array");
}

if (start < 0 || end < 0) {
// TODO: Maybe introduce negative indices similar to list slicing
throw InvalidInputException("Only positives indices in range selection");
}

if (start > end) {
throw InvalidInputException("Start index not smaller or equal to last index");
if (start < 0 || end < 0 || start > end) {
throw InvalidInputException("Invalid range indices");
}

size_t array_length = yyjson_mut_arr_size(arr);
size_t first_index = static_cast<size_t>(start);
size_t last_index = static_cast<size_t>(end + 1);

if (first_index > array_length) {
auto doc = JSONCommon::CreateDocument(alc);
return yyjson_mut_arr(doc);
}

Expand Down
5 changes: 3 additions & 2 deletions extension/json/json_functions/json_object_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace duckdb {

//! Add a JSON Object or String to an object
yyjson_mut_val *ObjectAddJSON(yyjson_mut_val *obj, yyjson_mut_doc *doc, string_t key, string_t value, yyjson_alc *alc,
Vector &result) {
yyjson_mut_val *ObjectAddJSON(yyjson_mut_val *obj, string_t key, string_t value, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_obj(obj)) {
throw InvalidInputException("JSON input not an JSON Object");
}

auto doc = JSONCommon::CreateDocument(alc);

const char *_key = key.GetDataWriteable();
auto mut_key = yyjson_mut_strcpy(doc, _key);

Expand Down
5 changes: 3 additions & 2 deletions extension/json/json_functions/json_object_rename_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace duckdb {

//! Rename all existing matching key into the new given key
yyjson_mut_val *ObjectRenameKey(yyjson_mut_val *obj, yyjson_mut_doc *doc, string_t key, string_t new_key,
yyjson_alc *alc, Vector &result) {
yyjson_mut_val *ObjectRenameKey(yyjson_mut_val *obj, string_t key, string_t new_key, yyjson_alc *alc, Vector &result) {
if (!yyjson_mut_is_obj(obj)) {
throw InvalidInputException("JSON input not an JSON Object");
}

auto doc = JSONCommon::CreateDocument(alc);

const char *_key = key.GetDataWriteable();
auto mut_key = yyjson_mut_strcpy(doc, _key);
auto k = yyjson_mut_get_str(mut_key);
Expand Down
5 changes: 3 additions & 2 deletions extension/json/json_functions/json_object_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace duckdb {

//! Replace String or JSON value of a key in JSON object
yyjson_mut_val *ObjectReplaceJSON(yyjson_mut_val *obj, yyjson_mut_doc *doc, string_t key, string_t element,
yyjson_alc *alc, Vector &result) {
yyjson_mut_val *ObjectReplaceJSON(yyjson_mut_val *obj, string_t key, string_t element, yyjson_alc *alc,
Vector &result) {
if (!yyjson_mut_is_obj(obj)) {
throw InvalidInputException("JSON input not an JSON Object");
}

const char *_key = key.GetDataWriteable();
auto doc = JSONCommon::CreateDocument(alc);
auto mut_key = yyjson_mut_strcpy(doc, _key);

auto edoc = JSONCommon::ReadDocument(element, JSONCommon::READ_FLAG, alc);
Expand Down

0 comments on commit c4ff8cd

Please sign in to comment.