mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 19:59:54 -05:00
Merged PR 1672946: [Update] Make update parsing cheaper
### Does this PR have any customer impact? Yes ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Perf ### 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? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Currently for Update/Delete, we currently parse the update spec, query spec, etc into pgbsons on startup. Then reuse it per commands. This has a few issues - for large document requests, this causes us to duplicate memory and persist it for the duration of the update and also transcode all up front - this is worse for multi-node when all we need to do is build yet another update spec and push it to the worker. Instead, we now only cache pointers to the spec document; then when we need it we convert to pgbson only if necessary. A consequence of this is that on a single node update, if we're doing a simple updateOne on _id then we would never serialize the updateSpec at all (for instance). We also more eagerly free memory on the intermediate update specs. ---- #### AI description (iteration 1) #### PR Classification Performance optimization for update parsing by refactoring BSON value handling. #### PR Summary This PR streamlines update parsing by replacing heavy BSON initialization routines with lightweight wrappers and converting update-related types from `pgbson*` to `bson_value_t*`, which reduces overhead and improves coordinator/worker compatibility. - `src/commands/update.c`: Replaced multiple `PgbsonInitFromDocumentBsonValue` and similar calls with efficient wrappers like `CreateBsonValueFrom` and updated serialization (including back-compatible array filter and update document formats). - `include/commands/update.h`: Updated update structure fields to use `bson_value_t*` instead of `pgbson*`, aligning interfaces with the new parsing approach. - `src/update/bson_update.c` and `bson_update_operators_workflow.c`: Refactored update type determination and replace document logic to use new BSON value iterators and helper functions. - Other modules (e.g. `commands/find_and_modify.c`, `commands/delete.c`, query operators, sharding) were updated to leverage the lighter-weight BSON representation and its associated helper routines. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
This commit is contained in:
1 parent
f63bc316ac
commit
8261e772f4
25 files changed
+343
-235
No files matched your search
@@ -22,7 +22,7 @@ typedef struct BsonPositionalQueryData
|
||||
List *queryQuals;
|
||||
} BsonPositionalQueryData;
|
||||
|
||||
BsonPositionalQueryData * GetPositionalQueryData(const pgbson *query);
|
||||
BsonPositionalQueryData * GetPositionalQueryData(const bson_value_t *query);
|
||||
|
||||
int32_t MatchPositionalQueryAgainstDocument(const BsonPositionalQueryData *data,
|
||||
const pgbson *document);
|
||||
|
||||
@@ -67,7 +67,8 @@ typedef struct WriteError
|
||||
} WriteError;
|
||||
|
||||
|
||||
bool FindShardKeyValueForDocumentId(MongoCollection *collection, pgbson *queryDoc,
|
||||
bool FindShardKeyValueForDocumentId(MongoCollection *collection, const
|
||||
bson_value_t *queryDoc,
|
||||
bson_value_t *objectId, int64 *shardKeyValue);
|
||||
|
||||
bool IsCommonSpecIgnoredField(const char *fieldName);
|
||||
@@ -75,6 +76,8 @@ bool IsCommonSpecIgnoredField(const char *fieldName);
|
||||
WriteError * GetWriteErrorFromErrorData(ErrorData *errorData, int writeErrorIdx);
|
||||
bool TryGetErrorMessageAndCode(ErrorData *errorData, int *code, char **errmessage);
|
||||
|
||||
pgbson * GetObjectIdFilterFromQueryDocumentValue(const bson_value_t *queryDoc,
|
||||
bool *hasNonIdFields);
|
||||
pgbson * GetObjectIdFilterFromQueryDocument(pgbson *queryDoc, bool *hasNonIdFields);
|
||||
|
||||
|
||||
|
||||
@@ -21,16 +21,16 @@
|
||||
typedef struct
|
||||
{
|
||||
/* list of Deletions */
|
||||
pgbson *query;
|
||||
const bson_value_t *query;
|
||||
|
||||
/* sort order to use when selecting 1 row */
|
||||
pgbson *sort;
|
||||
const bson_value_t *sort;
|
||||
|
||||
/* whether to return deleted document */
|
||||
bool returnDeletedDocument;
|
||||
|
||||
/* fields to return if returning a document */
|
||||
pgbson *returnFields;
|
||||
const bson_value_t *returnFields;
|
||||
} DeleteOneParams;
|
||||
|
||||
|
||||
|
||||
@@ -33,25 +33,25 @@ typedef enum
|
||||
typedef struct
|
||||
{
|
||||
/* update only documents matching this query */
|
||||
pgbson *query;
|
||||
const bson_value_t *query;
|
||||
|
||||
/* apply this update */
|
||||
pgbson *update;
|
||||
const bson_value_t *update;
|
||||
|
||||
/* whether to use upsert if no documents match */
|
||||
int isUpsert;
|
||||
|
||||
/* sort order to use when selecting 1 row */
|
||||
pgbson *sort;
|
||||
const bson_value_t *sort;
|
||||
|
||||
/* whether to return a document */
|
||||
UpdateReturnValue returnDocument;
|
||||
|
||||
/* fields to return if returning a document */
|
||||
pgbson *returnFields;
|
||||
const bson_value_t *returnFields;
|
||||
|
||||
/* array filters specified in the update */
|
||||
pgbson *arrayFilters;
|
||||
const bson_value_t *arrayFilters;
|
||||
|
||||
/* whether to bypass document validation */
|
||||
bool bypassDocumentValidation;
|
||||
|
||||
@@ -110,6 +110,8 @@ typedef struct BsonQueryOperatorContext
|
||||
|
||||
Var * MakeSimpleDocumentVar(void);
|
||||
Node * ReplaceBsonQueryOperators(Query *node, ParamListInfo boundParams);
|
||||
|
||||
void ValidateQueryDocumentValue(const bson_value_t *queryDocumentValue);
|
||||
void ValidateQueryDocument(pgbson *queryDocument);
|
||||
bool QueryDocumentsAreEquivalent(const pgbson *leftQueryDocument,
|
||||
const pgbson *rightQueryDocument);
|
||||
@@ -117,7 +119,7 @@ List * CreateQualsFromQueryDocIterator(bson_iter_t *queryDocIterator,
|
||||
BsonQueryOperatorContext *context);
|
||||
Node * EvaluateBoundParameters(Node *expression, ParamListInfo boundParams);
|
||||
|
||||
List * CreateQualsForBsonValueTopLevelQuery(const pgbson *query);
|
||||
List * CreateQualsForBsonValueTopLevelQueryIter(bson_iter_t *queryIter);
|
||||
Expr * CreateQualForBsonValueExpression(const bson_value_t *expression, const
|
||||
char *collationString);
|
||||
Expr * CreateQualForBsonValueArrayExpression(const bson_value_t *expression);
|
||||
|
||||
@@ -17,6 +17,10 @@ int64 ComputeShardKeyHashForDocument(pgbson *shardKey, uint64_t collectionId,
|
||||
pgbson *document);
|
||||
bool ComputeShardKeyHashForQuery(pgbson *shardKey, uint64_t collectionId, pgbson *query,
|
||||
int64 *shardKeyHash, bool *isShardKeyCollationAware);
|
||||
bool ComputeShardKeyHashForQueryValue(pgbson *shardKey, uint64_t collectionId, const
|
||||
bson_value_t *query,
|
||||
int64 *shardKeyHash,
|
||||
bool *isShardKeyCollationAware);
|
||||
|
||||
Expr * ComputeShardKeyExprForQueryValue(pgbson *shardKey, uint64_t collectionId, const
|
||||
bson_value_t *queryDocument,
|
||||
|
||||
@@ -20,9 +20,11 @@ typedef enum
|
||||
UpdateType_AggregationPipeline
|
||||
} UpdateType;
|
||||
|
||||
UpdateType DetermineUpdateType(pgbson *updateSpec);
|
||||
void ValidateUpdateDocument(pgbson *updateSpec, pgbson *querySpec, pgbson *arrayFilters);
|
||||
pgbson * BsonUpdateDocument(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
pgbson *querySpec, pgbson *arrayFilters);
|
||||
UpdateType DetermineUpdateType(const bson_value_t *updateSpec);
|
||||
void ValidateUpdateDocument(const bson_value_t *updateSpec, const bson_value_t *querySpec,
|
||||
const bson_value_t *arrayFilters);
|
||||
pgbson * BsonUpdateDocument(pgbson *sourceDocument, const bson_value_t *updateSpec,
|
||||
const bson_value_t *querySpec, const
|
||||
bson_value_t *arrayFilters);
|
||||
|
||||
#endif
|
||||
@@ -32,7 +32,7 @@ struct BsonIntermediatePathNode;
|
||||
typedef struct PositionalUpdateSpec
|
||||
{
|
||||
/* The input query spec - used when evaluating $ positional operators */
|
||||
pgbson *querySpec;
|
||||
const bson_value_t *querySpec;
|
||||
|
||||
/* hashmap of char* to bson_value_t* */
|
||||
HTAB *arrayFilters;
|
||||
@@ -100,8 +100,9 @@ typedef struct MongoUpdateOperatorSpec
|
||||
|
||||
/* aggregation */
|
||||
|
||||
struct AggregationPipelineUpdateState * GetAggregationPipelineUpdateState(
|
||||
pgbson *updateSpec);
|
||||
struct AggregationPipelineUpdateState * GetAggregationPipelineUpdateState(const
|
||||
bson_value_t *
|
||||
updateSpec);
|
||||
|
||||
pgbson * ProcessAggregationPipelineUpdate(pgbson *sourceDoc,
|
||||
const struct AggregationPipelineUpdateState *
|
||||
@@ -112,9 +113,12 @@ pgbson * ProcessAggregationPipelineUpdate(pgbson *sourceDoc,
|
||||
/* Update workflows */
|
||||
void RegisterUpdateOperatorExtension(const MongoUpdateOperatorSpec *extensibleDefinition);
|
||||
|
||||
const struct BsonIntermediatePathNode * GetOperatorUpdateState(pgbson *updateSpec,
|
||||
pgbson *querySpec,
|
||||
pgbson *arrayFilters,
|
||||
const struct BsonIntermediatePathNode * GetOperatorUpdateState(const
|
||||
bson_value_t *updateSpec,
|
||||
const bson_value_t *
|
||||
querySpec,
|
||||
const bson_value_t *
|
||||
arrayFilters,
|
||||
bool isUpsert);
|
||||
pgbson * ProcessUpdateOperatorWithState(pgbson *sourceDoc,
|
||||
const struct BsonIntermediatePathNode *
|
||||
|
||||
@@ -99,10 +99,12 @@ static void PositionalSetIntermediateArrayIndex(void *state, int32_t index);
|
||||
* object that is used in evaluation of the positional $ operator.
|
||||
*/
|
||||
BsonPositionalQueryData *
|
||||
GetPositionalQueryData(const pgbson *query)
|
||||
GetPositionalQueryData(const bson_value_t *query)
|
||||
{
|
||||
/* Step 1: Create Quals for the query based on BSON value inputs */
|
||||
List *queryQuals = CreateQualsForBsonValueTopLevelQuery(query);
|
||||
bson_iter_t queryDocIterator;
|
||||
BsonValueInitIterator(query, &queryDocIterator);
|
||||
List *queryQuals = CreateQualsForBsonValueTopLevelQueryIter(&queryDocIterator);
|
||||
|
||||
List *finalQuals = NIL;
|
||||
|
||||
|
||||
@@ -506,8 +506,10 @@ PostProcessLeafNodeForFind(void *state, const StringView *path, BsonPathNode *ch
|
||||
{
|
||||
treeState->positionalOperatorState->isQueryEmpty = IsPgbsonEmptyDocument(
|
||||
treeState->querySpec);
|
||||
bson_value_t queryValue = ConvertPgbsonToBsonValue(
|
||||
treeState->querySpec);
|
||||
treeState->positionalOperatorState->positionalQueryData =
|
||||
GetPositionalQueryData(treeState->querySpec);
|
||||
GetPositionalQueryData(&queryValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ static pgbson * RewriteDocumentAddObjectIdCore(const bson_value_t *docValue,
|
||||
* document IDs that match, it uses the smallest one.
|
||||
*/
|
||||
bool
|
||||
FindShardKeyValueForDocumentId(MongoCollection *collection, pgbson *queryDoc,
|
||||
FindShardKeyValueForDocumentId(MongoCollection *collection, const bson_value_t *queryDoc,
|
||||
bson_value_t *objectId, int64 *shardKeyValue)
|
||||
{
|
||||
StringInfoData selectQuery;
|
||||
@@ -129,7 +129,8 @@ FindShardKeyValueForDocumentId(MongoCollection *collection, pgbson *queryDoc,
|
||||
|
||||
/* we use bytea because bson may not have the same OID on all nodes */
|
||||
argTypes[1] = BYTEAOID;
|
||||
argValues[1] = PointerGetDatum(CastPgbsonToBytea(queryDoc));
|
||||
argValues[1] = PointerGetDatum(CastPgbsonToBytea(PgbsonInitFromDocumentBsonValue(
|
||||
queryDoc)));
|
||||
|
||||
char *argNulls = NULL;
|
||||
bool readOnly = false;
|
||||
@@ -189,6 +190,24 @@ SetExplicitStatementTimeout(int timeoutMilliseconds)
|
||||
}
|
||||
|
||||
|
||||
pgbson *
|
||||
GetObjectIdFilterFromQueryDocumentValue(const bson_value_t *queryDoc,
|
||||
bool *queryHasNonIdFilters)
|
||||
{
|
||||
bson_iter_t queryIterator;
|
||||
bson_value_t queryIdValue;
|
||||
bool errorOnConflict = false;
|
||||
BsonValueInitIterator(queryDoc, &queryIterator);
|
||||
if (TraverseQueryDocumentAndGetId(&queryIterator, &queryIdValue, errorOnConflict,
|
||||
queryHasNonIdFilters))
|
||||
{
|
||||
return BsonValueToDocumentPgbson(&queryIdValue);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Extracts the object_id if applicable from a query doc and returns a serialized pgbson
|
||||
* containing the object_id (top level column) value if one was extracted.
|
||||
@@ -197,17 +216,8 @@ SetExplicitStatementTimeout(int timeoutMilliseconds)
|
||||
pgbson *
|
||||
GetObjectIdFilterFromQueryDocument(pgbson *queryDoc, bool *queryHasNonIdFilters)
|
||||
{
|
||||
bson_iter_t queryIterator;
|
||||
bson_value_t queryIdValue;
|
||||
bool errorOnConflict = false;
|
||||
PgbsonInitIterator(queryDoc, &queryIterator);
|
||||
if (TraverseQueryDocumentAndGetId(&queryIterator, &queryIdValue, errorOnConflict,
|
||||
queryHasNonIdFilters))
|
||||
{
|
||||
return BsonValueToDocumentPgbson(&queryIdValue);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
bson_value_t queryIdValue = ConvertPgbsonToBsonValue(queryDoc);
|
||||
return GetObjectIdFilterFromQueryDocumentValue(&queryIdValue, queryHasNonIdFilters);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -453,7 +453,7 @@ BuildDeletionSpecListFromSequence(pgbsonsequence *sequence)
|
||||
static DeletionSpec *
|
||||
BuildDeletionSpec(bson_iter_t *deletionIter)
|
||||
{
|
||||
pgbson *query = NULL;
|
||||
bson_value_t *query = NULL;
|
||||
int64 limit = -1;
|
||||
|
||||
while (bson_iter_next(deletionIter))
|
||||
@@ -464,7 +464,7 @@ BuildDeletionSpec(bson_iter_t *deletionIter)
|
||||
{
|
||||
EnsureTopLevelFieldType("delete.deletes.q", deletionIter, BSON_TYPE_DOCUMENT);
|
||||
|
||||
query = PgbsonInitFromIterDocumentValue(deletionIter);
|
||||
query = CreateBsonValueCopy(bson_iter_value(deletionIter));
|
||||
}
|
||||
else if (strcmp(field, "limit") == 0)
|
||||
{
|
||||
@@ -656,7 +656,7 @@ ProcessDeletion(MongoCollection *collection, DeletionSpec *deletionSpec,
|
||||
"regular delete")));
|
||||
}
|
||||
|
||||
pgbson *query = deletionSpec->deleteOneParams.query;
|
||||
pgbson *query = PgbsonInitFromDocumentBsonValue(deletionSpec->deleteOneParams.query);
|
||||
|
||||
/* determine whether query filters by a single shard key value */
|
||||
int64 shardKeyHash = 0;
|
||||
@@ -678,14 +678,15 @@ ProcessDeletion(MongoCollection *collection, DeletionSpec *deletionSpec,
|
||||
TraverseQueryDocumentAndGetId(&queryDocIter, &idFromQueryDocument,
|
||||
errorOnConflict, &queryHasNonIdFilters);
|
||||
|
||||
uint64_t result = 0;
|
||||
if (deletionSpec->limit == 0)
|
||||
{
|
||||
/*
|
||||
* Delete as many document as match the query. This is not a retryable
|
||||
* operation, so we ignore transactionId.
|
||||
*/
|
||||
return DeleteAllMatchingDocuments(collection, query,
|
||||
hasShardKeyValueFilter, shardKeyHash);
|
||||
result = DeleteAllMatchingDocuments(collection, query,
|
||||
hasShardKeyValueFilter, shardKeyHash);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -719,8 +720,11 @@ ProcessDeletion(MongoCollection *collection, DeletionSpec *deletionSpec,
|
||||
"_id or shard key filter")));
|
||||
}
|
||||
|
||||
return deleteOneResult.isRowDeleted ? 1 : 0;
|
||||
result = deleteOneResult.isRowDeleted ? 1 : 0;
|
||||
}
|
||||
|
||||
pfree(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1060,9 +1064,11 @@ DeleteOneInternal(MongoCollection *collection, DeleteOneParams *deleteOneParams,
|
||||
{
|
||||
uint64 planId = QUERY_DELETE_ONE;
|
||||
List *sortFieldDocuments = deleteOneParams->sort == NULL ? NIL :
|
||||
PgbsonDecomposeFields(deleteOneParams->sort);
|
||||
BsonValueDocumentDecomposeFields(deleteOneParams->sort);
|
||||
|
||||
pgbson *query = PgbsonInitFromDocumentBsonValue(deleteOneParams->query);
|
||||
bool queryHasNonIdFilters = false;
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(deleteOneParams->query,
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(query,
|
||||
&queryHasNonIdFilters);
|
||||
|
||||
int argCount = 2 + list_length(sortFieldDocuments);
|
||||
@@ -1176,7 +1182,7 @@ DeleteOneInternal(MongoCollection *collection, DeleteOneParams *deleteOneParams,
|
||||
|
||||
/* we use bytea because bson may not have the same OID on all nodes */
|
||||
argTypes[1] = BYTEAOID;
|
||||
argValues[1] = PointerGetDatum(CastPgbsonToBytea(deleteOneParams->query));
|
||||
argValues[1] = PointerGetDatum(CastPgbsonToBytea(query));
|
||||
|
||||
char *argNulls = NULL;
|
||||
bool readOnly = false;
|
||||
@@ -1245,7 +1251,7 @@ DeleteOneInternal(MongoCollection *collection, DeleteOneParams *deleteOneParams,
|
||||
bool forceProjectId = false;
|
||||
bool allowInclusionExclusion = false;
|
||||
bson_iter_t projectIter;
|
||||
PgbsonInitIterator(deleteOneParams->returnFields, &projectIter);
|
||||
BsonValueInitIterator(deleteOneParams->returnFields, &projectIter);
|
||||
|
||||
const BsonProjectionQueryState *projectionState =
|
||||
GetProjectionStateForBsonProject(&projectIter,
|
||||
@@ -1282,12 +1288,12 @@ SerializeDeleteOneParams(const DeleteOneParams *deleteParams)
|
||||
|
||||
if (deleteParams->query != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "query", 5, deleteParams->query);
|
||||
PgbsonWriterAppendValue(&writer, "query", 5, deleteParams->query);
|
||||
}
|
||||
|
||||
if (deleteParams->sort != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "sort", 4, deleteParams->sort);
|
||||
PgbsonWriterAppendValue(&writer, "sort", 4, deleteParams->sort);
|
||||
}
|
||||
|
||||
PgbsonWriterAppendBool(&writer, "returnDeletedDocument", 21,
|
||||
@@ -1295,8 +1301,8 @@ SerializeDeleteOneParams(const DeleteOneParams *deleteParams)
|
||||
|
||||
if (deleteParams->returnFields != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "returnFields", 12,
|
||||
deleteParams->returnFields);
|
||||
PgbsonWriterAppendValue(&writer, "returnFields", 12,
|
||||
deleteParams->returnFields);
|
||||
}
|
||||
|
||||
PgbsonWriterEndDocument(&commandWriter, &writer);
|
||||
@@ -1316,13 +1322,13 @@ DeserializeDeleteWorkerSpecForDeleteOne(const bson_value_t *workerSpecValue,
|
||||
const char *key = bson_iter_key(&commandIter);
|
||||
if (strcmp(key, "query") == 0)
|
||||
{
|
||||
deleteOneParams->query = PgbsonInitFromDocumentBsonValue(bson_iter_value(
|
||||
&commandIter));
|
||||
deleteOneParams->query = CreateBsonValueCopy(bson_iter_value(
|
||||
&commandIter));
|
||||
}
|
||||
else if (strcmp(key, "sort") == 0)
|
||||
{
|
||||
deleteOneParams->sort = PgbsonInitFromDocumentBsonValue(bson_iter_value(
|
||||
&commandIter));
|
||||
deleteOneParams->sort = CreateBsonValueCopy(bson_iter_value(
|
||||
&commandIter));
|
||||
}
|
||||
else if (strcmp(key, "returnDeletedDocument") == 0)
|
||||
{
|
||||
@@ -1330,7 +1336,7 @@ DeserializeDeleteWorkerSpecForDeleteOne(const bson_value_t *workerSpecValue,
|
||||
}
|
||||
else if (strcmp(key, "returnFields") == 0)
|
||||
{
|
||||
deleteOneParams->returnFields = PgbsonInitFromDocumentBsonValue(
|
||||
deleteOneParams->returnFields = CreateBsonValueCopy(
|
||||
bson_iter_value(&commandIter));
|
||||
}
|
||||
}
|
||||
@@ -1534,7 +1540,7 @@ ValidateQueryDocuments(BatchDeletionSpec *batchSpec)
|
||||
MemoryContext oldContext = CurrentMemoryContext;
|
||||
PG_TRY();
|
||||
{
|
||||
ValidateQueryDocument(deletionSpec->deleteOneParams.query);
|
||||
ValidateQueryDocumentValue(deletionSpec->deleteOneParams.query);
|
||||
isSuccess = true;
|
||||
}
|
||||
PG_CATCH();
|
||||
|
||||
@@ -35,19 +35,19 @@ typedef struct
|
||||
char *collectionName;
|
||||
|
||||
/* "query" field */
|
||||
pgbson *query;
|
||||
bson_value_t *query;
|
||||
|
||||
/* "sort" field */
|
||||
pgbson *sort;
|
||||
bson_value_t *sort;
|
||||
|
||||
/* "remove" field */
|
||||
bool remove;
|
||||
|
||||
/* "update" field */
|
||||
pgbson *update;
|
||||
bson_value_t *update;
|
||||
|
||||
/* "arrayFilters" field */
|
||||
pgbson *arrayFilters;
|
||||
bson_value_t *arrayFilters;
|
||||
|
||||
/*
|
||||
* "new" field
|
||||
@@ -58,7 +58,7 @@ typedef struct
|
||||
bool returnNewDocument;
|
||||
|
||||
/* "fields" field */
|
||||
pgbson *returnFields;
|
||||
bson_value_t *returnFields;
|
||||
|
||||
/* "upsert" field */
|
||||
bool upsert;
|
||||
@@ -192,7 +192,7 @@ command_find_and_modify(PG_FUNCTION_ARGS)
|
||||
* noop, but we still need to report errors due to invalid query
|
||||
* / update documents.
|
||||
*/
|
||||
ValidateQueryDocument(spec.query);
|
||||
ValidateQueryDocumentValue(spec.query);
|
||||
|
||||
if (!spec.remove)
|
||||
{
|
||||
@@ -268,7 +268,7 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
if (EnsureTopLevelFieldTypeNullOk("findAndModify.query", &messageIter,
|
||||
BSON_TYPE_DOCUMENT))
|
||||
{
|
||||
spec.query = PgbsonInitFromIterDocumentValue(&messageIter);
|
||||
spec.query = CreateBsonValueCopy(bson_iter_value(&messageIter));
|
||||
}
|
||||
}
|
||||
else if (strcmp(key, "sort") == 0)
|
||||
@@ -276,7 +276,7 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
if (EnsureTopLevelFieldTypeNullOk("findAndModify.sort", &messageIter,
|
||||
BSON_TYPE_DOCUMENT))
|
||||
{
|
||||
spec.sort = PgbsonInitFromIterDocumentValue(&messageIter);
|
||||
spec.sort = CreateBsonValueCopy(bson_iter_value(&messageIter));
|
||||
}
|
||||
}
|
||||
else if (strcmp(key, "remove") == 0)
|
||||
@@ -298,7 +298,7 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
}
|
||||
|
||||
/* we keep update documents in projected form to preserve the type */
|
||||
spec.update = BsonValueToDocumentPgbson(bson_iter_value(&messageIter));
|
||||
spec.update = CreateBsonValueCopy(bson_iter_value(&messageIter));
|
||||
}
|
||||
else if (strcmp(key, "new") == 0)
|
||||
{
|
||||
@@ -314,7 +314,7 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
BSON_TYPE_DOCUMENT) &&
|
||||
!IsBsonValueEmptyDocument(bson_iter_value(&messageIter)))
|
||||
{
|
||||
spec.returnFields = PgbsonInitFromIterDocumentValue(&messageIter);
|
||||
spec.returnFields = CreateBsonValueCopy(bson_iter_value(&messageIter));
|
||||
}
|
||||
}
|
||||
else if (strcmp(key, "upsert") == 0)
|
||||
@@ -338,7 +338,7 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
BSON_TYPE_ARRAY);
|
||||
|
||||
/* we keep arrayFilters in projected form to preserve the type */
|
||||
spec.arrayFilters = BsonValueToDocumentPgbson(bson_iter_value(&messageIter));
|
||||
spec.arrayFilters = CreateBsonValueCopy(bson_iter_value(&messageIter));
|
||||
}
|
||||
else if (!SkipFailOnCollation && strcmp(key, "collation") == 0)
|
||||
{
|
||||
@@ -445,7 +445,9 @@ ParseFindAndModifyMessage(pgbson *message)
|
||||
|
||||
if (spec.query == NULL)
|
||||
{
|
||||
spec.query = PgbsonInitEmpty();
|
||||
pgbson *emptyQuery = PgbsonInitEmpty();
|
||||
spec.query = palloc(sizeof(bson_value_t));
|
||||
*spec.query = ConvertPgbsonToBsonValue(emptyQuery);
|
||||
}
|
||||
|
||||
return spec;
|
||||
@@ -462,11 +464,13 @@ ProcessFindAndModifySpec(MongoCollection *collection, FindAndModifySpec *spec,
|
||||
{
|
||||
int64 shardKeyHash = 0;
|
||||
bool shardKeyValueCollationAware = false;
|
||||
bool hasShardKeyValueFilter = ComputeShardKeyHashForQuery(collection->shardKey,
|
||||
collection->collectionId,
|
||||
spec->query,
|
||||
&shardKeyHash,
|
||||
&shardKeyValueCollationAware);
|
||||
bool hasShardKeyValueFilter = ComputeShardKeyHashForQueryValue(collection->shardKey,
|
||||
collection->
|
||||
collectionId,
|
||||
spec->query,
|
||||
&shardKeyHash,
|
||||
&
|
||||
shardKeyValueCollationAware);
|
||||
|
||||
if (!hasShardKeyValueFilter)
|
||||
{
|
||||
|
||||
@@ -292,9 +292,11 @@ static void ProcessUpdate(MongoCollection *collection, UpdateSpec *updateSpec,
|
||||
bool forceInlineWrites,
|
||||
ExprEvalState *stateForSchemaValidation);
|
||||
static UpdateAllMatchingDocsResult UpdateAllMatchingDocuments(MongoCollection *collection,
|
||||
pgbson *query,
|
||||
pgbson *update,
|
||||
pgbson *arrayFilters, bool
|
||||
const bson_value_t *query,
|
||||
const bson_value_t *update,
|
||||
const bson_value_t *
|
||||
arrayFilters,
|
||||
bool
|
||||
hasShardKeyValueFilter,
|
||||
int64 shardKeyHash,
|
||||
ExprEvalState *
|
||||
@@ -315,8 +317,10 @@ static void UpdateOneInternalWithRetryRecord(MongoCollection *collection, int64
|
||||
UpdateOneResult *result,
|
||||
ExprEvalState *stateForSchemaValidation);
|
||||
static bool SelectUpdateCandidate(uint64 collectionId, const char *shardTableName, int64
|
||||
shardKeyHash, pgbson *query,
|
||||
pgbson *update, pgbson *arrayFilters, pgbson *sort,
|
||||
shardKeyHash, const bson_value_t *query,
|
||||
const bson_value_t *update, const
|
||||
bson_value_t *arrayFilters,
|
||||
const bson_value_t *sort,
|
||||
UpdateCandidate *updateCandidate,
|
||||
bool getOriginalDocument, bool *hasOnlyObjectIdFilter);
|
||||
static bool UpdateDocumentByTID(uint64 collectionId, const char *shardTableName, int64
|
||||
@@ -329,8 +333,9 @@ static void UpdateOneObjectId(MongoCollection *collection,
|
||||
bson_value_t *objectId, text *transactionId,
|
||||
UpdateOneResult *result,
|
||||
ExprEvalState *stateForSchemaValidation);
|
||||
static pgbson * UpsertDocument(MongoCollection *collection, pgbson *update,
|
||||
pgbson *query, pgbson *arrayFilters,
|
||||
static pgbson * UpsertDocument(MongoCollection *collection, const bson_value_t *update,
|
||||
const bson_value_t *query, const
|
||||
bson_value_t *arrayFilters,
|
||||
ExprEvalState *stateForSchemaValidation,
|
||||
bool hasOnlyObjectIdFilter);
|
||||
static List * ValidateQueryAndUpdateDocuments(BatchUpdateSpec *batchSpec);
|
||||
@@ -871,9 +876,9 @@ BuildUpdateSpecListFromSequence(pgbsonsequence *updateDocs, bool *hasUpsert)
|
||||
static UpdateSpec *
|
||||
BuildUpdateSpec(bson_iter_t *updateIter)
|
||||
{
|
||||
pgbson *query = NULL;
|
||||
pgbson *update = NULL;
|
||||
pgbson *arrayFilters = NULL;
|
||||
bson_value_t *query = NULL;
|
||||
bson_value_t *update = NULL;
|
||||
bson_value_t *arrayFilters = NULL;
|
||||
bool isMulti = false;
|
||||
bool isUpsert = false;
|
||||
|
||||
@@ -885,7 +890,8 @@ BuildUpdateSpec(bson_iter_t *updateIter)
|
||||
{
|
||||
EnsureTopLevelFieldType("update.updates.q", updateIter, BSON_TYPE_DOCUMENT);
|
||||
|
||||
query = PgbsonInitFromIterDocumentValue(updateIter);
|
||||
query = palloc(sizeof(bson_value_t));
|
||||
*query = *bson_iter_value(updateIter);
|
||||
}
|
||||
else if (strcmp(field, "u") == 0)
|
||||
{
|
||||
@@ -898,10 +904,8 @@ BuildUpdateSpec(bson_iter_t *updateIter)
|
||||
BsonIterTypeName(updateIter))));
|
||||
}
|
||||
|
||||
const bson_value_t *bsonValue = bson_iter_value(updateIter);
|
||||
|
||||
/* we keep update documents in projected form to preserve the type */
|
||||
update = BsonValueToDocumentPgbson(bsonValue);
|
||||
update = palloc(sizeof(bson_value_t));
|
||||
*update = *bson_iter_value(updateIter);
|
||||
}
|
||||
else if (strcmp(field, "multi") == 0)
|
||||
{
|
||||
@@ -919,10 +923,10 @@ BuildUpdateSpec(bson_iter_t *updateIter)
|
||||
{
|
||||
EnsureTopLevelFieldType("update.updates.arrayFilters", updateIter,
|
||||
BSON_TYPE_ARRAY);
|
||||
const bson_value_t *bsonValue = bson_iter_value(updateIter);
|
||||
|
||||
/* we keep arrayFilters in projected form to preserve the type */
|
||||
arrayFilters = BsonValueToDocumentPgbson(bsonValue);
|
||||
|
||||
arrayFilters = palloc(sizeof(bson_value_t));
|
||||
*arrayFilters = *bson_iter_value(updateIter);
|
||||
}
|
||||
else if (strcmp(field, "collation") == 0)
|
||||
{
|
||||
@@ -1210,6 +1214,8 @@ DoUnshardedMultiUpdateViaUpdateWorker(MongoCollection *collection, List *updates
|
||||
ReThrowError(errorData);
|
||||
}
|
||||
PG_END_TRY();
|
||||
|
||||
pfree(workerSpec);
|
||||
}
|
||||
|
||||
|
||||
@@ -1445,9 +1451,9 @@ ProcessUpdate(MongoCollection *collection, UpdateSpec *updateSpec,
|
||||
text *transactionId, UpdateResult *result, bool forceInlineWrites,
|
||||
ExprEvalState *stateForSchemaValidation)
|
||||
{
|
||||
pgbson *query = updateSpec->updateOneParams.query;
|
||||
pgbson *update = updateSpec->updateOneParams.update;
|
||||
pgbson *arrayFilters = updateSpec->updateOneParams.arrayFilters;
|
||||
const bson_value_t *query = updateSpec->updateOneParams.query;
|
||||
const bson_value_t *update = updateSpec->updateOneParams.update;
|
||||
const bson_value_t *arrayFilters = updateSpec->updateOneParams.arrayFilters;
|
||||
bool isUpsert = updateSpec->updateOneParams.isUpsert;
|
||||
bool isMulti = updateSpec->isMulti;
|
||||
|
||||
@@ -1455,8 +1461,9 @@ ProcessUpdate(MongoCollection *collection, UpdateSpec *updateSpec,
|
||||
int64 shardKeyHash = 0;
|
||||
bool isShardKeyValueCollationAware = false;
|
||||
bool hasShardKeyValueFilter =
|
||||
ComputeShardKeyHashForQuery(collection->shardKey, collection->collectionId, query,
|
||||
&shardKeyHash, &isShardKeyValueCollationAware);
|
||||
ComputeShardKeyHashForQueryValue(collection->shardKey, collection->collectionId,
|
||||
query,
|
||||
&shardKeyHash, &isShardKeyValueCollationAware);
|
||||
|
||||
result->rowsMatched = 0;
|
||||
result->rowsModified = 0;
|
||||
@@ -1535,7 +1542,7 @@ ProcessUpdate(MongoCollection *collection, UpdateSpec *updateSpec,
|
||||
{
|
||||
/* determine whether query filters by a single object ID */
|
||||
bson_iter_t queryDocIter;
|
||||
PgbsonInitIterator(query, &queryDocIter);
|
||||
BsonValueInitIterator(query, &queryDocIter);
|
||||
bson_value_t idFromQueryDocument = { 0 };
|
||||
bool errorOnConflict = false;
|
||||
bool queryHasNonIdFilters = false;
|
||||
@@ -1581,18 +1588,13 @@ ProcessUpdate(MongoCollection *collection, UpdateSpec *updateSpec,
|
||||
* number of updated rows and matched documents for the query.
|
||||
*/
|
||||
static UpdateAllMatchingDocsResult
|
||||
UpdateAllMatchingDocuments(MongoCollection *collection, pgbson *queryDoc,
|
||||
pgbson *updateDoc, pgbson *arrayFilters,
|
||||
UpdateAllMatchingDocuments(MongoCollection *collection, const bson_value_t *queryDocValue,
|
||||
const bson_value_t *updateDocValue, const
|
||||
bson_value_t *arrayFiltersValue,
|
||||
bool hasShardKeyValueFilter, int64 shardKeyHash,
|
||||
ExprEvalState *schemaValidationExprEvalState,
|
||||
bool *hasOnlyObjectIdFilter)
|
||||
{
|
||||
bool queryHasNonIdFilters = false;
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(queryDoc,
|
||||
&queryHasNonIdFilters);
|
||||
|
||||
*hasOnlyObjectIdFilter = objectIdFilter != NULL && !queryHasNonIdFilters;
|
||||
|
||||
const char *tableName = collection->tableName;
|
||||
bool isLocalShardQuery = false;
|
||||
if (collection->shardTableName[0] != '\0')
|
||||
@@ -1613,7 +1615,21 @@ UpdateAllMatchingDocuments(MongoCollection *collection, pgbson *queryDoc,
|
||||
memset(&result, 0, sizeof(UpdateAllMatchingDocsResult));
|
||||
|
||||
SPI_connect();
|
||||
initStringInfo(&updateQuery);
|
||||
|
||||
pgbson *queryDoc = PgbsonInitFromDocumentBsonValue(queryDocValue);
|
||||
|
||||
/* Here we need to create a document wrapper to preserve the type */
|
||||
pgbson *updateDoc = BsonValueToDocumentPgbson(updateDocValue);
|
||||
pgbson *arrayFilters = arrayFiltersValue == NULL ? NULL :
|
||||
BsonValueToDocumentPgbson(arrayFiltersValue);
|
||||
|
||||
/* Do these under the SPI Context so that they get deleted automatically at the end */
|
||||
bool queryHasNonIdFilters = false;
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(queryDoc,
|
||||
&queryHasNonIdFilters);
|
||||
|
||||
*hasOnlyObjectIdFilter = objectIdFilter != NULL && !queryHasNonIdFilters;
|
||||
|
||||
|
||||
/* We use a CTE with the UPDATE document and then select the count of number of rows to get the matched docs,
|
||||
* and the sum of the RETURNING value, which returns 1 if a document was updated and 0
|
||||
@@ -1635,6 +1651,7 @@ UpdateAllMatchingDocuments(MongoCollection *collection, pgbson *queryDoc,
|
||||
* value if no update is needed is with the multi CTE approach mentioned above, which is a lot slower.
|
||||
*
|
||||
*/
|
||||
initStringInfo(&updateQuery);
|
||||
if (EnableSchemaValidation && schemaValidationExprEvalState != NULL)
|
||||
{
|
||||
/*
|
||||
@@ -1895,7 +1912,7 @@ UpdateOne(MongoCollection *collection, UpdateOneParams *updateOneParams,
|
||||
|
||||
/* If we have to reinsert the document in a different shard */
|
||||
bool queryHasNonIdFilters = false;
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocumentValue(
|
||||
updateOneParams->query, &queryHasNonIdFilters);
|
||||
bool hasOnlyObjectIdFilter = objectIdFilter != NULL && !queryHasNonIdFilters;
|
||||
|
||||
@@ -2019,12 +2036,13 @@ CallUpdateWorkerForUpdateOne(MongoCollection *collection,
|
||||
result->resultDocument = NULL;
|
||||
result->upsertedObjectId = NULL;
|
||||
|
||||
Datum resultDatum = CallUpdateWorker(collection, SerializeUpdateOneParams(
|
||||
updateOneParams, collection->shardKey), NULL,
|
||||
pgbson *workerParams = SerializeUpdateOneParams(updateOneParams,
|
||||
collection->shardKey);
|
||||
Datum resultDatum = CallUpdateWorker(collection, workerParams, NULL,
|
||||
shardKeyHash, transactionId);
|
||||
|
||||
pfree(workerParams);
|
||||
pgbson *resultPgbson = DatumGetPgBson(resultDatum);
|
||||
|
||||
DeserializeUpdateOneResult(resultPgbson, result);
|
||||
}
|
||||
|
||||
@@ -2294,26 +2312,22 @@ WriteUpdateOneParamsAsUpdateSpec(UpdateOneParams *params, pgbson_writer *writer)
|
||||
{
|
||||
if (params->query != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(writer, "q", 1, params->query);
|
||||
PgbsonWriterAppendValue(writer, "q", 1, params->query);
|
||||
}
|
||||
|
||||
pgbsonelement updateElement;
|
||||
PgbsonToSinglePgbsonElement(params->update, &updateElement);
|
||||
PgbsonWriterAppendValue(writer, "u", 1, &updateElement.bsonValue);
|
||||
PgbsonWriterAppendValue(writer, "u", 1, params->update);
|
||||
|
||||
PgbsonWriterAppendBool(writer, "upsert", -1, params->isUpsert != 0);
|
||||
|
||||
if (params->sort != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(writer, "sort", 4, params->sort);
|
||||
PgbsonWriterAppendValue(writer, "sort", 4, params->sort);
|
||||
}
|
||||
|
||||
if (params->arrayFilters != NULL)
|
||||
{
|
||||
pgbsonelement arrayFilterElement = { 0 };
|
||||
PgbsonToSinglePgbsonElement(params->arrayFilters, &arrayFilterElement);
|
||||
PgbsonWriterAppendValue(writer, "arrayFilters", 12,
|
||||
&arrayFilterElement.bsonValue);
|
||||
params->arrayFilters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2369,7 +2383,7 @@ SerializeUpdateOneParams(UpdateOneParams *params, pgbson *shardKeyBson)
|
||||
|
||||
if (params->query != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "query", -1, params->query);
|
||||
PgbsonWriterAppendValue(&writer, "query", -1, params->query);
|
||||
}
|
||||
|
||||
if (shardKeyBson != NULL)
|
||||
@@ -2377,7 +2391,14 @@ SerializeUpdateOneParams(UpdateOneParams *params, pgbson *shardKeyBson)
|
||||
PgbsonWriterAppendDocument(&writer, "shardKeyBson", -1, shardKeyBson);
|
||||
}
|
||||
|
||||
PgbsonWriterAppendDocument(&writer, "update", -1, params->update);
|
||||
/* Back-compat (to handle new coordinator, old worker)
|
||||
* write it as a document that is { "": value }
|
||||
* TODO: See if this can be elided in the future
|
||||
*/
|
||||
pgbson_writer valueWriter;
|
||||
PgbsonWriterStartDocument(&writer, "update", -1, &valueWriter);
|
||||
PgbsonWriterAppendValue(&valueWriter, "", 0, params->update);
|
||||
PgbsonWriterEndDocument(&writer, &valueWriter);
|
||||
|
||||
PgbsonWriterAppendBool(&writer, "isUpsert", -1, params->isUpsert != 0);
|
||||
|
||||
@@ -2386,19 +2407,26 @@ SerializeUpdateOneParams(UpdateOneParams *params, pgbson *shardKeyBson)
|
||||
|
||||
if (params->sort != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "sort", 4, params->sort);
|
||||
PgbsonWriterAppendValue(&writer, "sort", 4, params->sort);
|
||||
}
|
||||
|
||||
PgbsonWriterAppendInt32(&writer, "returnDocument", -1, (int) params->returnDocument);
|
||||
|
||||
if (params->returnFields != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "returnFields", -1, params->returnFields);
|
||||
PgbsonWriterAppendValue(&writer, "returnFields", -1, params->returnFields);
|
||||
}
|
||||
|
||||
if (params->arrayFilters != NULL)
|
||||
{
|
||||
PgbsonWriterAppendDocument(&writer, "arrayFilters", -1, params->arrayFilters);
|
||||
/*
|
||||
* back compat: To handle new coordinator old worker scenario, write it in the
|
||||
* prior format of { "": value }
|
||||
* TODO: See if this can be elided in the future
|
||||
*/
|
||||
PgbsonWriterStartDocument(&writer, "arrayFilters", -1, &valueWriter);
|
||||
PgbsonWriterAppendValue(&valueWriter, "", 0, params->arrayFilters);
|
||||
PgbsonWriterEndDocument(&writer, &valueWriter);
|
||||
}
|
||||
|
||||
PgbsonWriterEndDocument(&commandWriter, &writer);
|
||||
@@ -2515,27 +2543,34 @@ DeserializeUpdateWorkerSpec(pgbson *updateInternalSpec,
|
||||
}
|
||||
else if (strcmp(key, "arrayFilters") == 0)
|
||||
{
|
||||
updateOneParams->arrayFilters = PgbsonInitFromDocumentBsonValue(
|
||||
bson_iter_value(&internalIter));
|
||||
/* From a compat perspective, this is serialized as { "": value } */
|
||||
pgbsonelement arrayFilterElement;
|
||||
const bson_value_t *itervalue = bson_iter_value(&internalIter);
|
||||
BsonValueToPgbsonElement(itervalue, &arrayFilterElement);
|
||||
updateOneParams->arrayFilters = CreateBsonValueCopy(
|
||||
&arrayFilterElement.bsonValue);
|
||||
}
|
||||
else if (strcmp(key, "query") == 0)
|
||||
{
|
||||
updateOneParams->query = PgbsonInitFromDocumentBsonValue(bson_iter_value(
|
||||
&internalIter));
|
||||
updateOneParams->query = CreateBsonValueCopy(bson_iter_value(
|
||||
&internalIter));
|
||||
}
|
||||
else if (strcmp(key, "sort") == 0)
|
||||
{
|
||||
updateOneParams->sort = PgbsonInitFromDocumentBsonValue(bson_iter_value(
|
||||
&internalIter));
|
||||
updateOneParams->sort = CreateBsonValueCopy(bson_iter_value(
|
||||
&internalIter));
|
||||
}
|
||||
else if (strcmp(key, "update") == 0)
|
||||
{
|
||||
updateOneParams->update = PgbsonInitFromDocumentBsonValue(bson_iter_value(
|
||||
&internalIter));
|
||||
pgbsonelement updateElement;
|
||||
const bson_value_t *itervalue = bson_iter_value(&internalIter);
|
||||
BsonValueToPgbsonElement(itervalue, &updateElement);
|
||||
updateOneParams->update = CreateBsonValueCopy(
|
||||
&updateElement.bsonValue);
|
||||
}
|
||||
else if (strcmp(key, "returnFields") == 0)
|
||||
{
|
||||
updateOneParams->returnFields = PgbsonInitFromDocumentBsonValue(
|
||||
updateOneParams->returnFields = CreateBsonValueCopy(
|
||||
bson_iter_value(&internalIter));
|
||||
}
|
||||
else if (strcmp(key, "returnDocument") == 0)
|
||||
@@ -2969,7 +3004,7 @@ UpdateOneInternal(MongoCollection *collection, UpdateOneParams *updateOneParams,
|
||||
bool forceProjectId = false;
|
||||
bool allowInclusionExclusion = false;
|
||||
bson_iter_t projectIter;
|
||||
PgbsonInitIterator(updateOneParams->returnFields, &projectIter);
|
||||
BsonValueInitIterator(updateOneParams->returnFields, &projectIter);
|
||||
|
||||
const BsonProjectionQueryState *projectionState =
|
||||
GetProjectionStateForBsonProject(&projectIter,
|
||||
@@ -2987,15 +3022,20 @@ UpdateOneInternal(MongoCollection *collection, UpdateOneParams *updateOneParams,
|
||||
*/
|
||||
static bool
|
||||
SelectUpdateCandidate(uint64 collectionId, const char *shardTableName, int64 shardKeyHash,
|
||||
pgbson *query,
|
||||
pgbson *update, pgbson *arrayFilters, pgbson *sort,
|
||||
const bson_value_t *queryValue,
|
||||
const bson_value_t *update, const bson_value_t *arrayFilters,
|
||||
const bson_value_t *sort,
|
||||
UpdateCandidate *updateCandidate, bool getOriginalDocument,
|
||||
bool *hasOnlyObjectIdFilter)
|
||||
{
|
||||
uint64 planId = QUERY_UPDATE_SELECT_UPDATE_CANDIDATE;
|
||||
StringInfoData updateQuery;
|
||||
List *sortFieldDocuments = sort != NULL ? PgbsonDecomposeFields(sort) : NIL;
|
||||
SPI_connect();
|
||||
|
||||
StringInfoData updateQuery;
|
||||
List *sortFieldDocuments = sort != NULL ? BsonValueDocumentDecomposeFields(sort) :
|
||||
NIL;
|
||||
|
||||
pgbson *query = PgbsonInitFromDocumentBsonValue(queryValue);
|
||||
bool queryHasNonIdFilters = false;
|
||||
pgbson *objectIdFilter = GetObjectIdFilterFromQueryDocument(query,
|
||||
&queryHasNonIdFilters);
|
||||
@@ -3013,8 +3053,6 @@ SelectUpdateCandidate(uint64 collectionId, const char *shardTableName, int64 sha
|
||||
bool foundDocument = false;
|
||||
int varArgsOffset = 2;
|
||||
|
||||
SPI_connect();
|
||||
|
||||
initStringInfo(&updateQuery);
|
||||
|
||||
appendStringInfo(&updateQuery, "SELECT ctid, object_id, document FROM");
|
||||
@@ -3147,7 +3185,7 @@ SelectUpdateCandidate(uint64 collectionId, const char *shardTableName, int64 sha
|
||||
|
||||
/* Do this inside the SPI context so that the memory gets cleaned up once we close the SPI session. */
|
||||
pgbson *originalDoc = DatumGetPgBson(originalDocumentDatum);
|
||||
pgbson *updatedDocument = BsonUpdateDocument(originalDoc, update, query,
|
||||
pgbson *updatedDocument = BsonUpdateDocument(originalDoc, update, queryValue,
|
||||
arrayFilters);
|
||||
|
||||
if (updatedDocument != NULL)
|
||||
@@ -3379,8 +3417,8 @@ UpdateOneObjectId(MongoCollection *collection, UpdateOneParams *updateOneParams,
|
||||
* and returns the inserted object ID.
|
||||
*/
|
||||
static pgbson *
|
||||
UpsertDocument(MongoCollection *collection, pgbson *update,
|
||||
pgbson *query, pgbson *arrayFilters,
|
||||
UpsertDocument(MongoCollection *collection, const bson_value_t *update,
|
||||
const bson_value_t *query, const bson_value_t *arrayFilters,
|
||||
ExprEvalState *stateForSchemaValidation,
|
||||
bool hasOnlyObjectIdFilter)
|
||||
{
|
||||
@@ -3445,7 +3483,7 @@ ValidateQueryAndUpdateDocuments(BatchUpdateSpec *batchSpec)
|
||||
MemoryContext oldContext = CurrentMemoryContext;
|
||||
PG_TRY();
|
||||
{
|
||||
ValidateQueryDocument(updateSpec->updateOneParams.query);
|
||||
ValidateQueryDocumentValue(updateSpec->updateOneParams.query);
|
||||
ValidateUpdateDocument(updateSpec->updateOneParams.update,
|
||||
updateSpec->updateOneParams.query,
|
||||
updateSpec->updateOneParams.arrayFilters);
|
||||
|
||||
@@ -546,7 +546,7 @@ CreateQualForBsonValueArrayExpression(const bson_value_t *expression)
|
||||
* typically supplied by an @@ operator).
|
||||
*/
|
||||
List *
|
||||
CreateQualsForBsonValueTopLevelQuery(const pgbson *query)
|
||||
CreateQualsForBsonValueTopLevelQueryIter(bson_iter_t *queryIter)
|
||||
{
|
||||
Var *var = makeVar(1, 1, INTERNALOID, -1, DEFAULT_COLLATION_OID, 0);
|
||||
BsonQueryOperatorContext context = { 0 };
|
||||
@@ -557,9 +557,7 @@ CreateQualsForBsonValueTopLevelQuery(const pgbson *query)
|
||||
context.requiredFilterPathNameHashSet = NULL;
|
||||
context.variableContext = NULL;
|
||||
|
||||
bson_iter_t queryDocIterator;
|
||||
PgbsonInitIterator(query, &queryDocIterator);
|
||||
return CreateQualsFromQueryDocIterator(&queryDocIterator, &context);
|
||||
return CreateQualsFromQueryDocIterator(queryIter, &context);
|
||||
}
|
||||
|
||||
|
||||
@@ -1116,8 +1114,23 @@ IsValidBsonDocumentForDollarInOrNinOp(const bson_value_t *value)
|
||||
void
|
||||
ValidateQueryDocument(pgbson *queryDocument)
|
||||
{
|
||||
bson_value_t queryValue = ConvertPgbsonToBsonValue(queryDocument);
|
||||
return ValidateQueryDocumentValue(&queryValue);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValidateQueryDocumentValue(const bson_value_t *value)
|
||||
{
|
||||
if (value->value_type != BSON_TYPE_DOCUMENT)
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
|
||||
errmsg("Validate query must be given a document - not: %s",
|
||||
BsonTypeName(value->value_type))));
|
||||
}
|
||||
|
||||
bson_iter_t queryDocIter;
|
||||
PgbsonInitIterator(queryDocument, &queryDocIter);
|
||||
BsonValueInitIterator(value, &queryDocIter);
|
||||
|
||||
BsonQueryOperatorContext context = {
|
||||
.documentExpr = (Expr *) MakeSimpleDocumentVar(),
|
||||
|
||||
@@ -109,10 +109,6 @@ static bool ComputeShardKeyFieldValuesHash(ShardKeyFieldValues *shardKeyValues,
|
||||
static void ValidateShardKey(const pgbson *shardKeyDoc);
|
||||
static void FindShardKeyFieldValuesForQuery(bson_iter_t *queryDocument,
|
||||
ShardKeyFieldValues *shardKeyValues);
|
||||
static bool ComputeShardKeyHashForQueryValue(pgbson *shardKey, uint64_t collectionId,
|
||||
const bson_value_t *query,
|
||||
int64 *shardKeyHash,
|
||||
bool *isShardKeyValueCollationAware);
|
||||
static Expr * FindShardKeyValuesExpr(bson_iter_t *queryDocIter, pgbson *shardKey, int
|
||||
collectionVarno, ShardKeyFieldValues *fieldValues,
|
||||
bool *isShardKeyValueCollationAware);
|
||||
@@ -588,7 +584,7 @@ CreateShardKeyValueFilter(int collectionVarno, Const *valueConst)
|
||||
* ComputeShardKeyHashForQueryValue returns whether the given query filters all
|
||||
* shard key fields by a specific value and computes the hash of the values.
|
||||
*/
|
||||
static bool
|
||||
bool
|
||||
ComputeShardKeyHashForQueryValue(pgbson *shardKey, uint64_t collectionId,
|
||||
const bson_value_t *query, int64 *shardKeyHash,
|
||||
bool *isShardKeyValueCollationAware)
|
||||
|
||||
@@ -325,7 +325,7 @@ SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_upd
|
||||
{ "_id" : { "$numberInt" : "1" }, "expdata" : { "$numberInt" : "5" } }
|
||||
(1 row)
|
||||
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1}', '{ "": { "$bit": { "expdata" : { "or" : 10 }} }, "upsert" : false}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1}', '{ "": { "$bit": { "expdata" : { "or" : 10 }} }}', '{}');
|
||||
bson_update_document
|
||||
-------------------------------------------------------------------------
|
||||
{ "_id" : { "$numberInt" : "1" }, "expdata" : { "$numberInt" : "10" } }
|
||||
|
||||
@@ -93,7 +93,7 @@ SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_upd
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "expdata" :[13,3,1]}', '{ "": { "$bit": { "expdata.0" : { "and" :10 } } }}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "expdata" :13}', '{ "": { "$bit": { "expdata" : { "and" : 0, "or" : 10 }} }}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "expdata" :13}', '{ "": { "$bit": { "expdata" : { "or" : 10, "xor" : 10 }} }}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1}', '{ "": { "$bit": { "expdata" : { "or" : 10 }} }, "upsert" : false}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1}', '{ "": { "$bit": { "expdata" : { "or" : 10 }} }}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "key": { "x": { "y": [ 100, 200 ]}}, "f": 12 }', '{ "": { "$bit": {"key.x.y.0": {"and": 10}, "f": {"and": 10 }}}}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "key": { "x": { "y": 10 }}, "f": 1}', '{ "": { "$bit": { "key.x.y": { "xor": 10 } }}}', '{}');
|
||||
SELECT newDocument as bson_update_document FROM documentdb_api_internal.bson_update_document('{"_id": 1, "key": {"x": { "y": [100 , 200 ] } }, "f": 1 } ', '{ "": { "$bit": { "key.x.y.0": { "and": 10 } }}}', '{}');
|
||||
|
||||
@@ -78,17 +78,21 @@ typedef struct
|
||||
/* Forward declaration */
|
||||
/* --------------------------------------------------------- */
|
||||
|
||||
static void BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, pgbson *updateSpec,
|
||||
pgbson *querySpec, pgbson *arrayFilters,
|
||||
static void BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata,
|
||||
const bson_value_t *updateSpec,
|
||||
const bson_value_t *querySpec, const
|
||||
bson_value_t *arrayFilters,
|
||||
bool buildSourceDocOnUpsert);
|
||||
|
||||
static pgbson * BsonUpdateDocumentCore(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
static pgbson * BsonUpdateDocumentCore(pgbson *sourceDocument, const
|
||||
bson_value_t *updateSpec,
|
||||
BsonUpdateMetadata *metadata);
|
||||
|
||||
static pgbson * ProcessReplaceDocument(pgbson *sourceDoc, pgbson *updateSpec,
|
||||
static pgbson * ProcessReplaceDocument(pgbson *sourceDoc, const bson_value_t *updateSpec,
|
||||
bool isUpsert);
|
||||
|
||||
static pgbson * BuildBsonDocumentFromQuery(pgbson *sourceDoc, pgbson *querySpec,
|
||||
static pgbson * BuildBsonDocumentFromQuery(pgbson *sourceDoc, const
|
||||
bson_value_t *querySpec,
|
||||
UpdateType updateType);
|
||||
|
||||
static void ProcessQueryProjectionValue(void *context, const char *path, const
|
||||
@@ -171,10 +175,21 @@ bson_update_document(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
pgbson *sourceDocument = PG_GETARG_PGBSON(0);
|
||||
pgbson *updateSpec = PG_GETARG_PGBSON(1);
|
||||
pgbson *querySpec = PG_GETARG_PGBSON(2);
|
||||
pgbson *arrayFilters = PG_GETARG_MAYBE_NULL_PGBSON(3);
|
||||
pgbson *updateSpecDoc = PG_GETARG_PGBSON(1);
|
||||
pgbson *querySpecDoc = PG_GETARG_PGBSON(2);
|
||||
pgbson *arrayFiltersDoc = PG_GETARG_MAYBE_NULL_PGBSON(3);
|
||||
|
||||
pgbsonelement updateSpecElement;
|
||||
PgbsonToSinglePgbsonElement(updateSpecDoc, &updateSpecElement);
|
||||
bson_value_t querySpec = ConvertPgbsonToBsonValue(querySpecDoc);
|
||||
pgbsonelement arrayFiltersBase = { 0 };
|
||||
bson_value_t *arrayFilters = NULL;
|
||||
|
||||
if (arrayFiltersDoc != NULL)
|
||||
{
|
||||
PgbsonToSinglePgbsonElement(arrayFiltersDoc, &arrayFiltersBase);
|
||||
arrayFilters = &arrayFiltersBase.bsonValue;
|
||||
}
|
||||
|
||||
/* an empty document is treated as an upsert. */
|
||||
bool buildSourceDocOnUpsert = IsPgbsonEmptyDocument(sourceDocument);
|
||||
@@ -189,7 +204,7 @@ bson_update_document(PG_FUNCTION_ARGS)
|
||||
&stateArgPositions[0],
|
||||
3,
|
||||
BuildBsonUpdateMetadata,
|
||||
updateSpec, querySpec, arrayFilters,
|
||||
&updateSpecElement.bsonValue, &querySpec, arrayFilters,
|
||||
buildSourceDocOnUpsert);
|
||||
|
||||
if (metadata == NULL)
|
||||
@@ -215,7 +230,8 @@ bson_update_document(PG_FUNCTION_ARGS)
|
||||
elog(ERROR, "incorrect number of output arguments");
|
||||
}
|
||||
|
||||
pgbson *document = BsonUpdateDocumentCore(sourceDocument, updateSpec, metadata);
|
||||
pgbson *document = BsonUpdateDocumentCore(sourceDocument,
|
||||
&updateSpecElement.bsonValue, metadata);
|
||||
|
||||
if (document != NULL)
|
||||
{
|
||||
@@ -255,7 +271,8 @@ bson_update_returned_value(PG_FUNCTION_ARGS)
|
||||
* can be used to validate given update document.
|
||||
*/
|
||||
void
|
||||
ValidateUpdateDocument(pgbson *updateSpec, pgbson *querySpec, pgbson *arrayFilters)
|
||||
ValidateUpdateDocument(const bson_value_t *updateSpec, const bson_value_t *querySpec,
|
||||
const bson_value_t *arrayFilters)
|
||||
{
|
||||
BsonUpdateMetadata metadata = { 0 };
|
||||
bool buildSourceDocOnUpsert = false;
|
||||
@@ -269,8 +286,8 @@ ValidateUpdateDocument(pgbson *updateSpec, pgbson *querySpec, pgbson *arrayFilte
|
||||
* returns NULL if no update is needed.
|
||||
*/
|
||||
pgbson *
|
||||
BsonUpdateDocument(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
pgbson *querySpec, pgbson *arrayFilters)
|
||||
BsonUpdateDocument(pgbson *sourceDocument, const bson_value_t *updateSpec,
|
||||
const bson_value_t *querySpec, const bson_value_t *arrayFilters)
|
||||
{
|
||||
BsonUpdateMetadata metadata = { 0 };
|
||||
|
||||
@@ -294,7 +311,7 @@ BsonUpdateDocument(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
* Returns NULL if update was a no-op.
|
||||
*/
|
||||
pgbson *
|
||||
BsonUpdateDocumentCore(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
BsonUpdateDocumentCore(pgbson *sourceDocument, const bson_value_t *updateSpec,
|
||||
BsonUpdateMetadata *updateMetadata)
|
||||
{
|
||||
bson_iter_t sourceDocumentIterator;
|
||||
@@ -396,8 +413,8 @@ BsonUpdateDocumentCore(pgbson *sourceDocument, pgbson *updateSpec,
|
||||
* into the metadata value provided.
|
||||
*/
|
||||
static void
|
||||
BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, pgbson *updateSpec,
|
||||
pgbson *querySpec, pgbson *arrayFilters,
|
||||
BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, const bson_value_t *updateSpec,
|
||||
const bson_value_t *querySpec, const bson_value_t *arrayFilters,
|
||||
bool buildSourceDocOnUpsert)
|
||||
{
|
||||
metadata->updateType = DetermineUpdateType(updateSpec);
|
||||
@@ -417,9 +434,7 @@ BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, pgbson *updateSpec,
|
||||
{
|
||||
if (arrayFilters != NULL)
|
||||
{
|
||||
pgbsonelement arrayFiltersElement = { 0 };
|
||||
PgbsonToSinglePgbsonElement(arrayFilters, &arrayFiltersElement);
|
||||
if (!IsBsonValueEmptyArray(&arrayFiltersElement.bsonValue))
|
||||
if (!IsBsonValueEmptyArray(arrayFilters))
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE),
|
||||
errmsg(
|
||||
@@ -442,9 +457,7 @@ BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, pgbson *updateSpec,
|
||||
case UpdateType_ReplaceDocument:
|
||||
{
|
||||
/* Simply validate the replace doc */
|
||||
bson_iter_t updateIterator;
|
||||
PgbsonInitIteratorAtPath(updateSpec, "", &updateIterator);
|
||||
if (!BSON_ITER_HOLDS_DOCUMENT(&updateIterator))
|
||||
if (updateSpec->value_type != BSON_TYPE_DOCUMENT)
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
|
||||
"Replace should be a document")));
|
||||
@@ -468,25 +481,18 @@ BuildBsonUpdateMetadata(BsonUpdateMetadata *metadata, pgbson *updateSpec,
|
||||
* Otherwise it's a replace.
|
||||
*/
|
||||
UpdateType
|
||||
DetermineUpdateType(pgbson *updateSpec)
|
||||
DetermineUpdateType(const bson_value_t *updateSpec)
|
||||
{
|
||||
bson_iter_t updateIterator;
|
||||
bson_iter_t updateDocumentIterator;
|
||||
bool isUpdateTypeReplacement = false;
|
||||
|
||||
if (!PgbsonInitIteratorAtPath(updateSpec, "", &updateIterator))
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg(
|
||||
"Update should be specified")));
|
||||
}
|
||||
|
||||
if (BSON_ITER_HOLDS_ARRAY(&updateIterator))
|
||||
if (updateSpec->value_type == BSON_TYPE_ARRAY)
|
||||
{
|
||||
return UpdateType_AggregationPipeline;
|
||||
}
|
||||
else if (BSON_ITER_HOLDS_DOCUMENT(&updateIterator) &&
|
||||
bson_iter_recurse(&updateIterator, &updateDocumentIterator))
|
||||
else if (updateSpec->value_type == BSON_TYPE_DOCUMENT)
|
||||
{
|
||||
BsonValueInitIterator(updateSpec, &updateDocumentIterator);
|
||||
while (bson_iter_next(&updateDocumentIterator))
|
||||
{
|
||||
const char *path = bson_iter_key(&updateDocumentIterator);
|
||||
@@ -532,23 +538,20 @@ DetermineUpdateType(pgbson *updateSpec)
|
||||
* from the filters into the target document.
|
||||
*/
|
||||
static pgbson *
|
||||
ProcessReplaceDocument(pgbson *sourceDoc, pgbson *updateSpec,
|
||||
ProcessReplaceDocument(pgbson *sourceDoc, const bson_value_t *updateSpec,
|
||||
bool isUpsert)
|
||||
{
|
||||
bson_iter_t sourceDocIterator;
|
||||
bson_iter_t updateIterator;
|
||||
bson_iter_t replaceDocumentIterator;
|
||||
pgbson_writer writer;
|
||||
PgbsonInitIteratorAtPath(updateSpec, "", &updateIterator);
|
||||
if (!BSON_ITER_HOLDS_DOCUMENT(&updateIterator))
|
||||
if (updateSpec->value_type != BSON_TYPE_DOCUMENT)
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
|
||||
"Replace should be a document")));
|
||||
}
|
||||
|
||||
uint32_t documentLength;
|
||||
const uint8_t *documentBytes;
|
||||
bson_iter_document(&updateIterator, &documentLength, &documentBytes);
|
||||
uint32_t documentLength = updateSpec->value.v_doc.data_len;
|
||||
const uint8_t *documentBytes = updateSpec->value.v_doc.data;
|
||||
|
||||
/* validate the replace document. */
|
||||
ValidateInputBsonBytes(documentBytes,
|
||||
@@ -652,13 +655,13 @@ ProcessReplaceDocument(pgbson *sourceDoc, pgbson *updateSpec,
|
||||
* will be used in the upsert case as the initial document.
|
||||
*/
|
||||
static pgbson *
|
||||
BuildBsonDocumentFromQuery(pgbson *sourceDoc, pgbson *querySpec,
|
||||
BuildBsonDocumentFromQuery(pgbson *sourceDoc, const bson_value_t *querySpec,
|
||||
UpdateType updateType)
|
||||
{
|
||||
BsonIntermediatePathNode *root = MakeRootNode();
|
||||
|
||||
bson_iter_t queryDocIterator;
|
||||
PgbsonInitIterator(querySpec, &queryDocIterator);
|
||||
BsonValueInitIterator(querySpec, &queryDocIterator);
|
||||
QueryProjectionContext context = { .root = root, .updateType = updateType };
|
||||
|
||||
bool isUpsert = true;
|
||||
|
||||
@@ -163,17 +163,16 @@ static MongoUpdateAggregationOperator AggregationOperators[] =
|
||||
* the pipeline against documents to produce modified documents.
|
||||
*/
|
||||
struct AggregationPipelineUpdateState *
|
||||
GetAggregationPipelineUpdateState(pgbson *updateSpec)
|
||||
GetAggregationPipelineUpdateState(const bson_value_t *updateSpec)
|
||||
{
|
||||
bson_iter_t updateIterator;
|
||||
PgbsonInitIteratorAtPath(updateSpec, "", &updateIterator);
|
||||
if (!BSON_ITER_HOLDS_ARRAY(&updateIterator) ||
|
||||
!bson_iter_recurse(&updateIterator, &updateIterator))
|
||||
if (updateSpec->value_type != BSON_TYPE_ARRAY)
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
|
||||
"aggregation pipeline should be an array")));
|
||||
}
|
||||
|
||||
bson_iter_t updateIterator;
|
||||
BsonValueInitIterator(updateSpec, &updateIterator);
|
||||
List *aggregationStages = NIL;
|
||||
while (bson_iter_next(&updateIterator))
|
||||
{
|
||||
|
||||
@@ -305,8 +305,9 @@ static void HandleSetOnInsertUpdateTree(BsonIntermediatePathNode *tree,
|
||||
static uint32_t ArrayFiltersKeyHash(const void *key, Size keysize);
|
||||
static int ArrayFiltersKeyCompare(const void *obj1, const void *obj2, Size objsize);
|
||||
static void WriteCurrentArrayFilterValue(pgbson_writer *writer, bson_value_t *entryValue);
|
||||
static HTAB * BuildExpressionForArrayFilters(pgbson *arrayFilters);
|
||||
static void PostValidateArrayFilters(HTAB *arrayFiltersHash, pgbson *updateSpec);
|
||||
static HTAB * BuildExpressionForArrayFilters(const bson_value_t *arrayFilters);
|
||||
static void PostValidateArrayFilters(HTAB *arrayFiltersHash, const
|
||||
bson_value_t *updateSpec);
|
||||
|
||||
/* Value writer functions */
|
||||
static bool HandleUpdateDocumentId(pgbson_writer *writer,
|
||||
@@ -615,18 +616,17 @@ RegisterUpdateOperatorExtension(const MongoUpdateOperatorSpec *extensibleDefinit
|
||||
* and can be reused in processing document updates.
|
||||
*/
|
||||
const BsonIntermediatePathNode *
|
||||
GetOperatorUpdateState(pgbson *updateSpec, pgbson *querySpec,
|
||||
pgbson *arrayFilters, bool isUpsert)
|
||||
GetOperatorUpdateState(const bson_value_t *updateSpec, const bson_value_t *querySpec,
|
||||
const bson_value_t *arrayFilters, bool isUpsert)
|
||||
{
|
||||
bson_iter_t updateIterator;
|
||||
PgbsonInitIteratorAtPath(updateSpec, "", &updateIterator);
|
||||
if (!BSON_ITER_HOLDS_DOCUMENT(&updateIterator) ||
|
||||
!bson_iter_recurse(&updateIterator, &updateIterator))
|
||||
if (updateSpec->value_type != BSON_TYPE_DOCUMENT)
|
||||
{
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
|
||||
"Update should be a document")));
|
||||
}
|
||||
|
||||
bson_iter_t updateIterator;
|
||||
BsonValueInitIterator(updateSpec, &updateIterator);
|
||||
HTAB *arrayFilterHash = BuildExpressionForArrayFilters(arrayFilters);
|
||||
PositionalUpdateSpec positionalSpec =
|
||||
{
|
||||
@@ -2318,7 +2318,7 @@ ArrayFiltersKeyCompare(const void *obj1, const void *obj2, Size objsize)
|
||||
* a QueryFilterPathAndValue.
|
||||
*/
|
||||
static HTAB *
|
||||
BuildExpressionForArrayFilters(pgbson *arrayFilters)
|
||||
BuildExpressionForArrayFilters(const bson_value_t *arrayFilters)
|
||||
{
|
||||
if (arrayFilters == NULL)
|
||||
{
|
||||
@@ -2334,11 +2334,8 @@ BuildExpressionForArrayFilters(pgbson *arrayFilters)
|
||||
hash_create("Bson array filter hash table", 32, &hashBuilderInfo,
|
||||
DefaultExtensionHashFlags);
|
||||
|
||||
pgbsonelement arrayFiltersElement;
|
||||
bson_iter_t arrayIterator;
|
||||
PgbsonToSinglePgbsonElement(arrayFilters, &arrayFiltersElement);
|
||||
|
||||
BsonValueInitIterator(&arrayFiltersElement.bsonValue, &arrayIterator);
|
||||
BsonValueInitIterator(arrayFilters, &arrayIterator);
|
||||
|
||||
while (bson_iter_next(&arrayIterator))
|
||||
{
|
||||
@@ -2502,7 +2499,7 @@ WriteCurrentArrayFilterValue(pgbson_writer *writer, bson_value_t *entryValue)
|
||||
* by at least 1 update entry.
|
||||
*/
|
||||
static void
|
||||
PostValidateArrayFilters(HTAB *arrayFiltersHash, pgbson *updateSpec)
|
||||
PostValidateArrayFilters(HTAB *arrayFiltersHash, const bson_value_t *updateSpec)
|
||||
{
|
||||
if (arrayFiltersHash == NULL)
|
||||
{
|
||||
@@ -2517,10 +2514,7 @@ PostValidateArrayFilters(HTAB *arrayFiltersHash, pgbson *updateSpec)
|
||||
{
|
||||
if (!entry->filterUsed)
|
||||
{
|
||||
bson_iter_t updateSpecIter;
|
||||
PgbsonInitIteratorAtPath(updateSpec, "", &updateSpecIter);
|
||||
const bson_value_t *bsonValue = bson_iter_value(&updateSpecIter);
|
||||
const char *updateSpecStr = FormatBsonValueForShellLogging(bsonValue);
|
||||
const char *updateSpecStr = FormatBsonValueForShellLogging(updateSpec);
|
||||
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_FAILEDTOPARSE),
|
||||
errmsg(
|
||||
"The array filter for identifier \'%.*s\' was not used in the update %s",
|
||||
|
||||
Reference in new issue
Block a user