-
Notifications
You must be signed in to change notification settings - Fork 2
Add kv_list and refactor session_settings with it #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+242
−40
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #ifndef PG_CLICKHOUSE_KV_LIST_H | ||
| #define PG_CLICKHOUSE_KV_LIST_H | ||
|
|
||
| #include <stdbool.h> | ||
| #include "postgres.h" | ||
| #include "nodes/pathnodes.h" | ||
|
|
||
| /* | ||
| * A simple data structure with a list of key/value string pairs. Use | ||
| * new_kv_list_from_list() to create. | ||
| */ | ||
| typedef struct kv_list | ||
| { | ||
| int length; | ||
| /* key/value char * pairs follow the length field. */ | ||
| char data[]; | ||
| } kv_list; | ||
|
|
||
| /* | ||
| * Iterator for a kv_list. Use new_kv_iter() to create. | ||
| * | ||
| * for (kv_iter iter = new_kv_iter(kv); !kv_iter_done(&iter); kv_iter_next(&iter)) | ||
| * { | ||
| * printf("%i, %s => %s\n", iter.num, iter.name, iter.value); | ||
| * } | ||
| */ | ||
| typedef struct kv_iter { | ||
| int togo; | ||
| char * name; | ||
| char * value; | ||
| } kv_iter; | ||
|
|
||
| /* | ||
| * Defines the allocator to use when creating a new kv_list. | ||
| * kv_pair_guc_malloc is the same as kv_pair_malloc on Postgres 15 and | ||
| * earlier, so be sure to free() the memory on those versions. | ||
| */ | ||
| enum kv_pair_alloc | ||
| { | ||
| kv_pair_guc_malloc, | ||
| kv_pair_malloc, | ||
| kv_pair_palloc, | ||
| }; | ||
|
|
||
| /* | ||
| * Create a new kv_list from a PostgreSQL List of DefElem. Allocate the memory | ||
| * using the specified allocator. | ||
| */ | ||
| kv_list * new_kv_list_from_pg_list(List * list, int allocate); | ||
|
|
||
| /* Create a new kv_iter for a key_pairs. */ | ||
| kv_iter new_kv_iter(const kv_list * ns); | ||
|
|
||
| /* Iterate to the next item. Returns false if there are no items. */ | ||
| bool kv_iter_next(kv_iter * state); | ||
|
|
||
| /* Retruns true if iteration by kv_iter is complete. */ | ||
| bool kv_iter_done(kv_iter * state); | ||
|
|
||
| #endif /* PG_CLICKHOUSE_KV_LIST_H */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #include <stddef.h> | ||
| #include "kv_list.h" | ||
| #include "utils/elog.h" | ||
| #include "nodes/pathnodes.h" | ||
| #include "utils/guc.h" | ||
|
|
||
| static kv_list * allocate_list(size_t size, int allocate) | ||
| { | ||
| kv_list *pairs; | ||
|
|
||
| switch (allocate) | ||
| { | ||
| case kv_pair_guc_malloc: | ||
| /* Fall through to malloc prior to Postgres 16. */ | ||
| #if PG_VERSION_NUM >= 160000 | ||
| pairs = guc_malloc(ERROR, size); | ||
| break; | ||
| #endif | ||
| case kv_pair_malloc: | ||
| pairs = malloc(size); | ||
| break; | ||
| case kv_pair_palloc: | ||
| pairs = palloc(size); | ||
| break; | ||
| default: | ||
| ereport(ERROR, | ||
| (errcode(ERRCODE_FDW_ERROR), | ||
| errmsg("unknown kv_pair_alloc %i", allocate))); | ||
| } | ||
|
|
||
| if (!pairs) | ||
| ereport(ERROR, | ||
| (errcode(ERRCODE_FDW_OUT_OF_MEMORY), | ||
| errmsg("out of memory"))); | ||
|
|
||
| return pairs; | ||
| } | ||
|
|
||
| extern kv_list * new_kv_list_from_pg_list(List * list, int allocate) | ||
| { | ||
| ListCell *lc; | ||
| DefElem *elem; | ||
| kv_list *pairs; | ||
| size_t kv_size = 0; | ||
| size_t ck_size = 0; | ||
|
|
||
| /* Count up space for dynamic key/value pairs. */ | ||
| foreach(lc, list) | ||
| { | ||
| elem = (DefElem *) lfirst(lc); | ||
| kv_size += 2 + strlen(elem->defname) + strlen(strVal(elem->arg)); | ||
| } | ||
|
|
||
| /* Alloc the result ... */ | ||
| pairs = allocate_list(offsetof(kv_list, data) + kv_size, allocate); | ||
|
|
||
| /* ... and fill it in */ | ||
| pairs->length = list_length(list); | ||
|
|
||
| /* In this loop, size ck_size reprises the kv_size calculation above. */ | ||
| foreach(lc, list) | ||
| { | ||
| char *str; | ||
|
|
||
| /* Append the element name and arg that constitute the pair. */ | ||
| elem = (DefElem *) lfirst(lc); | ||
| str = (char *) pairs->data + ck_size; | ||
| strcpy(str, elem->defname); | ||
| ck_size += strlen(str) + 1; | ||
| str = (char *) pairs->data + ck_size; | ||
| strcpy(str, strVal(elem->arg)); | ||
| ck_size += strlen(str) + 1; | ||
| } | ||
|
|
||
| /* Assert the two loops agreed on size calculations. */ | ||
| Assert(kv_size == ck_size); | ||
|
|
||
| return pairs; | ||
| } | ||
|
|
||
| extern kv_iter new_kv_iter(const kv_list * ns) | ||
| { | ||
| char *name; | ||
|
|
||
| /* The list may be NULL or empty. */ | ||
| if (!ns || ns->length < 1) | ||
| return (kv_iter) | ||
| { | ||
| 0, | ||
| }; | ||
|
|
||
| /* Grab the number of pairs point to the first name and value. */ | ||
| name = (char *) ns->data; | ||
| return (kv_iter) | ||
| { | ||
| ns->length, name, name + strlen(name) + 1 | ||
| }; | ||
| } | ||
|
|
||
| extern bool | ||
| kv_iter_next(kv_iter * iter) | ||
| { | ||
| if (iter->togo == 0) | ||
| return false; | ||
|
|
||
| /* Point to the the next name and value. */ | ||
| iter->togo--; | ||
| iter->name = iter->value + strlen(iter->value) + 1; | ||
| iter->value = iter->name + strlen(iter->name) + 1; | ||
| return true; | ||
| } | ||
|
|
||
| extern bool | ||
| kv_iter_done(kv_iter * iter) | ||
| { | ||
| return iter->togo == 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.