Merged PR 1629302: [Composite POC] Fix up operators and type bracketing

### Does this PR have any customer impact?
Yes

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Feature

### 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
1) Fix up the remaining operators - implementations are sub-optimal
2) Add type bracketing options for all types and use them in operators
3) Add support for truncation of terms

TODO:
- Support recheck on the index for operators than just bounds
- Support N boundaries for index terms.

----
#### AI description  (iteration 1)
#### PR Classification
Bug fix

#### PR Summary
This pull request addresses issues with operators and type bracketing in BSON composite indexes.
- Added `bson_gin_index_types_core.c` to manage BSON type bounds for indexes.
- Updated `bson_gin_composite_entrypoint.c` to fix operator handling and apply type bracketing for various BSON index strategies.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
This commit is contained in:
Vinod Sridharan authored and Santiago Fernandez Madero committed 2025-04-10 10:29:08 -06:00
1 parent 4bc38f75c6
commit 5ad889feeb
11 files changed
+1028 -89

No files matched your search

@@ -26,6 +26,6 @@ typedef struct DollarRangeParams
bool isMaxInclusive;
} DollarRangeParams;
DollarRangeParams * ParseQueryDollarRange(pgbson *rangeFilter);
DollarRangeParams * ParseQueryDollarRange(pgbsonelement *filterElement);
#endif
@@ -78,7 +78,6 @@ Datum GenerateRootExistsTerm(const IndexTermCreateMetadata *);
Datum GenerateRootNonExistsTerm(const IndexTermCreateMetadata *);
Datum GenerateRootTruncatedTerm(const IndexTermCreateMetadata *);
Datum GenerateRootMultiKeyTerm(const IndexTermCreateMetadata *);
int32_t CompareBsonIndexTerm(BsonIndexTerm *left, BsonIndexTerm *right,
bool *isComparisonValid);
@@ -0,0 +1,19 @@
/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/opclass/bson_gin_index_types_core.h
*
* Common declarations of the serialization of index terms.
*
*-------------------------------------------------------------------------
*/
#ifndef BSON_GIN_INDEX_TYPES_CORE_H
#define BSON_GIN_INDEX_TYPES_CORE_H
bson_value_t GetUpperBound(bson_type_t type, bool *isUpperBoundInclusive);
bson_value_t GetLowerBound(bson_type_t type, bool *isUpperBoundInclusive);
bson_type_t GetBsonTypeNameFromStringForDollarType(const char *typeNameStr);
#endif
@@ -5,6 +5,26 @@ CREATE OPERATOR CLASS __API_SCHEMA_INTERNAL_V2__.bson_rum_composite_path_ops
OPERATOR 3 __API_CATALOG_SCHEMA__.@>= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 4 __API_CATALOG_SCHEMA__.@< (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 5 __API_CATALOG_SCHEMA__.@<= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 6 __API_CATALOG_SCHEMA__.@*= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 7 __API_CATALOG_SCHEMA__.@!= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 8 __API_CATALOG_SCHEMA__.@!*= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 9 __API_CATALOG_SCHEMA__.@~ (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 10 __API_CATALOG_SCHEMA__.@? (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 11 __API_CATALOG_SCHEMA__.@@# (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 12 __API_CATALOG_SCHEMA__.@# (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 13 __API_CATALOG_SCHEMA__.@&= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 14 __API_CATALOG_SCHEMA__.=?= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 15 __API_CATALOG_SCHEMA__.@!& (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 16 __API_CATALOG_SCHEMA__.@!| (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 17 __API_CATALOG_SCHEMA__.@#? (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 18 __API_CATALOG_SCHEMA__.@& (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 19 __API_CATALOG_SCHEMA__.@| (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 20 __API_CATALOG_SCHEMA__.@% (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 25 __API_CATALOG_SCHEMA__.@<> (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 26 __API_SCHEMA_INTERNAL_V2__.@!> (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 27 __API_SCHEMA_INTERNAL_V2__.@!>= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 28 __API_SCHEMA_INTERNAL_V2__.@!< (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
OPERATOR 29 __API_SCHEMA_INTERNAL_V2__.@!<= (__CORE_SCHEMA__.bson, __CORE_SCHEMA__.bson),
FUNCTION 1 __API_CATALOG_SCHEMA__.gin_bson_compare(bytea, bytea),
FUNCTION 2 __API_SCHEMA_INTERNAL_V2__.gin_bson_composite_path_extract_value(__CORE_SCHEMA__.bson, internal),
FUNCTION 3 __API_SCHEMA_INTERNAL_V2__.gin_bson_composite_path_extract_query(__CORE_SCHEMA__.bson, internal, int2, internal, internal, internal, internal),
@@ -15,16 +15,12 @@
DollarRangeParams *
ParseQueryDollarRange(pgbson *rangeFilter)
ParseQueryDollarRange(pgbsonelement *filterElement)
{
pgbsonelement filterElement;
PgbsonToSinglePgbsonElement(rangeFilter, &filterElement);
DollarRangeParams *rangeParams = palloc0(sizeof(DollarRangeParams));
bson_iter_t rangeIter;
BsonValueInitIterator(&filterElement.bsonValue, &rangeIter);
BsonValueInitIterator(&filterElement->bsonValue, &rangeIter);
while (bson_iter_next(&rangeIter))
{
const char *key = bson_iter_key(&rangeIter);
@@ -27,11 +27,13 @@
#include <funcapi.h>
#include <lib/stringinfo.h>
#include "io/bson_core.h"
#include "aggregation/bson_query_common.h"
#include "opclass/bson_gin_common.h"
#include "opclass/bson_gin_private.h"
#include "opclass/bson_gin_index_mgmt.h"
#include "opclass/bson_gin_index_term.h"
#include "io/bson_core.h"
#include "opclass/bson_gin_index_types_core.h"
#include "query/bson_compare.h"
#include "utils/documentdb_errors.h"
#include "metadata/metadata_cache.h"
@@ -43,6 +45,10 @@ typedef struct CompositeSingleBound
{
bson_value_t bound;
bool isBoundInclusive;
/* The processed bound (post truncation if any) */
bson_value_t processedBoundValue;
bool isProcessedValueTruncated;
} CompositeSingleBound;
typedef struct CompositeIndexBounds
@@ -51,12 +57,16 @@ typedef struct CompositeIndexBounds
CompositeSingleBound upperBound;
bool isEqualityBound;
bool requiresRuntimeRecheck;
} CompositeIndexBounds;
typedef struct CompositeQueryRunData
{
CompositeIndexBounds indexBounds[INDEX_MAX_KEYS];
int32_t numIndexPaths;
bool hasTruncation;
bool requiresRuntimeRecheck;
} CompositeQueryRunData;
/* --------------------------------------------------------- */
@@ -86,15 +96,14 @@ static void ParseBoundsFromValue(const char **indexPaths, int32_t numPaths,
static void ParseBoundsForCompositeOperator(pgbsonelement *singleElement, const
char **indexPaths, int32_t numPaths,
CompositeIndexBounds *queryBounds);
static void ProcessBoundForQuery(CompositeSingleBound *bound, const
IndexTermCreateMetadata *metadata);
inline static bool
IsSupportedStrategy(BsonIndexStrategy strategy)
inline static int32_t
GetSinglePathTruncationLimit(int32_t compositeTruncationLimit, int32_t numPaths)
{
return (strategy == BSON_INDEX_STRATEGY_DOLLAR_EQUAL ||
strategy == BSON_INDEX_STRATEGY_DOLLAR_GREATER ||
strategy == BSON_INDEX_STRATEGY_DOLLAR_GREATER_EQUAL ||
strategy == BSON_INDEX_STRATEGY_DOLLAR_LESS ||
strategy == BSON_INDEX_STRATEGY_DOLLAR_LESS_EQUAL);
/* allocate 4 bytes for each path\0 + 1 typecode within the truncated array */
return (compositeTruncationLimit / numPaths) - 4;
}
@@ -182,7 +191,35 @@ gin_bson_composite_path_extract_query(PG_FUNCTION_ARGS)
runData->indexBounds);
}
/* Now that we have the bounds from the operators, generate index terms - we will either have
IndexTermCreateMetadata singlePathMetadata = GetIndexTermMetadata(options);
singlePathMetadata.indexTermSizeLimit =
GetSinglePathTruncationLimit(singlePathMetadata.indexTermSizeLimit,
numPaths);
/* For the next phase, process each term and handle truncation */
for (int i = 0; i < numPaths; i++)
{
if (runData->indexBounds[i].lowerBound.bound.value_type != BSON_TYPE_EOD)
{
ProcessBoundForQuery(&runData->indexBounds[i].lowerBound,
&singlePathMetadata);
runData->hasTruncation = runData->hasTruncation ||
runData->indexBounds[i].lowerBound.
isProcessedValueTruncated;
}
if (runData->indexBounds[i].upperBound.bound.value_type != BSON_TYPE_EOD)
{
ProcessBoundForQuery(&runData->indexBounds[i].upperBound,
&singlePathMetadata);
runData->hasTruncation = runData->hasTruncation ||
runData->indexBounds[i].upperBound.
isProcessedValueTruncated;
}
}
/*
* Now that we have the bounds from the operators, generate index terms - we will either have
* one or two terms.
*/
pgbson_writer lower_bound_writer;
@@ -192,9 +229,11 @@ gin_bson_composite_path_extract_query(PG_FUNCTION_ARGS)
PgbsonWriterStartArray(&lower_bound_writer, "$", 1, &lower_bound_array_writer);
bool hasInequalityMatch = false;
for (int i = 0; i < numPaths; i++)
{
runData->requiresRuntimeRecheck = runData->requiresRuntimeRecheck ||
runData->indexBounds[i].requiresRuntimeRecheck;
/* If both lower and upper bound match it's equality */
if (runData->indexBounds[i].lowerBound.bound.value_type != BSON_TYPE_EOD &&
runData->indexBounds[i].upperBound.bound.value_type != BSON_TYPE_EOD &&
@@ -204,10 +243,9 @@ gin_bson_composite_path_extract_query(PG_FUNCTION_ARGS)
&runData->indexBounds[i].upperBound.bound))
{
runData->indexBounds[i].isEqualityBound = true;
/* If it's an equality bound, we only need to generate one term */
PgbsonArrayWriterWriteValue(&lower_bound_array_writer,
&runData->indexBounds[i].lowerBound.bound);
&runData->indexBounds[i].lowerBound.
processedBoundValue);
continue;
}
@@ -216,7 +254,8 @@ gin_bson_composite_path_extract_query(PG_FUNCTION_ARGS)
{
/* There exists a lower bound for this key */
PgbsonArrayWriterWriteValue(&lower_bound_array_writer,
&runData->indexBounds[i].lowerBound.bound);
&runData->indexBounds[i].lowerBound.
processedBoundValue);
}
else
{
@@ -228,27 +267,39 @@ gin_bson_composite_path_extract_query(PG_FUNCTION_ARGS)
}
}
PgbsonWriterEndArray(&lower_bound_writer, &lower_bound_array_writer);
IndexTermCreateMetadata metadata = GetIndexTermMetadata(options);
*extra_data = palloc(sizeof(Pointer));
**extra_data = (Pointer) runData;
*nentries = 1;
*partialmatch = (bool *) palloc0(sizeof(bool) * 2);
if (hasInequalityMatch)
{
/* indicate that we have partial matches */
*partialmatch = (bool *) palloc(sizeof(bool));
**partialmatch = true;
(*partialmatch)[0] = true;
}
*extra_data = palloc0(sizeof(Pointer) * 2);
(*extra_data)[0] = (Pointer) runData;
pgbsonelement lower_bound_element = { 0 };
lower_bound_element.path = "$";
lower_bound_element.pathLength = 1;
lower_bound_element.bsonValue = PgbsonArrayWriterGetValue(&lower_bound_array_writer);
Datum *entries = (Datum *) palloc(sizeof(Datum));
Datum *entries = (Datum *) palloc(sizeof(Datum) * 2);
BsonIndexTermSerialized ser = SerializeBsonIndexTerm(&lower_bound_element, &metadata);
entries[0] = PointerGetDatum(ser.indexTermVal);
if (runData->hasTruncation)
{
*nentries = 2;
entries[1] = GenerateRootTruncatedTerm(&metadata);
(*partialmatch)[1] = false;
(*extra_data)[1] = NULL; /* no extra data for the truncated term */
}
PG_RETURN_POINTER(entries);
}
@@ -267,7 +318,7 @@ Datum
gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
{
/* 0 will be the value we passed in for the extract query */
bytea *queryValue = PG_GETARG_BYTEA_PP(0);
/* bytea *queryValue = PG_GETARG_BYTEA_PP(0); */
/* 1 is the value in the index we want to compare against. */
bytea *compareValue = PG_GETARG_BYTEA_PP(1);
@@ -282,8 +333,6 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
}
CompositeQueryRunData *runData = (CompositeQueryRunData *) extraData;
BsonIndexTerm queryTerm;
InitializeBsonIndexTerm(queryValue, &queryTerm);
BsonIndexTerm compareTerm;
InitializeBsonIndexTerm(compareValue, &compareTerm);
@@ -310,7 +359,8 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
{
/* We have an equality on a term - if not equal - we can bail */
if (!BsonValueEquals(compareValue,
&runData->indexBounds[compareIndex].lowerBound.bound))
&runData->indexBounds[compareIndex].lowerBound.
processedBoundValue))
{
/* Stop the search */
return hasEqualityPrefix ? 1 : -1;
@@ -326,7 +376,7 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
bool isComparisonValid = false;
int32_t compareBounds = CompareBsonValueAndType(
compareValue,
&runData->indexBounds[compareIndex].lowerBound.bound,
&runData->indexBounds[compareIndex].lowerBound.processedBoundValue,
&isComparisonValid);
if (!isComparisonValid)
{
@@ -337,7 +387,9 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
{
if (!runData->indexBounds[compareIndex].lowerBound.isBoundInclusive)
{
return -1;
/* for truncated you can't be sure - let the runtime re-evaluate */
return runData->indexBounds[compareIndex].lowerBound.
isProcessedValueTruncated ? 0 : -1;
}
else
{
@@ -358,7 +410,7 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
bool isComparisonValid = false;
int32_t compareBounds = CompareBsonValueAndType(
compareValue,
&runData->indexBounds[compareIndex].upperBound.bound,
&runData->indexBounds[compareIndex].upperBound.processedBoundValue,
&isComparisonValid);
if (!isComparisonValid)
{
@@ -369,7 +421,9 @@ gin_bson_composite_path_compare_partial(PG_FUNCTION_ARGS)
{
if (!runData->indexBounds[compareIndex].upperBound.isBoundInclusive)
{
return -1;
/* for truncated you can't be sure - let the runtime re-evaluate */
return runData->indexBounds[compareIndex].upperBound.
isProcessedValueTruncated ? 0 : -1;
}
else
{
@@ -405,7 +459,7 @@ gin_bson_composite_path_consistent(PG_FUNCTION_ARGS)
StrategyNumber strategy = PG_GETARG_UINT16(1);
int32_t numKeys = (int32_t) PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
bool *recheck = (bool *) PG_GETARG_POINTER(5); /* out param. */
/* Datum *queryKeys = (Datum *) PG_GETARG_POINTER(6); */
@@ -416,14 +470,21 @@ gin_bson_composite_path_consistent(PG_FUNCTION_ARGS)
strategy)));
}
if (numKeys == 1)
CompositeQueryRunData *runData = (CompositeQueryRunData *) extra_data[0];
if (numKeys > 2)
{
/* TODO: Handle truncation */
*recheck = false;
return check[0];
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE),
errmsg("Too many keys in consistent check: %d", numKeys)));
}
ereport(ERROR, (errmsg("unknown consistent for composite")));
*recheck = runData->requiresRuntimeRecheck;
if (runData->hasTruncation)
{
*recheck = check[1];
}
return check[0];
}
@@ -562,12 +623,11 @@ gin_bson_composite_path_options(PG_FUNCTION_ARGS)
IndexTraverseOption
GetCompositePathIndexTraverseOption(BsonIndexStrategy strategy, void *contextOptions,
const
char *currentPath, uint32_t
currentPathLength,
const char *currentPath,
uint32_t currentPathLength,
bson_type_t bsonType)
{
if (!EnableNewCompositeIndexOpclass || !IsSupportedStrategy(strategy))
if (!EnableNewCompositeIndexOpclass)
{
return IndexTraverse_Invalid;
}
@@ -772,7 +832,7 @@ GenerateCompositeTermsCore(pgbson *bson, BsonGinCompositePathOptions *options,
/* The truncation limit will be divided by the numPaths */
singlePathOptions->base.indexTermTruncateLimit =
options->base.indexTermTruncateLimit / pathCount;
GetSinglePathTruncationLimit(options->base.indexTermTruncateLimit, pathCount);
singlePathOptions->isWildcard = false;
singlePathOptions->generateNotFoundTerm = true;
singlePathOptions->path = sizeof(BsonGinSinglePathOptions);
@@ -798,9 +858,10 @@ GenerateCompositeTermsCore(pgbson *bson, BsonGinCompositePathOptions *options,
/* Now that we have the per term counts, generate the overall terms */
/* Add an additional one in case we need a truncated term */
Datum *indexEntries = palloc0(sizeof(Datum) * (totalTermCount + 1));
Datum *indexEntries = palloc0(sizeof(Datum) * (totalTermCount + 2));
bool hasTruncation = false;
bool hasUndefined = false;
IndexTermCreateMetadata overallMetadata = GetIndexTermMetadata(options);
for (uint32_t i = 0; i < totalTermCount; i++)
{
@@ -823,6 +884,14 @@ GenerateCompositeTermsCore(pgbson *bson, BsonGinCompositePathOptions *options,
{
hasTruncation = true;
}
if (indexTerm.element.pathLength == 0 &&
indexTerm.element.bsonValue.value_type == BSON_TYPE_NULL)
{
/* This is the "path does not exist" term */
indexTerm.element.bsonValue.value_type = BSON_TYPE_UNDEFINED;
hasUndefined = true;
}
PgbsonArrayWriterWriteValue(&termWriter, &indexTerm.element.bsonValue);
}
PgbsonWriterEndArray(&singleWriter, &termWriter);
@@ -847,6 +916,12 @@ GenerateCompositeTermsCore(pgbson *bson, BsonGinCompositePathOptions *options,
totalTermCount++;
}
if (hasUndefined)
{
indexEntries[totalTermCount] = GenerateRootNonExistsTerm(&overallMetadata);
totalTermCount++;
}
*nentries = totalTermCount;
return indexEntries;
}
@@ -997,6 +1072,24 @@ SetUpperBound(CompositeSingleBound *currentBoundValue, const
}
static void
SetBoundsExistsTrue(CompositeIndexBounds *queryBounds, int32_t attribute)
{
/* This is similar to $exists: true */
CompositeSingleBound bounds = { 0 };
bounds.bound.value_type = BSON_TYPE_MINKEY;
bounds.isBoundInclusive = true;
SetLowerBound(&queryBounds[attribute].lowerBound, &bounds);
bounds.bound.value_type = BSON_TYPE_MAXKEY;
bounds.isBoundInclusive = true;
SetUpperBound(&queryBounds[attribute].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[attribute].requiresRuntimeRecheck = true;
}
static void
ParseBoundsFromValue(const char **indexPaths, int32_t numPaths,
pgbsonelement *queryElement,
@@ -1027,7 +1120,6 @@ ParseBoundsFromValue(const char **indexPaths, int32_t numPaths,
{
case BSON_INDEX_STRATEGY_DOLLAR_EQUAL:
{
/* It's never set */
CompositeSingleBound equalsBounds = { 0 };
equalsBounds.bound = queryElement->bsonValue;
equalsBounds.isBoundInclusive = true;
@@ -1036,28 +1128,279 @@ ParseBoundsFromValue(const char **indexPaths, int32_t numPaths,
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_GREATER:
case BSON_INDEX_STRATEGY_DOLLAR_GREATER_EQUAL:
{
CompositeSingleBound lowerBounds = { 0 };
lowerBounds.bound = queryElement->bsonValue;
lowerBounds.isBoundInclusive = queryStrategy ==
BSON_INDEX_STRATEGY_DOLLAR_GREATER_EQUAL;
SetLowerBound(&queryBounds[i].lowerBound, &lowerBounds);
if (queryElement->bsonValue.value_type == BSON_TYPE_MINKEY)
{
/* This is similar to $exists: true */
SetBoundsExistsTrue(queryBounds, i);
}
else
{
CompositeSingleBound bounds = { 0 };
bounds.bound = queryElement->bsonValue;
bounds.isBoundInclusive = true;
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
/* Apply type bracketing */
bounds.bound = GetUpperBound(
queryElement->bsonValue.value_type,
&bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
}
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_GREATER:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = queryElement->bsonValue;
bounds.isBoundInclusive = false;
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
/* Apply type bracketing */
bounds.bound = GetUpperBound(
queryElement->bsonValue.value_type,
&bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_LESS:
case BSON_INDEX_STRATEGY_DOLLAR_LESS_EQUAL:
{
CompositeSingleBound upperBounds = { 0 };
upperBounds.bound = queryElement->bsonValue;
upperBounds.isBoundInclusive = queryStrategy ==
BSON_INDEX_STRATEGY_DOLLAR_LESS_EQUAL;
SetUpperBound(&queryBounds[i].upperBound, &upperBounds);
CompositeSingleBound bounds = { 0 };
bounds.bound = queryElement->bsonValue;
bounds.isBoundInclusive = queryStrategy ==
BSON_INDEX_STRATEGY_DOLLAR_LESS_EQUAL;
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* Apply type bracketing */
bounds.bound = GetLowerBound(
queryElement->bsonValue.value_type,
&bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_REGEX:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(BSON_TYPE_UTF8, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(BSON_TYPE_UTF8, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_EXISTS:
{
int existsValue = BsonValueAsInt32(&queryElement->bsonValue);
if (existsValue == 1)
{
/* { exists: true } */
SetBoundsExistsTrue(queryBounds, i);
}
else
{
CompositeSingleBound equalsBounds = { 0 };
equalsBounds.bound.value_type = BSON_TYPE_NULL;
equalsBounds.isBoundInclusive = true;
SetLowerBound(&queryBounds[i].lowerBound, &equalsBounds);
SetUpperBound(&queryBounds[i].upperBound, &equalsBounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
}
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_ELEMMATCH:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_ALL:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_SIZE:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(BSON_TYPE_ARRAY, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_MOD:
{
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(BSON_TYPE_DOUBLE, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(BSON_TYPE_DOUBLE, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_TYPE:
{
if (queryElement->bsonValue.value_type == BSON_TYPE_UTF8)
{
/* Single $type */
bson_type_t typeValue =
GetBsonTypeNameFromStringForDollarType(
queryElement->bsonValue.value.v_utf8.str);
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(typeValue, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(typeValue, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
}
else if (BsonValueIsNumberOrBool(&queryElement->bsonValue))
{
int64_t typeCode = BsonValueAsInt64(&queryElement->bsonValue);
/* TryGetTypeFromInt64 should be successful as this was already validated in the planner when walking the query. */
bson_type_t resolvedType;
if (!TryGetTypeFromInt64(typeCode, &resolvedType))
{
ereport(ERROR,
(errcode(ERRCODE_DOCUMENTDB_BADVALUE),
errmsg("Invalid $type value: %ld", typeCode)));
}
CompositeSingleBound bounds = { 0 };
bounds.bound = GetLowerBound(resolvedType, &bounds.isBoundInclusive);
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound = GetUpperBound(resolvedType, &bounds.isBoundInclusive);
SetUpperBound(&queryBounds[i].upperBound, &bounds);
}
else if (queryElement->bsonValue.value_type == BSON_TYPE_ARRAY)
{
/* Array of types */
/* TODO: Support multiple boundaries */
CompositeSingleBound bounds = { 0 };
bounds.bound.value_type = BSON_TYPE_MINKEY;
bounds.isBoundInclusive = true;
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound.value_type = BSON_TYPE_MAXKEY;
bounds.isBoundInclusive = true;
SetUpperBound(&queryBounds[i].lowerBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
}
else
{
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
"Invalid $type value for composite index: %s",
BsonValueToJsonForLogging(
&queryElement->bsonValue))));
}
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_RANGE:
{
DollarRangeParams *params = ParseQueryDollarRange(queryElement);
CompositeSingleBound bounds = { 0 };
if (params->minValue.value_type == BSON_TYPE_MINKEY &&
params->isMinInclusive)
{
SetBoundsExistsTrue(queryBounds, i);
}
else if (params->minValue.value_type != BSON_TYPE_EOD)
{
bounds.bound = params->minValue;
bounds.isBoundInclusive = params->isMinInclusive;
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
}
if (params->maxValue.value_type != BSON_TYPE_EOD)
{
bounds.bound = params->maxValue;
bounds.isBoundInclusive = params->isMaxInclusive;
SetUpperBound(&queryBounds[i].upperBound, &bounds);
}
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_IN:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_EQUAL:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_IN:
case BSON_INDEX_STRATEGY_DOLLAR_BITS_ALL_CLEAR:
case BSON_INDEX_STRATEGY_DOLLAR_BITS_ANY_CLEAR:
case BSON_INDEX_STRATEGY_DOLLAR_BITS_ALL_SET:
case BSON_INDEX_STRATEGY_DOLLAR_BITS_ANY_SET:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_GT:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_GTE:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_LT:
case BSON_INDEX_STRATEGY_DOLLAR_NOT_LTE:
{
/* TODO: Support multiple boundaries */
CompositeSingleBound bounds = { 0 };
bounds.bound.value_type = BSON_TYPE_MINKEY;
bounds.isBoundInclusive = true;
SetLowerBound(&queryBounds[i].lowerBound, &bounds);
bounds.bound.value_type = BSON_TYPE_MAXKEY;
bounds.isBoundInclusive = true;
SetUpperBound(&queryBounds[i].lowerBound, &bounds);
/* TODO: Replace this with index eval function */
queryBounds[i].requiresRuntimeRecheck = true;
break;
}
case BSON_INDEX_STRATEGY_DOLLAR_TEXT:
case BSON_INDEX_STRATEGY_DOLLAR_GEOINTERSECTS:
case BSON_INDEX_STRATEGY_DOLLAR_GEOWITHIN:
case BSON_INDEX_STRATEGY_GEONEAR:
case BSON_INDEX_STRATEGY_GEONEAR_RANGE:
case BSON_INDEX_STRATEGY_COMPOSITE_QUERY:
case BSON_INDEX_STRATEGY_UNIQUE_EQUAL:
case BSON_INDEX_STRATEGY_DOLLAR_ORDERBY:
default:
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg(
@@ -1067,3 +1410,33 @@ ParseBoundsFromValue(const char **indexPaths, int32_t numPaths,
}
}
}
static void
ProcessBoundForQuery(CompositeSingleBound *bound, const IndexTermCreateMetadata *metadata)
{
pgbson_writer writer;
PgbsonWriterInit(&writer);
pgbsonelement termElement = { 0 };
termElement.path = "$";
termElement.pathLength = 1;
termElement.bsonValue = bound->bound;
BsonIndexTermSerialized serialized = SerializeBsonIndexTerm(&termElement, metadata);
if (serialized.isIndexTermTruncated)
{
/* preserve and store the value */
BsonIndexTerm term;
InitializeBsonIndexTerm(serialized.indexTermVal, &term);
bound->processedBoundValue = term.element.bsonValue;
bound->isProcessedValueTruncated = term.isIndexTermTruncated;
}
else
{
/* Just keep the original */
pfree(serialized.indexTermVal);
bound->processedBoundValue = bound->bound;
bound->isProcessedValueTruncated = false;
}
}
+3 -12
View File
@@ -30,6 +30,7 @@
#include "opclass/bson_gin_private.h"
#include "opclass/bson_gin_index_mgmt.h"
#include "opclass/bson_gin_index_term.h"
#include "opclass/bson_gin_index_types_core.h"
#include "io/bson_core.h"
#include "aggregation/bson_query_common.h"
#include "query/bson_compare.h"
@@ -1682,7 +1683,7 @@ GinBsonExtractQueryDollarRange(BsonExtractQueryArgs *args)
}
DollarRangeValues *rangeValues = palloc0(sizeof(DollarRangeValues));
DollarRangeParams *params = ParseQueryDollarRange(filter);
DollarRangeParams *params = ParseQueryDollarRange(&filterElement);
rangeValues->params = *params;
pgbsonelement maxElement =
@@ -2835,17 +2836,7 @@ GinBsonExtractQueryDollarType(BsonExtractQueryArgs *args)
if (documentElement.bsonValue.value_type == BSON_TYPE_UTF8)
{
const char *typeNameStr = documentElement.bsonValue.value.v_utf8.str;
bson_type_t type;
if (strcmp(typeNameStr, "number") == 0)
{
/* Since $type on index only validates sort order, int32 is sufficient */
type = BSON_TYPE_INT32;
}
else
{
type = BsonTypeFromName(typeNameStr);
}
bson_type_t type = GetBsonTypeNameFromStringForDollarType(typeNameStr);
extraDataPtr[index++] = Int32GetDatum((int32_t) type);
}
/**
@@ -0,0 +1,368 @@
/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* src/opclass/bson_gin_index_types_core.c
*
*
* Core logic for type management of BSON for indexes
* See also: https://www.postgresql.org/docs/current/gin-extensibility.html
*
*-------------------------------------------------------------------------
*/
#include <postgres.h>
#include <fmgr.h>
#include <miscadmin.h>
#include <math.h>
#include "io/bson_core.h"
#include "query/bson_compare.h"
#include <opclass/bson_gin_index_types_core.h>
bson_value_t
GetUpperBound(bson_type_t type, bool *isUpperBoundInclusive)
{
bson_value_t upperBound = { 0 };
*isUpperBoundInclusive = true;
switch (type)
{
case BSON_TYPE_MINKEY:
{
upperBound.value_type = BSON_TYPE_MINKEY;
break;
}
case BSON_TYPE_UNDEFINED:
case BSON_TYPE_NULL:
{
/* Since these are equivalent it doesn't matter
* which one you pick, they'll be treated as the same
*/
upperBound.value_type = BSON_TYPE_NULL;
break;
}
case BSON_TYPE_DOUBLE:
case BSON_TYPE_INT32:
case BSON_TYPE_INT64:
case BSON_TYPE_DECIMAL128:
{
/* For numbers positive infinity inclusive is the largest number */
upperBound.value_type = BSON_TYPE_DOUBLE;
upperBound.value.v_double = INFINITY;
break;
}
case BSON_TYPE_UTF8:
case BSON_TYPE_SYMBOL:
{
/* If we want to be able to accept UTF8 strings with invalid sequences
* then the max UTf8 string can only be the smallest document exclusive
*/
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_DOCUMENT, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_DOCUMENT:
{
/* If we want to be able to accept any document
* then the max document string can only be the smallest array exclusive
*/
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_ARRAY, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_ARRAY:
{
/* Pick the smallest binary value exclusive */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_BINARY, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_BINARY:
{
/* Exclusive to the lower bound of the next type */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_OID, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_OID:
{
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_BOOL, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_BOOL:
{
/* Highest bool value is true - inclusive */
upperBound.value_type = BSON_TYPE_BOOL;
upperBound.value.v_bool = true;
break;
}
case BSON_TYPE_DATE_TIME:
{
upperBound.value_type = BSON_TYPE_DATE_TIME;
upperBound.value.v_datetime = INT64_MAX;
*isUpperBoundInclusive = true; /* INT64_MAX is inclusive */
break;
}
case BSON_TYPE_TIMESTAMP:
{
upperBound.value_type = BSON_TYPE_TIMESTAMP;
upperBound.value.v_timestamp.increment = UINT32_MAX;
upperBound.value.v_timestamp.timestamp = UINT32_MAX;
*isUpperBoundInclusive = true;
break;
}
case BSON_TYPE_REGEX:
{
/* Exclusive to the lower bound of the next type */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_DBPOINTER, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_DBPOINTER:
{
/* Exclusive to the lower bound of the next type */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_CODE, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_CODE:
{
/* Exclusive to the lower bound of the next type */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_CODEWSCOPE, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_CODEWSCOPE:
{
/* Exclusive to the lower bound of the next type */
bool isBoundInclusiveIgnore = false;
upperBound = GetLowerBound(BSON_TYPE_MAXKEY, &isBoundInclusiveIgnore);
*isUpperBoundInclusive = false;
break;
}
case BSON_TYPE_MAXKEY:
{
upperBound.value_type = BSON_TYPE_MAXKEY;
break;
}
default:
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg(
"Unsupported BSON type for upper bound determination: %d",
type),
errdetail_log(
"The type %s is not supported in composite index upper bounds.",
BsonTypeName(type))));
}
}
return upperBound;
}
bson_value_t
GetLowerBound(bson_type_t type, bool *isLowerBoundInclusive)
{
bson_value_t lowerBound = { 0 };
*isLowerBoundInclusive = false;
switch (type)
{
case BSON_TYPE_MINKEY:
{
lowerBound.value_type = BSON_TYPE_MINKEY;
break;
}
case BSON_TYPE_UNDEFINED:
case BSON_TYPE_NULL:
{
lowerBound.value_type = BSON_TYPE_NULL;
break;
}
case BSON_TYPE_DOUBLE:
case BSON_TYPE_INT32:
case BSON_TYPE_INT64:
case BSON_TYPE_DECIMAL128:
{
lowerBound.value_type = BSON_TYPE_DOUBLE;
lowerBound.value.v_double = -INFINITY; /* Negative infinity for lower bound */
*isLowerBoundInclusive = true; /* Negative infinity is inclusive */
break;
}
case BSON_TYPE_UTF8:
case BSON_TYPE_SYMBOL:
{
lowerBound.value_type = BSON_TYPE_UTF8;
lowerBound.value.v_utf8.str = ""; /* Empty string for lower bound */
lowerBound.value.v_utf8.len = 0;
*isLowerBoundInclusive = true; /* Empty string is inclusive */
break;
}
case BSON_TYPE_DOCUMENT:
{
pgbson *emptyDoc = PgbsonInitEmpty();
lowerBound = ConvertPgbsonToBsonValue(emptyDoc);
*isLowerBoundInclusive = true;
break;
}
case BSON_TYPE_ARRAY:
{
pgbson *emptyDoc = PgbsonInitEmpty();
lowerBound = ConvertPgbsonToBsonValue(emptyDoc);
lowerBound.value_type = BSON_TYPE_ARRAY;
*isLowerBoundInclusive = true;
break;
}
case BSON_TYPE_BINARY:
{
lowerBound.value_type = BSON_TYPE_BINARY;
lowerBound.value.v_binary.data_len = 0;
lowerBound.value.v_binary.data = NULL;
lowerBound.value.v_binary.subtype = BSON_SUBTYPE_BINARY;
*isLowerBoundInclusive = true; /* Empty binary is inclusive */
break;
}
case BSON_TYPE_OID:
{
lowerBound.value_type = BSON_TYPE_OID;
memset(lowerBound.value.v_oid.bytes, 0, sizeof(lowerBound.value.v_oid.bytes));
*isLowerBoundInclusive = true;
break;
}
case BSON_TYPE_BOOL:
{
lowerBound.value_type = BSON_TYPE_BOOL;
lowerBound.value.v_bool = false; /* Lowest bool value is false */
*isLowerBoundInclusive = true; /* false is inclusive */
break;
}
case BSON_TYPE_DATE_TIME:
{
lowerBound.value_type = BSON_TYPE_DATE_TIME;
lowerBound.value.v_datetime = INT64_MIN; /* Minimum date time value */
*isLowerBoundInclusive = true; /* INT64_MIN is inclusive */
break;
}
case BSON_TYPE_TIMESTAMP:
{
lowerBound.value_type = BSON_TYPE_TIMESTAMP;
lowerBound.value.v_timestamp.increment = 0;
lowerBound.value.v_timestamp.timestamp = 0; /* Minimum timestamp value */
*isLowerBoundInclusive = true; /* 0 is inclusive */
break;
}
case BSON_TYPE_REGEX:
{
lowerBound.value_type = BSON_TYPE_REGEX;
lowerBound.value.v_regex.regex = ""; /* Empty regex for lower bound */
lowerBound.value.v_regex.options = NULL; /* Empty options for lower bound */
*isLowerBoundInclusive = true; /* Empty regex is inclusive */
break;
}
case BSON_TYPE_DBPOINTER:
{
lowerBound.value_type = BSON_TYPE_DBPOINTER;
lowerBound.value.v_dbpointer.collection_len = 0;
lowerBound.value.v_dbpointer.collection = "";
memset(lowerBound.value.v_dbpointer.oid.bytes, 0,
sizeof(lowerBound.value.v_dbpointer.oid.bytes));
*isLowerBoundInclusive = true;
break;
}
case BSON_TYPE_CODE:
{
lowerBound.value_type = BSON_TYPE_CODE;
lowerBound.value.v_code.code = ""; /* Empty string for lower bound */
lowerBound.value.v_code.code_len = 0;
*isLowerBoundInclusive = true; /* Empty string is inclusive */
break;
}
case BSON_TYPE_CODEWSCOPE:
{
lowerBound.value_type = BSON_TYPE_CODEWSCOPE;
lowerBound.value.v_codewscope.code = ""; /* Empty string for lower bound */
lowerBound.value.v_codewscope.code_len = 0;
pgbson *emptyDoc = PgbsonInitEmpty();
bson_value_t emptyDocValue = ConvertPgbsonToBsonValue(emptyDoc);
lowerBound.value.v_codewscope.scope_data = emptyDocValue.value.v_doc.data;
lowerBound.value.v_codewscope.scope_len = emptyDocValue.value.v_doc.data_len;
*isLowerBoundInclusive = true; /* Empty string is inclusive */
break;
}
case BSON_TYPE_MAXKEY:
{
lowerBound.value_type = BSON_TYPE_MAXKEY;
break;
}
default:
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg(
"Unsupported BSON type for upper bound determination: %d",
type),
errdetail_log(
"The type %s is not supported in composite index upper bounds.",
BsonTypeName(type))));
}
}
return lowerBound;
}
bson_type_t
GetBsonTypeNameFromStringForDollarType(const char *typeNameStr)
{
if (strcmp(typeNameStr, "number") == 0)
{
/* Since $type on index only validates sort order, double is sufficient */
return BSON_TYPE_DOUBLE;
}
else
{
return BsonTypeFromName(typeNameStr);
}
}
@@ -3108,7 +3108,9 @@ static void
PopulateRangeStateFromQuery(DollarRangeParams *params, const
pgbson *filter)
{
*params = *ParseQueryDollarRange((pgbson *) filter);
pgbsonelement filterElement = { 0 };
PgbsonToSinglePgbsonElement(filter, &filterElement);
*params = *ParseQueryDollarRange(&filterElement);
Assert(params != NULL);
}
@@ -47,27 +47,29 @@ SELECT * FROM documentdb_test_helpers.gin_bson_get_composite_path_generated_term
-- test when one doesn't exist
SELECT * FROM documentdb_test_helpers.gin_bson_get_composite_path_generated_terms('{ "b": [ true, false ] }', '[ "a", "b" ]', 2000, false);
gin_bson_get_composite_path_generated_terms
---------------------------------------------
{ "$" : [ null, [ true, false ] ] }
{ "$" : [ null, true ] }
{ "$" : [ null, false ] }
(3 rows)
gin_bson_get_composite_path_generated_terms
--------------------------------------------------------
{ "$" : [ { "$undefined" : true }, [ true, false ] ] }
{ "$" : [ { "$undefined" : true }, true ] }
{ "$" : [ { "$undefined" : true }, false ] }
{ "" : { "$undefined" : true } }
(4 rows)
SELECT * FROM documentdb_test_helpers.gin_bson_get_composite_path_generated_terms('{ "a": [ 1, 2, 3 ] }', '[ "a", "b" ]', 2000, false);
gin_bson_get_composite_path_generated_terms
------------------------------------------------------------------------------------------------
{ "$" : [ [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "3" } ], null ] }
{ "$" : [ { "$numberInt" : "1" }, null ] }
{ "$" : [ { "$numberInt" : "2" }, null ] }
{ "$" : [ { "$numberInt" : "3" }, null ] }
(4 rows)
gin_bson_get_composite_path_generated_terms
-------------------------------------------------------------------------------------------------------------------
{ "$" : [ [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "3" } ], { "$undefined" : true } ] }
{ "$" : [ { "$numberInt" : "1" }, { "$undefined" : true } ] }
{ "$" : [ { "$numberInt" : "2" }, { "$undefined" : true } ] }
{ "$" : [ { "$numberInt" : "3" }, { "$undefined" : true } ] }
{ "" : { "$undefined" : true } }
(5 rows)
-- test when one gets truncated (a has 29 letters, truncation limit is 50 /2 so 25 per path)
SELECT * FROM documentdb_test_helpers.gin_bson_get_composite_path_generated_terms('{ "a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "b": 1 }', '[ "a", "b" ]', 50, true);
gin_bson_get_composite_path_generated_terms
---------------------------------------------
{ "$" : [ "aaaaaaaaaaaa" ], "t" : true }
gin_bson_get_composite_path_generated_terms
--------------------------------------------------------------
{ "$" : [ "aaaaaaaa", { "$numberInt" : "1" } ], "t" : true }
{ "" : { "$maxKey" : 1 }, "t" : true }
(2 rows)
@@ -193,3 +195,133 @@ SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{
----------
(0 rows)
-- now add some cross-type members
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 5, "a": "string1", "b": true }');
insert_one
----------------------------------------------------------------------
{ "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } }
(1 row)
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 6, "a": "string2", "b": true }');
insert_one
----------------------------------------------------------------------
{ "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } }
(1 row)
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 7, "a": { "key": "string2" }, "b": true }');
insert_one
----------------------------------------------------------------------
{ "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } }
(1 row)
-- has cross type values
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$exists": true }, "b": true } }');
document
---------------------------------------------------------------------------------------------------------------------
{ "_id" : { "$numberInt" : "1" }, "a" : { "$numberInt" : "1" }, "b" : true }
{ "_id" : { "$numberInt" : "2" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : true }
{ "_id" : { "$numberInt" : "3" }, "a" : { "$numberInt" : "1" }, "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "4" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "5" }, "a" : "string1", "b" : true }
{ "_id" : { "$numberInt" : "6" }, "a" : "string2", "b" : true }
{ "_id" : { "$numberInt" : "7" }, "a" : { "key" : "string2" }, "b" : true }
(7 rows)
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gte": { "$minKey": 1 } }, "b": true } }');
document
---------------------------------------------------------------------------------------------------------------------
{ "_id" : { "$numberInt" : "1" }, "a" : { "$numberInt" : "1" }, "b" : true }
{ "_id" : { "$numberInt" : "2" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : true }
{ "_id" : { "$numberInt" : "3" }, "a" : { "$numberInt" : "1" }, "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "4" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "5" }, "a" : "string1", "b" : true }
{ "_id" : { "$numberInt" : "6" }, "a" : "string2", "b" : true }
{ "_id" : { "$numberInt" : "7" }, "a" : { "key" : "string2" }, "b" : true }
(7 rows)
-- applies type bracketing
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gt": 0 }, "b": true } }');
document
---------------------------------------------------------------------------------------------------------------------
{ "_id" : { "$numberInt" : "1" }, "a" : { "$numberInt" : "1" }, "b" : true }
{ "_id" : { "$numberInt" : "2" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : true }
{ "_id" : { "$numberInt" : "3" }, "a" : { "$numberInt" : "1" }, "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "4" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : [ true, false ] }
(4 rows)
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gte": "string0" }, "b": true } }');
document
-----------------------------------------------------------------
{ "_id" : { "$numberInt" : "5" }, "a" : "string1", "b" : true }
{ "_id" : { "$numberInt" : "6" }, "a" : "string2", "b" : true }
(2 rows)
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "string" }, "b": true } }');
document
-----------------------------------------------------------------
{ "_id" : { "$numberInt" : "5" }, "a" : "string1", "b" : true }
{ "_id" : { "$numberInt" : "6" }, "a" : "string2", "b" : true }
(2 rows)
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "object" }, "b": true } }');
document
-----------------------------------------------------------------------------
{ "_id" : { "$numberInt" : "7" }, "a" : { "key" : "string2" }, "b" : true }
(1 row)
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "number" }, "b": true } }');
document
---------------------------------------------------------------------------------------------------------------------
{ "_id" : { "$numberInt" : "1" }, "a" : { "$numberInt" : "1" }, "b" : true }
{ "_id" : { "$numberInt" : "2" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : true }
{ "_id" : { "$numberInt" : "3" }, "a" : { "$numberInt" : "1" }, "b" : [ true, false ] }
{ "_id" : { "$numberInt" : "4" }, "a" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ], "b" : [ true, false ] }
(4 rows)
-- runtime recheck
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$regex": ".+2$" }, "b": true } }');
document
-----------------------------------------------------------------
{ "_id" : { "$numberInt" : "6" }, "a" : "string2", "b" : true }
(1 row)
-- add large keys
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', FORMAT('{ "_id": 8, "a": { "key": "%s" }, "b": "%s" }', repeat('a', 10000), repeat('a', 10000))::bson);
insert_one
----------------------------------------------------------------------
{ "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } }
(1 row)
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 5000), repeat('a', 5000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
document
----------
(0 rows)
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 8000), repeat('a', 8000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
document
----------
(0 rows)
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 10000), repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
document
------------------------------------
{ "_id" : { "$numberInt" : "8" } }
(1 row)
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" } }, "projection": { "_id": 1 } }', repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
document
------------------------------------
{ "_id" : { "$numberInt" : "8" } }
(1 row)
SELECT FORMAT('{ "find": "comp_collection", "filter": { "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
document
------------------------------------
{ "_id" : { "$numberInt" : "8" } }
(1 row)
@@ -51,3 +51,42 @@ SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": 1, "b": { "$gt": false } } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": 2, "b": { "$gt": false } } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": 1, "b": { "$gt": true } } }');
-- now add some cross-type members
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 5, "a": "string1", "b": true }');
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 6, "a": "string2", "b": true }');
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', '{ "_id": 7, "a": { "key": "string2" }, "b": true }');
-- has cross type values
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$exists": true }, "b": true } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gte": { "$minKey": 1 } }, "b": true } }');
-- applies type bracketing
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gt": 0 }, "b": true } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$gte": "string0" }, "b": true } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "string" }, "b": true } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "object" }, "b": true } }');
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$type": "number" }, "b": true } }');
-- runtime recheck
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', '{ "find": "comp_collection", "filter": { "a": { "$regex": ".+2$" }, "b": true } }');
-- add large keys
SELECT documentdb_api.insert_one('comp_db', 'comp_collection', FORMAT('{ "_id": 8, "a": { "key": "%s" }, "b": "%s" }', repeat('a', 10000), repeat('a', 10000))::bson);
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 5000), repeat('a', 5000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 8000), repeat('a', 8000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" }, "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 10000), repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
SELECT FORMAT('{ "find": "comp_collection", "filter": { "a": { "key": "%s" } }, "projection": { "_id": 1 } }', repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);
SELECT FORMAT('{ "find": "comp_collection", "filter": { "b": "%s" }, "projection": { "_id": 1 } }', repeat('a', 10000)) AS q1 \gset
SELECT document FROM documentdb_api_catalog.bson_aggregation_find('comp_db', :'q1'::bson);