From f6a67cec9443f7f4e28ecefa732b6e88facd770e Mon Sep 17 00:00:00 2001 From: Cornelius Boateng Date: Tue, 6 May 2025 00:44:07 +0000 Subject: [PATCH] Merged PR 1654888: [engine][collation] Support for $indexOfArray ### Does this PR have any customer impact? No ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) No ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Are you introducing any new config? If yes, do you have tests with and without them being set? No ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Support collation with `$indexOfArray` `$indexOfBytes, $indexOfCP, $strcasecmp` are collation-agnostic. They do not respect collation. `$let` and `$zip` respect collation. ---- #### AI description (iteration 1) #### PR Classification New feature: This pull request adds support for collation in string and array operators, specifically for `$indexOfBytes`, `$indexOfCP`, and `$indexOfArray`. #### PR Summary This pull request introduces collation support to enhance string and array operations, allowing for locale-aware comparisons in `$indexOfBytes`, `$indexOfCP`, and `$indexOfArray` operators. - Modified `bson_expression_string_operators.c` to include collation handling in string comparison functions and updated function signatures to accept collation strings. - Updated `bson_expression_array_operators.c` to support collation in array index operations, including changes to function signatures and argument handling. - Introduced new structures `StringCompareArguments` and `IndexOfArrayArguments` to manage collation strings and arguments for the respective operations. --- CHANGELOG.md | 1 + ...son_dollar_ops_collation_tests_runtime.out | 142 ++++++++++++++++++ ...son_dollar_ops_collation_tests_runtime.sql | 32 +++- .../bson_expression_array_operators.c | 40 ++++- 4 files changed, 205 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18f37eb3..857e9bc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### documentdb v0.104-0 (Unreleased) ### * Add string case support for `$toDate` operator +* Support collation with `$indexOfArray` aggregation operator. *[Feature]* * Support collation with arrays and objects comparisons *[Feature]* ### documentdb v0.103-0 (Unreleased) ### diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_dollar_ops_collation_tests_runtime.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_dollar_ops_collation_tests_runtime.out index 159e5ea7..fe683043 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_dollar_ops_collation_tests_runtime.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_dollar_ops_collation_tests_runtime.out @@ -2185,6 +2185,102 @@ SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "fi --------------------------------------------------------------------- (0 rows) +-- support for $indexOfArray +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfArray": [ ["CAT", "DOG"], "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(2 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfArray": [ ["CAT", "DOG"], "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + document +--------------------------------------------------------------------- +(0 rows) + +-- support for $indexOfBytes (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(1 row) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(1 row) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "$a", "aT" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + document +--------------------------------------------------------------------- +(0 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "$a", "AT" ] }, -1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "2" }, "a" : "dog" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } + { "_id" : { "$numberInt" : "4" }, "a" : "dOg" } + { "_id" : "bat", "a" : "bat" } + { "_id" : "hen", "a" : "hen" } +(6 rows) + +-- support for $indexOfCP (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(1 row) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(1 row) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "$a", "at" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : "bat", "a" : "bat" } +(2 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "$a", "AT" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + document +--------------------------------------------------------------------- +(0 rows) + +-- support for $strcasecmp (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(2 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(2 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(2 rows) + +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat" } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt" } +(2 rows) + -- $addFields SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$addFields": { "newField": { "$ne": ["$a", "CAT"] } } } ], "cursor": {}, "collation": { "locale": "fr", "strength" : 1} }'); document @@ -2892,6 +2988,52 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_p { "_id" : "bat", "a" : "bat", "newField" : false } (6 rows) +-- support in $let +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$let": { "vars": { "var1": "$a" }, "in": { "$cond": { "if": { "$eq": ["$$var1", "CAT"] }, "then": 1, "else": 0 } } } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat", "newField" : { "$numberInt" : "1" } } + { "_id" : { "$numberInt" : "2" }, "a" : "dog", "newField" : { "$numberInt" : "0" } } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt", "newField" : { "$numberInt" : "1" } } + { "_id" : { "$numberInt" : "4" }, "a" : "dOg", "newField" : { "$numberInt" : "0" } } + { "_id" : "hen", "a" : "hen", "newField" : { "$numberInt" : "0" } } + { "_id" : "bat", "a" : "bat", "newField" : { "$numberInt" : "0" } } +(6 rows) + +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$let": { "vars": { "var1": "$a" }, "in": { "$cond": { "if": { "$eq": ["$$var1", "CAT"] }, "then": 1, "else": 0 } } } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 3} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat", "newField" : { "$numberInt" : "0" } } + { "_id" : { "$numberInt" : "2" }, "a" : "dog", "newField" : { "$numberInt" : "0" } } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt", "newField" : { "$numberInt" : "0" } } + { "_id" : { "$numberInt" : "4" }, "a" : "dOg", "newField" : { "$numberInt" : "0" } } + { "_id" : "hen", "a" : "hen", "newField" : { "$numberInt" : "0" } } + { "_id" : "bat", "a" : "bat", "newField" : { "$numberInt" : "0" } } +(6 rows) + +-- support for $zip +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$zip": { "inputs": [ {"$cond": [{"$eq": ["CAT", "$a"]}, ["$a"], ["null"]]}, ["$a"]] } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat", "newField" : [ [ "cat", "cat" ] ] } + { "_id" : { "$numberInt" : "2" }, "a" : "dog", "newField" : [ [ "null", "dog" ] ] } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt", "newField" : [ [ "cAt", "cAt" ] ] } + { "_id" : { "$numberInt" : "4" }, "a" : "dOg", "newField" : [ [ "null", "dOg" ] ] } + { "_id" : "hen", "a" : "hen", "newField" : [ [ "null", "hen" ] ] } + { "_id" : "bat", "a" : "bat", "newField" : [ [ "null", "bat" ] ] } +(6 rows) + +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$zip": { "inputs": [ {"$cond": [{"$eq": ["CAT", "$a"]}, ["$a"], ["null"]]}, ["$a"]] } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 3} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "cat", "newField" : [ [ "null", "cat" ] ] } + { "_id" : { "$numberInt" : "2" }, "a" : "dog", "newField" : [ [ "null", "dog" ] ] } + { "_id" : { "$numberInt" : "3" }, "a" : "cAt", "newField" : [ [ "null", "cAt" ] ] } + { "_id" : { "$numberInt" : "4" }, "a" : "dOg", "newField" : [ [ "null", "dOg" ] ] } + { "_id" : "hen", "a" : "hen", "newField" : [ [ "null", "hen" ] ] } + { "_id" : "bat", "a" : "bat", "newField" : [ [ "null", "bat" ] ] } +(6 rows) + -- query match -- enableLetAndCollationForQueryMatch GUC off: ignore collation SET documentdb.enableLetAndCollationForQueryMatch TO off; diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_dollar_ops_collation_tests_runtime.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_dollar_ops_collation_tests_runtime.sql index 2682ae30..8ef5eaf6 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_dollar_ops_collation_tests_runtime.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_dollar_ops_collation_tests_runtime.sql @@ -528,10 +528,8 @@ SELECT document FROM bson_aggregation_pipeline('db', -- unsupported $merge SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "ci_search6", "pipeline": [{"$merge" : { "into": "ci_search9", "whenMatched" : "replace" }} ], "collation": { "locale": "en", "strength" : 1} }'); - SELECT documentdb_api.shard_collection('db', 'ci_search6', '{ "_id": "hashed" }', false); - -- lookup with id join (collation aware) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "ci_search6", "pipeline": [ { "$lookup": { "from": "ci_search6", "as": "matched_docs", "localField": "_id", "foreignField": "_id", "pipeline": [ { "$match": { "$or" : [ { "a.b": "cat" }, { "a.b": "dog" } ] } } ] } } ], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); @@ -597,6 +595,28 @@ SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "fi SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$in": ["$_id", ["HEN", "BAT"]]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$in": ["$_id", ["HEN", "BAT"]]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); +-- support for $indexOfArray +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfArray": [ ["CAT", "DOG"], "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfArray": [ ["CAT", "DOG"], "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + +-- support for $indexOfBytes (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "$a", "aT" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfBytes": [ "$a", "AT" ] }, -1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + +-- support for $indexOfCP (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "cAtALoNa", "$a" ] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "$a", "at" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$indexOfCP": [ "$a", "AT" ] }, 1]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); + +-- support for $strcasecmp (doesn't respect collation) +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 1 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 2 } }'); +SELECT document FROM bson_aggregation_find('db', '{ "find": "coll_agg_proj", "filter": { "$expr": {"$eq": [{ "$strcasecmp": ["$a", "CAT"] }, 0]} }, "sort": { "_id": 1 }, "skip": 0, "collation": { "locale": "en", "strength" : 3 } }'); + -- $addFields SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$addFields": { "newField": { "$ne": ["$a", "CAT"] } } } ], "cursor": {}, "collation": { "locale": "fr", "strength" : 1} }'); SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$addFields": { "newField": { "$lte": ["$a", "CAT"] } } } ], "cursor": {}, "collation": { "locale": "fr", "strength" : 1} }'); @@ -691,6 +711,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_p SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": {"$setIsSubset": [["$a", "cAT", "dog"], ["CAT", "DOG"]]} } }], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": {"$setIsSubset": [["$a", "cAT", "dog"], ["CAT", "DOG"]]} } }], "cursor": {}, "collation": { "locale": "en", "strength" : 3} }'); +-- support in $let +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$let": { "vars": { "var1": "$a" }, "in": { "$cond": { "if": { "$eq": ["$$var1", "CAT"] }, "then": 1, "else": 0 } } } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$let": { "vars": { "var1": "$a" }, "in": { "$cond": { "if": { "$eq": ["$$var1", "CAT"] }, "then": 1, "else": 0 } } } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 3} }'); + +-- support for $zip +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$zip": { "inputs": [ {"$cond": [{"$eq": ["CAT", "$a"]}, ["$a"], ["null"]]}, ["$a"]] } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 1} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "coll_agg_proj", "pipeline": [ { "$project": { "a": 1, "newField": { "$zip": { "inputs": [ {"$cond": [{"$eq": ["CAT", "$a"]}, ["$a"], ["null"]]}, ["$a"]] } } } }], "cursor": {}, "collation": { "locale": "en", "strength" : 3} }'); + -- query match -- enableLetAndCollationForQueryMatch GUC off: ignore collation SET documentdb.enableLetAndCollationForQueryMatch TO off; diff --git a/pg_documentdb/src/operators/bson_expression_array_operators.c b/pg_documentdb/src/operators/bson_expression_array_operators.c index 2780c396..7354f4b7 100644 --- a/pg_documentdb/src/operators/bson_expression_array_operators.c +++ b/pg_documentdb/src/operators/bson_expression_array_operators.c @@ -50,7 +50,7 @@ typedef struct DollarInArguments { AggregationExpressionData *targetValue; AggregationExpressionData *searchArray; - char *collationString; + const char *collationString; } DollarInArguments; /* State for a $arrayElemAt, $first or $last operator. */ @@ -181,6 +181,14 @@ typedef struct ZipParseInputsResult int outputSubArrayLength; } ZipParseInputsResult; + +/* Struct that represents parsed arguments of $indexOfArray and collation string. */ +typedef struct IndexOfArrayArguments +{ + List *argumentsList; + const char *collationString; +} IndexOfArrayArguments; + /* --------------------------------------------------------- */ /* Forward declaration */ /* --------------------------------------------------------- */ @@ -246,7 +254,8 @@ static void ValidateArraySizeLimit(int32_t startValue, int32_t endValue, int32_t stepValue); static int32 GetIndexValueFromDollarIdxInput(bson_value_t *arg, bool isStartIndex); static int32 FindIndexInArrayFor(bson_value_t *array, bson_value_t *element, - int32 startIndex, int32 endIndex); + int32 startIndex, int32 endIndex, + const char *collationString); static void SetResultArrayForDollarReverse(bson_value_t *array, bson_value_t *result); static void SetResultValueForDollarMaxMin(const bson_value_t *inputArgument, bson_value_t *result, bool isFindMax); @@ -1504,7 +1513,8 @@ ParseDollarIndexOfArray(const bson_value_t *argument, AggregationExpressionData result.value.v_int32 = FindIndexInArrayFor(&arrExpressionData->value, &searchExpressionData->value, startIndex, - endIndex); + endIndex, + context->collationString); data->value = result; data->kind = AggregationExpressionKind_Constant; @@ -1514,7 +1524,18 @@ ParseDollarIndexOfArray(const bson_value_t *argument, AggregationExpressionData return; } - data->operator.arguments = argsList; + + IndexOfArrayArguments *parsedArguments = palloc0( + sizeof(IndexOfArrayArguments)); + + if (IsCollationApplicable(context->collationString)) + { + parsedArguments->collationString = pstrdup(context->collationString); + } + + parsedArguments->argumentsList = argsList; + data->operator.arguments = parsedArguments; + data->operator.argumentsKind = AggregationExpressionArgumentsKind_Palloc; } @@ -1527,7 +1548,9 @@ void HandlePreParsedDollarIndexOfArray(pgbson *doc, void *arguments, ExpressionResult *expressionResult) { - List *argsList = (List *) arguments; + IndexOfArrayArguments *parsedArguments = arguments; + List *argsList = parsedArguments->argumentsList; + const char *collationString = parsedArguments->collationString; /* evaluating the array argument expression */ AggregationExpressionData *arrExpressionData = list_nth(argsList, 0); @@ -1595,7 +1618,7 @@ HandlePreParsedDollarIndexOfArray(pgbson *doc, void *arguments, bson_value_t result = { .value_type = BSON_TYPE_INT32 }; result.value.v_int32 = FindIndexInArrayFor(&arrayExpression, &element, startIndex, - endIndex); + endIndex, collationString); ExpressionResultSetValue(expressionResult, &result); } @@ -4028,7 +4051,7 @@ GetIndexValueFromDollarIdxInput(bson_value_t *arg, bool isStartIndex) */ static int32 FindIndexInArrayFor(bson_value_t *array, bson_value_t *element, int32 startIndex, - int32 endIndex) + int32 endIndex, const char *collationString) { if (startIndex >= endIndex) { @@ -4047,7 +4070,8 @@ FindIndexInArrayFor(bson_value_t *array, bson_value_t *element, int32 startIndex while (bson_iter_next(&arrayIterator) && currentIndex < endIndex) { - if (BsonValueEquals(bson_iter_value(&arrayIterator), element)) + if (BsonValueEqualsWithCollation(bson_iter_value(&arrayIterator), element, + collationString)) { return currentIndex; }