From a3aec8c209412ea03b1491cffce544632b32bf34 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Fri, 17 Nov 2023 14:51:03 +0100 Subject: [PATCH] Add helper functions for arrays These will see more use in follow-up patches that adjust how compression settings are stored in our catalog. --- src/planner/expand_hypertable.c | 9 ++-- src/ts_catalog/CMakeLists.txt | 1 + src/ts_catalog/array_utils.c | 91 +++++++++++++++++++++++++++++++++ src/ts_catalog/array_utils.h | 20 ++++++++ tsl/src/remote/dist_commands.c | 3 +- 5 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 src/ts_catalog/array_utils.c create mode 100644 src/ts_catalog/array_utils.h diff --git a/src/planner/expand_hypertable.c b/src/planner/expand_hypertable.c index befba9ecd5c..dcfc698870b 100644 --- a/src/planner/expand_hypertable.c +++ b/src/planner/expand_hypertable.c @@ -44,20 +44,21 @@ #include #include -#include "chunk.h" #include "compat/compat.h" +#include "chunk.h" #include "cross_module_fn.h" -#include "extension.h" #include "extension_constants.h" +#include "extension.h" #include "guc.h" #include "hypertable.h" #include "hypertable_restrict_info.h" #include "import/planner.h" #include "nodes/chunk_append/chunk_append.h" -#include "partitioning.h" #include "partialize.h" +#include "partitioning.h" #include "planner.h" #include "time_utils.h" +#include "ts_catalog/array_utils.h" typedef struct CollectQualCtx { @@ -992,7 +993,7 @@ get_explicit_chunks(CollectQualCtx *ctx, PlannerInfo *root, RelOptInfo *rel, Hyp (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid number of array dimensions for chunks_in"))); - chunk_id_arr_size = ArrayGetNItems(ARR_NDIM(chunk_id_arr), ARR_DIMS(chunk_id_arr)); + chunk_id_arr_size = ts_array_length(chunk_id_arr); if (chunk_id_arr_size == 0) return NULL; diff --git a/src/ts_catalog/CMakeLists.txt b/src/ts_catalog/CMakeLists.txt index 155a8c2fb58..5a483f6f2ff 100644 --- a/src/ts_catalog/CMakeLists.txt +++ b/src/ts_catalog/CMakeLists.txt @@ -1,4 +1,5 @@ set(SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/array_utils.c ${CMAKE_CURRENT_SOURCE_DIR}/catalog.c ${CMAKE_CURRENT_SOURCE_DIR}/chunk_data_node.c ${CMAKE_CURRENT_SOURCE_DIR}/compression_chunk_size.c diff --git a/src/ts_catalog/array_utils.c b/src/ts_catalog/array_utils.c new file mode 100644 index 00000000000..4463b57283b --- /dev/null +++ b/src/ts_catalog/array_utils.c @@ -0,0 +1,91 @@ +/* + * This file and its contents are licensed under the Apache License 2.0. + * Please see the included NOTICE for copyright information and + * LICENSE-APACHE for a copy of the license. + */ +#include +#include +#include + +#include +#include "array_utils.h" + +extern TSDLLEXPORT int +ts_array_length(ArrayType *arr) +{ + return ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); +} + +/* + * Array helper function for internal catalog arrays. + * These are not suitable for arbitrary dimension + * arrays but only for 1-dimensional arrays as we use + * them in our catalog. + */ + +extern TSDLLEXPORT bool +ts_array_is_member(ArrayType *arr, const char *name) +{ + bool ret = false; + Datum datum; + bool null; + if (!arr) + return ret; + + Assert(ARR_NDIM(arr) == 1); + + ArrayIterator it = array_create_iterator(arr, 0, NULL); + while (array_iterate(it, &datum, &null)) + { + Assert(!null); + /* + * Our internal catalog arrays should either be NULL or + * have non-NULL members. During normal operation it should + * never have NULL members. If we have NULL members either + * the catalog is corrupted or some catalog tampering has + * happened. + */ + Ensure(!null, "array element was NULL"); + if (strncmp(TextDatumGetCString(datum), name, NAMEDATALEN) == 0) + { + ret = true; + break; + } + } + + array_free_iterator(it); + return ret; +} + +extern TSDLLEXPORT int +ts_array_position(ArrayType *arr, const char *name) +{ + int pos = 0; + Datum datum; + bool null; + if (!arr) + return pos; + + Assert(ARR_NDIM(arr) == 1); + + ArrayIterator it = array_create_iterator(arr, 0, NULL); + while (array_iterate(it, &datum, &null)) + { + pos++; + /* + * Our internal catalog arrays should either be NULL or + * have non-NULL members. During normal operation it should + * never have NULL members. If we have NULL members either + * the catalog is corrupted or some catalog tampering has + * happened. + */ + Ensure(!null, "array element was NULL"); + if (strncmp(TextDatumGetCString(datum), name, NAMEDATALEN) == 0) + { + break; + } + } + + array_free_iterator(it); + return pos; +} diff --git a/src/ts_catalog/array_utils.h b/src/ts_catalog/array_utils.h new file mode 100644 index 00000000000..ccd0ca57c07 --- /dev/null +++ b/src/ts_catalog/array_utils.h @@ -0,0 +1,20 @@ +/* + * This file and its contents are licensed under the Apache License 2.0. + * Please see the included NOTICE for copyright information and + * LICENSE-APACHE for a copy of the license. + */ +#include +#include + +#include "export.h" + +/* + * Array helper function for internal catalog arrays. + * These are not suitable for arbitrary dimension + * arrays but only for 1-dimensional arrays as we use + * them in our catalog. + */ + +extern TSDLLEXPORT int ts_array_length(ArrayType *arr); +extern TSDLLEXPORT bool ts_array_is_member(ArrayType *arr, const char *name); +extern TSDLLEXPORT int ts_array_position(ArrayType *arr, const char *name); diff --git a/tsl/src/remote/dist_commands.c b/tsl/src/remote/dist_commands.c index 92e3efd0fc6..f07599d6795 100644 --- a/tsl/src/remote/dist_commands.c +++ b/tsl/src/remote/dist_commands.c @@ -23,6 +23,7 @@ #include "errors.h" #include "deparse.h" #include "debug_point.h" +#include "ts_catalog/array_utils.h" typedef struct DistPreparedStmt { @@ -566,7 +567,7 @@ ts_dist_cmd_exec(PG_FUNCTION_ARGS) errmsg("invalid data nodes list"), errdetail("The array of data nodes cannot contain null values."))); - ndatanodes = ArrayGetNItems(ARR_NDIM(data_nodes), ARR_DIMS(data_nodes)); + ndatanodes = ts_array_length(data_nodes); if (ndatanodes == 0) ereport(ERROR,