From 05f90b30d3f5090e2e586c7fbfc02793c1269d97 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 10:40:02 +0300 Subject: [PATCH 01/16] Set INVALID_COLOCATION_ID if colocationId doesn't exist in the annotation. --- src/backend/distributed/utils/attribute.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 30d070c0a..d540576c5 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -16,6 +16,7 @@ #include "distributed/listutils.h" #include "distributed/jsonbutils.h" #include "distributed/tuplestore.h" +#include "distributed/colocation_utils.h" #include "executor/execdesc.h" #include "storage/ipc.h" #include "storage/lwlock.h" @@ -193,7 +194,7 @@ AttributeQueryIfAnnotated(const char *query_string, CmdType commandType) strcpy_s(attributeToTenant, sizeof(attributeToTenant), tenantId); } - colocationGroupId = ExtractFieldInt32(jsonbDatum, "cId", 0); + colocationGroupId = ExtractFieldInt32(jsonbDatum, "cId", INVALID_COLOCATION_ID); if (MultiTenantMonitoringLogLevel != CITUS_LOG_LEVEL_OFF) { From c8ed4d174acc30e9908e712a74be66e151d1c4d6 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 10:54:07 +0300 Subject: [PATCH 02/16] Renamings --- src/backend/distributed/utils/attribute.c | 46 ++++++++++++----------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index d540576c5..96bd2f3b1 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -194,7 +194,8 @@ AttributeQueryIfAnnotated(const char *query_string, CmdType commandType) strcpy_s(attributeToTenant, sizeof(attributeToTenant), tenantId); } - colocationGroupId = ExtractFieldInt32(jsonbDatum, "cId", INVALID_COLOCATION_ID); + colocationGroupId = ExtractFieldInt32(jsonbDatum, "cId", + INVALID_COLOCATION_ID); if (MultiTenantMonitoringLogLevel != CITUS_LOG_LEVEL_OFF) { @@ -675,21 +676,22 @@ Substring(const char *str, int start, int end) static char * EscapeCommentChars(const char *str) { - int len = strlen(str); - char *new_str = (char *) malloc(len * 2 + 1); - int j = 0; + int originalStringLength = strlen(str); + char *escapedString = (char *) malloc(originalStringLength * 2 + 1); + int escapedStringIndex = 0; - for (int i = 0; i < len; i++) + for (int originalStringIndex = 0; originalStringIndex < originalStringLength; + originalStringIndex++) { - if (str[i] == '*' || str[i] == '/') + if (str[originalStringIndex] == '*' || str[originalStringIndex] == '/') { - new_str[j++] = '\\'; + escapedString[escapedStringIndex++] = '\\'; } - new_str[j++] = str[i]; + escapedString[escapedStringIndex++] = str[originalStringIndex]; } - new_str[j] = '\0'; + escapedString[escapedStringIndex] = '\0'; - return new_str; + return escapedString; } @@ -697,22 +699,22 @@ EscapeCommentChars(const char *str) static char * UnescapeCommentChars(const char *str) { - int len = strlen(str); - char *new_str = (char *) malloc(len + 1); - int j = 0; + int originalStringLength = strlen(str); + char *unescapedString = (char *) malloc(originalStringLength + 1); + int unescapedStringIndex = 0; - for (int i = 0; i < len; i++) + for (int originalStringindex = 0; originalStringindex < originalStringLength; + originalStringindex++) { - if (str[i] == '\\' && i < len - 1) + if (str[originalStringindex] == '\\' && originalStringindex < + originalStringLength - 1 && (str[originalStringindex + 1] == '*' || + str[originalStringindex + 1] == '/')) { - if (str[i + 1] == '*' || str[i + 1] == '/') - { - i++; - } + originalStringindex++; } - new_str[j++] = str[i]; + unescapedString[unescapedStringIndex++] = str[originalStringindex]; } - new_str[j] = '\0'; + unescapedString[unescapedStringIndex] = '\0'; - return new_str; + return unescapedString; } From ae276e70ab6cc219dd90eb7142fca6a1f48599e3 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 11:00:37 +0300 Subject: [PATCH 03/16] Remove unnecessary check --- src/backend/distributed/utils/attribute.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 96bd2f3b1..707a6f7a2 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -627,8 +627,7 @@ ExtractTopComment(const char *inputString) { /* Skip the comment start characters */ commentEndCharsIndex += commentStartCharsLength; - while (inputString[commentEndCharsIndex] && - commentEndCharsIndex < inputStringLen && + while (commentEndCharsIndex < inputStringLen && !(inputString[commentEndCharsIndex] == '*' && inputString [commentEndCharsIndex + 1] == '/')) { @@ -708,7 +707,8 @@ UnescapeCommentChars(const char *str) { if (str[originalStringindex] == '\\' && originalStringindex < originalStringLength - 1 && (str[originalStringindex + 1] == '*' || - str[originalStringindex + 1] == '/')) + str + [originalStringindex + 1] == '/')) { originalStringindex++; } From de527dbb7189bacb58fbcc92b4853846b3be5277 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 11:02:52 +0300 Subject: [PATCH 04/16] Refactoring to reduce nesting --- src/backend/distributed/utils/attribute.c | 24 ++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 707a6f7a2..5178b2769 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -621,18 +621,20 @@ ExtractTopComment(const char *inputString) int commentEndCharsIndex = 0; - /* If query starts with a comment */ - if (inputString[commentEndCharsIndex] == '/' && - inputString[commentEndCharsIndex + 1] == '*') + /* If query doesn't start with a comment, return NULL */ + if (inputString[commentEndCharsIndex] != '/' || + inputString[commentEndCharsIndex + 1] != '*') { - /* Skip the comment start characters */ - commentEndCharsIndex += commentStartCharsLength; - while (commentEndCharsIndex < inputStringLen && - !(inputString[commentEndCharsIndex] == '*' && - inputString [commentEndCharsIndex + 1] == '/')) - { - commentEndCharsIndex++; - } + return NULL; + } + + /* Skip the comment start characters */ + commentEndCharsIndex += commentStartCharsLength; + while (commentEndCharsIndex < inputStringLen && + !(inputString[commentEndCharsIndex] == '*' && + inputString [commentEndCharsIndex + 1] == '/')) + { + commentEndCharsIndex++; } if (commentEndCharsIndex > commentStartCharsLength) From 707403530719398d2463516d77fa17ae43ca80f8 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 11:31:12 +0300 Subject: [PATCH 05/16] Indent --- src/backend/distributed/utils/attribute.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 5178b2769..dce644644 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -631,8 +631,8 @@ ExtractTopComment(const char *inputString) /* Skip the comment start characters */ commentEndCharsIndex += commentStartCharsLength; while (commentEndCharsIndex < inputStringLen && - !(inputString[commentEndCharsIndex] == '*' && - inputString [commentEndCharsIndex + 1] == '/')) + !(inputString[commentEndCharsIndex] == '*' && + inputString [commentEndCharsIndex + 1] == '/')) { commentEndCharsIndex++; } @@ -710,7 +710,8 @@ UnescapeCommentChars(const char *str) if (str[originalStringindex] == '\\' && originalStringindex < originalStringLength - 1 && (str[originalStringindex + 1] == '*' || str - [originalStringindex + 1] == '/')) + [ + originalStringindex + 1] == '/')) { originalStringindex++; } From bee5b6d58f12fbf6ad8af0c9dfc6fb7bdefadce1 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Thu, 16 Mar 2023 14:52:36 +0300 Subject: [PATCH 06/16] Indent --- src/backend/distributed/utils/attribute.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index dce644644..ecad5ec7d 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -707,11 +707,10 @@ UnescapeCommentChars(const char *str) for (int originalStringindex = 0; originalStringindex < originalStringLength; originalStringindex++) { - if (str[originalStringindex] == '\\' && originalStringindex < - originalStringLength - 1 && (str[originalStringindex + 1] == '*' || - str - [ - originalStringindex + 1] == '/')) + if (str[originalStringindex] == '\\' && + originalStringindex < originalStringLength - 1 && + (str[originalStringindex + 1] == '*' || + str[originalStringindex + 1] == '/')) { originalStringindex++; } From 5929fda143fae26cc8f88834b2cd6d0f1ef03c7d Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 08:15:11 +0300 Subject: [PATCH 07/16] Use palloc instead of malloc --- src/backend/distributed/utils/attribute.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index ecad5ec7d..9a839a6cb 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -678,7 +678,7 @@ static char * EscapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *escapedString = (char *) malloc(originalStringLength * 2 + 1); + char *escapedString = (char *) palloc(originalStringLength * 2 + 1); int escapedStringIndex = 0; for (int originalStringIndex = 0; originalStringIndex < originalStringLength; @@ -701,7 +701,7 @@ static char * UnescapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *unescapedString = (char *) malloc(originalStringLength + 1); + char *unescapedString = (char *) palloc(originalStringLength + 1); int unescapedStringIndex = 0; for (int originalStringindex = 0; originalStringindex < originalStringLength; From 9531cfd3bf4d9950e3593eed90f092418bb6ff4b Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 08:46:22 +0300 Subject: [PATCH 08/16] Use text_substr for getting top comment --- src/backend/distributed/utils/attribute.c | 32 +++-------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 9a839a6cb..d3567619c 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -63,7 +63,6 @@ static int CreateTenantStats(MultiTenantMonitor *monitor); static int FindTenantStats(MultiTenantMonitor *monitor); static size_t MultiTenantMonitorshmemSize(void); static char * ExtractTopComment(const char *inputString); -static char * Substring(const char *str, int start, int end); static char * EscapeCommentChars(const char *str); static char * UnescapeCommentChars(const char *str); @@ -639,7 +638,10 @@ ExtractTopComment(const char *inputString) if (commentEndCharsIndex > commentStartCharsLength) { - return Substring(inputString, commentStartCharsLength, commentEndCharsIndex); + Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum(inputString), + Int32GetDatum(commentStartCharsLength), + Int32GetDatum(commentEndCharsIndex - commentStartCharsLength)); + return TextDatumGetCString(substringTextDatum); } else { @@ -647,32 +649,6 @@ ExtractTopComment(const char *inputString) } } - -/* Extracts a substring from the input string between the specified start and end indices.*/ -static char * -Substring(const char *str, int start, int end) -{ - int len = strlen(str); - - /* Ensure start and end are within the bounds of the string */ - if (start < 0 || end > len || start > end) - { - return NULL; - } - - /* Allocate memory for the substring */ - char *substr = (char *) palloc((end - start + 1) * sizeof(char)); - - /* Copy the substring to the new memory location */ - strncpy_s(substr, end - start + 1, str + start, end - start); - - /* Add null terminator to end the substring */ - substr[end - start] = '\0'; - - return substr; -} - - /* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */ static char * EscapeCommentChars(const char *str) From f972b6fae9b3ed23392186934098f244cd04b272 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 08:46:52 +0300 Subject: [PATCH 09/16] Handle no comment end chars --- src/backend/distributed/utils/attribute.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index d3567619c..f2583da2c 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -636,6 +636,13 @@ ExtractTopComment(const char *inputString) commentEndCharsIndex++; } + /* If there is no end of comment chars , return NULL */ + if (inputString[commentEndCharsIndex] != '*' && + inputString[commentEndCharsIndex + 1] != '/') + { + return NULL; + } + if (commentEndCharsIndex > commentStartCharsLength) { Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum(inputString), From df3ad5c4f6845ff3888cdee990310e840752ff08 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 08:49:39 +0300 Subject: [PATCH 10/16] Indent --- src/backend/distributed/utils/attribute.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index f2583da2c..34054be71 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -645,9 +645,13 @@ ExtractTopComment(const char *inputString) if (commentEndCharsIndex > commentStartCharsLength) { - Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum(inputString), - Int32GetDatum(commentStartCharsLength), - Int32GetDatum(commentEndCharsIndex - commentStartCharsLength)); + Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum( + inputString), + Int32GetDatum( + commentStartCharsLength), + Int32GetDatum( + commentEndCharsIndex - + commentStartCharsLength)); return TextDatumGetCString(substringTextDatum); } else @@ -656,6 +660,7 @@ ExtractTopComment(const char *inputString) } } + /* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */ static char * EscapeCommentChars(const char *str) From 34e1dafb181327ccc5571c60e74f9d17d318fea6 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 09:40:08 +0300 Subject: [PATCH 11/16] Convert char* to text for text_substr call --- src/backend/distributed/utils/attribute.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 34054be71..dfa6951ed 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -646,9 +646,9 @@ ExtractTopComment(const char *inputString) if (commentEndCharsIndex > commentStartCharsLength) { Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum( - inputString), + cstring_to_text(inputString)), Int32GetDatum( - commentStartCharsLength), + commentStartCharsLength + 1), Int32GetDatum( commentEndCharsIndex - commentStartCharsLength)); @@ -666,7 +666,7 @@ static char * EscapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *escapedString = (char *) palloc(originalStringLength * 2 + 1); + char *escapedString = (char *) palloc0((originalStringLength * 2 + 1) * sizeof(char)); int escapedStringIndex = 0; for (int originalStringIndex = 0; originalStringIndex < originalStringLength; @@ -689,7 +689,7 @@ static char * UnescapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *unescapedString = (char *) palloc(originalStringLength + 1); + char *unescapedString = (char *) palloc0((originalStringLength + 1) * sizeof(char)); int unescapedStringIndex = 0; for (int originalStringindex = 0; originalStringindex < originalStringLength; From 1df9260f825202fce26a49c32488cbeb37a5d615 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Mon, 20 Mar 2023 11:02:34 +0300 Subject: [PATCH 12/16] Revert "Use text_substr for getting top comment" This reverts commit 9531cfd3bf4d9950e3593eed90f092418bb6ff4b. --- src/backend/distributed/utils/attribute.c | 35 +++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index dfa6951ed..4761aef02 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -63,6 +63,7 @@ static int CreateTenantStats(MultiTenantMonitor *monitor); static int FindTenantStats(MultiTenantMonitor *monitor); static size_t MultiTenantMonitorshmemSize(void); static char * ExtractTopComment(const char *inputString); +static char * Substring(const char *str, int start, int end); static char * EscapeCommentChars(const char *str); static char * UnescapeCommentChars(const char *str); @@ -645,14 +646,7 @@ ExtractTopComment(const char *inputString) if (commentEndCharsIndex > commentStartCharsLength) { - Datum substringTextDatum = DirectFunctionCall3(text_substr, PointerGetDatum( - cstring_to_text(inputString)), - Int32GetDatum( - commentStartCharsLength + 1), - Int32GetDatum( - commentEndCharsIndex - - commentStartCharsLength)); - return TextDatumGetCString(substringTextDatum); + return Substring(inputString, commentStartCharsLength, commentEndCharsIndex); } else { @@ -661,6 +655,31 @@ ExtractTopComment(const char *inputString) } +/* Extracts a substring from the input string between the specified start and end indices.*/ +static char * +Substring(const char *str, int start, int end) +{ + int len = strlen(str); + + /* Ensure start and end are within the bounds of the string */ + if (start < 0 || end > len || start > end) + { + return NULL; + } + + /* Allocate memory for the substring */ + char *substr = (char *) palloc((end - start + 1) * sizeof(char)); + + /* Copy the substring to the new memory location */ + strncpy_s(substr, end - start + 1, str + start, end - start); + + /* Add null terminator to end the substring */ + substr[end - start] = '\0'; + + return substr; +} + + /* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */ static char * EscapeCommentChars(const char *str) From 3f61a62420e303991ed42d77566428621da27084 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Tue, 21 Mar 2023 11:22:08 +0300 Subject: [PATCH 13/16] Add an additional comment Co-authored-by: Jelte Fennema --- src/backend/distributed/utils/attribute.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 4761aef02..dd0a95f13 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -630,6 +630,8 @@ ExtractTopComment(const char *inputString) /* Skip the comment start characters */ commentEndCharsIndex += commentStartCharsLength; + + /* Find the first comment end character */ while (commentEndCharsIndex < inputStringLen && !(inputString[commentEndCharsIndex] == '*' && inputString [commentEndCharsIndex + 1] == '/')) From 26226d0eb4abaa38d17a39b80f2e6a4ecd363709 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Tue, 21 Mar 2023 11:22:29 +0300 Subject: [PATCH 14/16] Update comment Co-authored-by: Jelte Fennema --- src/backend/distributed/utils/attribute.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index dd0a95f13..d53ea14b6 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -675,7 +675,7 @@ Substring(const char *str, int start, int end) /* Copy the substring to the new memory location */ strncpy_s(substr, end - start + 1, str + start, end - start); - /* Add null terminator to end the substring */ + /* Add null terminator to end of the substring */ substr[end - start] = '\0'; return substr; From 16c75165cf2a783ea06249eff3dfd8a431098e78 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Tue, 21 Mar 2023 09:58:31 +0300 Subject: [PATCH 15/16] Use stringinfo for escaping/unescaping --- src/backend/distributed/utils/attribute.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index d53ea14b6..04647e619 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -25,6 +25,7 @@ #include "utils/json.h" #include "distributed/utils/attribute.h" #include "common/base64.h" +#include "miscadmin.h" #include @@ -687,31 +688,28 @@ static char * EscapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *escapedString = (char *) palloc0((originalStringLength * 2 + 1) * sizeof(char)); - int escapedStringIndex = 0; + StringInfo escapedString = makeStringInfo(); for (int originalStringIndex = 0; originalStringIndex < originalStringLength; originalStringIndex++) { if (str[originalStringIndex] == '*' || str[originalStringIndex] == '/') { - escapedString[escapedStringIndex++] = '\\'; + appendStringInfoChar(escapedString, '\\'); } - escapedString[escapedStringIndex++] = str[originalStringIndex]; - } - escapedString[escapedStringIndex] = '\0'; - return escapedString; + appendStringInfoChar(escapedString, str[originalStringIndex]); + } + + return escapedString->data; } - /* UnescapeCommentChars removes the backslash that precedes '*' or '/' in the input string. */ static char * UnescapeCommentChars(const char *str) { int originalStringLength = strlen(str); - char *unescapedString = (char *) palloc0((originalStringLength + 1) * sizeof(char)); - int unescapedStringIndex = 0; + StringInfo unescapedString = makeStringInfo(); for (int originalStringindex = 0; originalStringindex < originalStringLength; originalStringindex++) @@ -723,9 +721,8 @@ UnescapeCommentChars(const char *str) { originalStringindex++; } - unescapedString[unescapedStringIndex++] = str[originalStringindex]; + appendStringInfoChar(unescapedString, str[originalStringindex]); } - unescapedString[unescapedStringIndex] = '\0'; - return unescapedString; + return unescapedString->data; } From 852db8f2f91afae6fb863a536b32960b0d98a569 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Tue, 21 Mar 2023 12:34:05 +0300 Subject: [PATCH 16/16] Rewrite ExtractTopComment by using strstr() and stringinfo --- src/backend/distributed/utils/attribute.c | 96 +++++++---------------- 1 file changed, 30 insertions(+), 66 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 04647e619..f2d3fc6a2 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -24,8 +24,7 @@ #include "utils/builtins.h" #include "utils/json.h" #include "distributed/utils/attribute.h" -#include "common/base64.h" -#include "miscadmin.h" + #include @@ -64,7 +63,6 @@ static int CreateTenantStats(MultiTenantMonitor *monitor); static int FindTenantStats(MultiTenantMonitor *monitor); static size_t MultiTenantMonitorshmemSize(void); static char * ExtractTopComment(const char *inputString); -static char * Substring(const char *str, int start, int end); static char * EscapeCommentChars(const char *str); static char * UnescapeCommentChars(const char *str); @@ -613,76 +611,42 @@ MultiTenantMonitorshmemSize(void) static char * ExtractTopComment(const char *inputString) { - int commentStartCharsLength = 2; - int inputStringLen = strlen(inputString); - if (inputStringLen < commentStartCharsLength) - { - return NULL; - } + int commentCharsLength = 2; + int inputStringLen = strlen(inputString); + if (inputStringLen < commentCharsLength) + { + return NULL; + } - int commentEndCharsIndex = 0; + const char *commentStartChars = "/*"; + const char *commentEndChars = "*/"; - /* If query doesn't start with a comment, return NULL */ - if (inputString[commentEndCharsIndex] != '/' || - inputString[commentEndCharsIndex + 1] != '*') - { - return NULL; - } + /* If query doesn't start with a comment, return NULL */ + if (strstr(inputString, commentStartChars) != inputString) + { + return NULL; + } - /* Skip the comment start characters */ - commentEndCharsIndex += commentStartCharsLength; - - /* Find the first comment end character */ - while (commentEndCharsIndex < inputStringLen && - !(inputString[commentEndCharsIndex] == '*' && - inputString [commentEndCharsIndex + 1] == '/')) - { - commentEndCharsIndex++; - } + StringInfo commentData = makeStringInfo(); - /* If there is no end of comment chars , return NULL */ - if (inputString[commentEndCharsIndex] != '*' && - inputString[commentEndCharsIndex + 1] != '/') - { - return NULL; - } + /* Skip the comment start characters */ + const char *commentStart = inputString + commentCharsLength; - if (commentEndCharsIndex > commentStartCharsLength) - { - return Substring(inputString, commentStartCharsLength, commentEndCharsIndex); - } - else - { - return NULL; - } + /* Find the first comment end character */ + const char *commentEnd = strstr(commentStart, commentEndChars); + if (commentEnd == NULL) + { + return NULL; + } + + /* Append the comment to the StringInfo buffer */ + int commentLength = commentEnd - commentStart; + appendStringInfo(commentData, "%.*s", commentLength, commentStart); + + /* Return the extracted comment */ + return commentData->data; } - -/* Extracts a substring from the input string between the specified start and end indices.*/ -static char * -Substring(const char *str, int start, int end) -{ - int len = strlen(str); - - /* Ensure start and end are within the bounds of the string */ - if (start < 0 || end > len || start > end) - { - return NULL; - } - - /* Allocate memory for the substring */ - char *substr = (char *) palloc((end - start + 1) * sizeof(char)); - - /* Copy the substring to the new memory location */ - strncpy_s(substr, end - start + 1, str + start, end - start); - - /* Add null terminator to end of the substring */ - substr[end - start] = '\0'; - - return substr; -} - - /* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */ static char * EscapeCommentChars(const char *str)