Skip to content

Commit

Permalink
Add helper functions for arrays
Browse files Browse the repository at this point in the history
These will see more use in follow-up patches that adjust how
compression settings are stored in our catalog.
  • Loading branch information
svenklemm committed Nov 17, 2023
1 parent 485383a commit a3aec8c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/planner/expand_hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@
#include <utils/fmgrprotos.h>
#include <utils/syscache.h>

#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
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/ts_catalog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
91 changes: 91 additions & 0 deletions src/ts_catalog/array_utils.c
Original file line number Diff line number Diff line change
@@ -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 <postgres.h>
#include <utils/array.h>
#include <utils/builtins.h>

#include <debug_assert.h>
#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;
}
20 changes: 20 additions & 0 deletions src/ts_catalog/array_utils.h
Original file line number Diff line number Diff line change
@@ -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 <postgres.h>
#include <utils/array.h>

#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);
3 changes: 2 additions & 1 deletion tsl/src/remote/dist_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "errors.h"
#include "deparse.h"
#include "debug_point.h"
#include "ts_catalog/array_utils.h"

typedef struct DistPreparedStmt
{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a3aec8c

Please sign in to comment.