diff --git a/CHANGELOG.md b/CHANGELOG.md index 54f1acce..ab1bf4f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### documentdb v0.102-0 (Unreleased) ### * Support index pushdown for vector search queries *[Bugfix]* +* Inline $match with let in $lookup pipelines as JOIN Filter *[Perf]* ### documentdb v0.101-0 (February 12, 2025) ### * Push $graphlookup recursive CTE JOIN filters to index *[Perf]* diff --git a/internal/pg_documentdb_distributed/src/test/regress/bin/expect_normalize.sed b/internal/pg_documentdb_distributed/src/test/regress/bin/expect_normalize.sed index af11ead1..a65c48e5 100755 --- a/internal/pg_documentdb_distributed/src/test/regress/bin/expect_normalize.sed +++ b/internal/pg_documentdb_distributed/src/test/regress/bin/expect_normalize.sed @@ -10,4 +10,5 @@ # Differing names can have differing table column widths s/^-[+-]{2,}$/---------------------------------------------------------------------/g # Replace the values of the time system variables ($$NOW) with a constant -s/\"sn\" : \{ \"\$date\" : \{ \"\$numberLong\" : \"[0-9]*\" \} \}/\"sn\" : NOW_SYS_VARIABLE/g \ No newline at end of file +s/\"sn\" : \{ \"\$date\" : \{ \"\$numberLong\" : \"[0-9]*\" \} \}/\"sn\" : NOW_SYS_VARIABLE/g +s/\"now\" : \{ \"\$date\" : \{ \"\$numberLong\" : \"[0-9]*\" \} \}/\"now\" : NOW_SYS_VARIABLE/g \ No newline at end of file diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_let.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_let.out index ae4f6abb..9f33d330 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_let.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_let.out @@ -531,7 +531,7 @@ SELECT document from bson_aggregation_pipeline('db', '{ "aggregate": "orderscoll (3 rows) EXPLAIN (COSTS OFF) SELECT document from bson_aggregation_pipeline('db', '{ "aggregate": "orderscoll", "pipeline": [ { "$lookup": { "from": "products", "let": { "productId": "$productId", "hello" : "parag" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$_id", "$$productId" ] } } }, { "$project": { "name": 1, "price": 1, "_id": 0 , "field" : "$$hello" } } ], "as": "productDetails" } }, { "$unwind": "$productDetails" } ] , "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Task Count: 1 @@ -544,17 +544,16 @@ EXPLAIN (COSTS OFF) SELECT document from bson_aggregation_pipeline('db', '{ "agg Recheck Cond: (shard_key_value = '4137'::bigint) -> Bitmap Index Scan on _id_ Index Cond: (shard_key_value = '4137'::bigint) - -> Materialize - -> Aggregate - -> Bitmap Heap Scan on documents_4138_413128 collection_0_1 - Recheck Cond: (shard_key_value = '4138'::bigint) - -> Bitmap Index Scan on _id_ - Index Cond: (shard_key_value = '4138'::bigint) + -> Aggregate + -> Bitmap Heap Scan on documents_4138_413128 collection_0_1 + Recheck Cond: (shard_key_value = '4138'::bigint) + Filter: documentdb_api_internal.bson_dollar_expr(document, '{ "" : { "$eq" : [ "$_id", "$$productId" ] } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "productId" : "$productId", "hello" : "parag" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)) + -> Bitmap Index Scan on _id_ + Index Cond: (shard_key_value = '4138'::bigint) SubPlan 1 -> Aggregate -> Function Scan on bson_lookup_unwind lookup_subpipeline_substage_0 - Filter: documentdb_api_internal.bson_dollar_expr(lookup_unwind, '{ "" : { "$eq" : [ "$_id", "$$productId" ] } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "productId" : "$productId", "hello" : "parag" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)) -(21 rows) +(20 rows) -- only part of the pipeline uses let SELECT document from bson_aggregation_pipeline('db', '{ "aggregate": "orderscoll", "pipeline": [ { "$lookup": { "from": "products", "let": { "productId": "$productId", "hello" : "parag" }, "pipeline": [ { "$match": { "_id": { "$gt": "P001" } } }, { "$project": { "name": 1, "price": 1, "_id": 0 , "field" : "$$hello" } } ], "as": "productDetails" } }, { "$unwind": "$productDetails" } ] , "cursor": {} }'); @@ -921,3 +920,283 @@ ERROR: Nested lookup with let on sharded collections not supported yet SELECT document from bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_let_pipelineto", "pipeline": [ { "$lookup": { "from": "aggregation_pipeline_let_pipelinefrom", "pipeline": [{ "$addFields": { "addedVal": "$$qval" }}, { "$lookup": { "from": "aggregation_pipeline_let_pipelinefrom_second", "localField": "qq", "foreignField": "qval", "as": "myExtra", "let": { "secondVar": "$quantity" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$qq", "$$secondVar" ] } }} ] }} ], "as": "myMatch", "localField": "restaurant_name", "foreignField": "name", "let": { "qval": "$qval" } }} ], "cursor": {} }'); ERROR: Nested lookup with let on sharded collections not supported yet +-- Test for lookup with let and $match with $expr +SELECT documentdb_api.insert_one('db','lookup_left',' { "_id" : 1, "item" : 1 }', NULL); +NOTICE: creating collection + insert_one +--------------------------------------------------------------------- + { "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } } +(1 row) + +SELECT documentdb_api.insert_one('db','lookup_left',' { "_id" : 2, "item" : 2 }', NULL); + insert_one +--------------------------------------------------------------------- + { "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } } +(1 row) + +-- Fill right collection with more than 100 MB of data +DO $$ +DECLARE i int; +BEGIN +FOR i IN 1..50 LOOP +PERFORM documentdb_api.insert_one('db', 'lookup_right', FORMAT('{ "_id": %s, "name": %s, "c": { "%s": [ %s "d" ] } }', i, i, i, REPEAT('"' || i || REPEAT('a', 1000) || '", ', 5000))::documentdb_core.bson, NULL); +END LOOP; +END; +$$; +NOTICE: creating collection +-- This will throw size error because $match in not inlined with right query as it is not the first stage +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$project": { "c": 0 } }, { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +ERROR: Size 106297794 is larger than maximum size allowed for an intermediate document 104857600 +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$project": { "c": 0 } }, { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_dollar_merge_documents("left", (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS "funcName" FROM documentdb_api_catalog.bson_lookup_unwind(lookup_non_inlined_stage_1."right", 'myMatch'::text) lookup_subpipeline_substage_0(lookup_unwind) WHERE documentdb_api_internal.bson_dollar_expr(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let), '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let))) AS document FROM (SELECT lookup_stage_1.document AS "left", "lookupRight_stage_1".document AS "right", documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(lookup_stage_1.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM ((SELECT collection.document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413201 collection WHERE (collection.shard_key_value OPERATOR(pg_catalog.=) '4140'::bigint)) lookup_stage_1 LEFT JOIN LATERAL (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS document FROM documentdb_data.documents_4141_413218 collection_0_1 WHERE (collection_0_1.shard_key_value OPERATOR(pg_catalog.=) '4141'::bigint)) "lookupRight_stage_1" ON (true))) lookup_non_inlined_stage_1 + Node: host=localhost port=58070 dbname=regression + -> Nested Loop Left Join + Output: documentdb_api_internal.bson_dollar_merge_documents(collection.document, (SubPlan 1)) + -> Bitmap Heap Scan on documentdb_data.documents_4140_413201 collection + Output: collection.shard_key_value, collection.object_id, collection.document, collection.creation_time + Recheck Cond: (collection.shard_key_value = '4140'::bigint) + -> Bitmap Index Scan on _id_ + Index Cond: (collection.shard_key_value = '4140'::bigint) + -> Materialize + Output: (COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson)) + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Bitmap Heap Scan on documentdb_data.documents_4141_413218 collection_0_1 + Output: collection_0_1.shard_key_value, collection_0_1.object_id, collection_0_1.document, collection_0_1.creation_time + Recheck Cond: (collection_0_1.shard_key_value = '4141'::bigint) + -> Bitmap Index Scan on _id_ + Index Cond: (collection_0_1.shard_key_value = '4141'::bigint) + SubPlan 1 + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on documentdb_api_catalog.bson_lookup_unwind lookup_subpipeline_substage_0 + Output: lookup_subpipeline_substage_0.lookup_unwind + Function Call: documentdb_api_catalog.bson_lookup_unwind((COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson)), 'myMatch'::text) + Filter: documentdb_api_internal.bson_dollar_expr(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)) +(30 rows) + +-- This should not throw error as $match is inlined with right query +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "item" : { "$numberInt" : "1" }, "myMatch" : [ { "_id" : { "$numberInt" : "1" }, "name" : { "$numberInt" : "1" } } ] } + { "_id" : { "$numberInt" : "2" }, "item" : { "$numberInt" : "2" }, "myMatch" : [ { "_id" : { "$numberInt" : "2" }, "name" : { "$numberInt" : "2" } } ] } +(2 rows) + +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_dollar_merge_documents("left", (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS "funcName" FROM documentdb_api_catalog.bson_lookup_unwind(lookup_non_inlined_stage_1."right", 'myMatch'::text) lookup_subpipeline_substage_0(lookup_unwind))) AS document FROM (SELECT lookup_stage_1.document AS "left", "lookupRight_stage_1".document AS "right", documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(lookup_stage_1.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM ((SELECT collection.document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413201 collection WHERE (collection.shard_key_value OPERATOR(pg_catalog.=) '4140'::bigint)) lookup_stage_1 LEFT JOIN LATERAL (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(lookup_right_query_stage_0.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS document FROM (SELECT collection_0_1.document FROM documentdb_data.documents_4141_413218 collection_0_1 WHERE (collection_0_1.shard_key_value OPERATOR(pg_catalog.=) '4141'::bigint)) lookup_right_query_stage_0 WHERE documentdb_api_internal.bson_dollar_expr(lookup_right_query_stage_0.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, lookup_stage_1.let)) "lookupRight_stage_1" ON (true))) lookup_non_inlined_stage_1 + Node: host=localhost port=58070 dbname=regression + -> Nested Loop Left Join + Output: documentdb_api_internal.bson_dollar_merge_documents(collection.document, (SubPlan 1)) + -> Bitmap Heap Scan on documentdb_data.documents_4140_413201 collection + Output: collection.shard_key_value, collection.object_id, collection.document, collection.creation_time + Recheck Cond: (collection.shard_key_value = '4140'::bigint) + -> Bitmap Index Scan on _id_ + Index Cond: (collection.shard_key_value = '4140'::bigint) + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Bitmap Heap Scan on documentdb_data.documents_4141_413218 collection_0_1 + Output: collection_0_1.shard_key_value, collection_0_1.object_id, collection_0_1.document, collection_0_1.creation_time + Recheck Cond: (collection_0_1.shard_key_value = '4141'::bigint) + Filter: documentdb_api_internal.bson_dollar_expr(collection_0_1.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)) + -> Bitmap Index Scan on _id_ + Index Cond: (collection_0_1.shard_key_value = '4141'::bigint) + SubPlan 1 + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(collection.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on documentdb_api_catalog.bson_lookup_unwind lookup_subpipeline_substage_0 + Output: lookup_subpipeline_substage_0.lookup_unwind + Function Call: documentdb_api_catalog.bson_lookup_unwind((COALESCE(documentdb_api_catalog.bson_array_agg(collection_0_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson)), 'myMatch'::text) +(28 rows) + +-- Shard left collection and right collections +SET citus.explain_all_tasks to on; +SET citus.max_adaptive_executor_pool_size to 1; +SELECT documentdb_api.shard_collection('{ "shardCollection": "db.lookup_left", "key": { "_id": "hashed" }, "numInitialChunks": 2 }'); + shard_collection +--------------------------------------------------------------------- + +(1 row) + +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "2" }, "item" : { "$numberInt" : "2" }, "myMatch" : [ { "_id" : { "$numberInt" : "2" }, "name" : { "$numberInt" : "2" } } ] } + { "_id" : { "$numberInt" : "1" }, "item" : { "$numberInt" : "1" }, "myMatch" : [ { "_id" : { "$numberInt" : "1" }, "name" : { "$numberInt" : "1" } } ] } +(2 rows) + +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + -> Distributed Subplan 409_1 + -> Custom Scan (Citus Adaptive) + Output: remote_scan."left", remote_scan."right", remote_scan.let + -> Distributed Subplan 412_1 + -> Custom Scan (Citus Adaptive) + Output: remote_scan.document, remote_scan.let + Task Count: 2 + Tasks Shown: All + -> Task + Query: SELECT document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413232 collection WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4140_413232 collection + Output: document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Task + Query: SELECT document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413233 collection WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4140_413233 collection + Output: document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Distributed Subplan 412_2 + -> Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT document FROM documentdb_data.documents_4141_413218 collection_0_1 WHERE (shard_key_value OPERATOR(pg_catalog.=) '4141'::bigint) + Node: host=localhost port=58070 dbname=regression + -> Bitmap Heap Scan on documentdb_data.documents_4141_413218 collection_0_1 + Output: document + Recheck Cond: (collection_0_1.shard_key_value = '4141'::bigint) + -> Bitmap Index Scan on _id_ + Index Cond: (collection_0_1.shard_key_value = '4141'::bigint) + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT lookup_stage_1.document AS "left", "lookupRight_stage_1".document AS "right", documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(lookup_stage_1.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM ((SELECT intermediate_result.document, intermediate_result.let FROM read_intermediate_result('412_1'::text, 'binary'::citus_copy_format) intermediate_result(document documentdb_core.bson, let documentdb_core.bson)) lookup_stage_1 LEFT JOIN LATERAL (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(lookup_right_query_stage_0.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS document FROM (SELECT intermediate_result.document FROM read_intermediate_result('412_2'::text, 'binary'::citus_copy_format) intermediate_result(document documentdb_core.bson)) lookup_right_query_stage_0 WHERE documentdb_api_internal.bson_dollar_expr(lookup_right_query_stage_0.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, lookup_stage_1.let)) "lookupRight_stage_1" ON (true)) + Node: host=localhost port=58070 dbname=regression + -> Nested Loop Left Join + Output: intermediate_result.document, (COALESCE(documentdb_api_catalog.bson_array_agg(intermediate_result_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson)), documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(intermediate_result.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result + Output: intermediate_result.document, intermediate_result.let + Function Call: read_intermediate_result('412_1'::text, 'binary'::citus_copy_format) + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(intermediate_result_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result_1 + Output: intermediate_result_1.document + Function Call: read_intermediate_result('412_2'::text, 'binary'::citus_copy_format) + Filter: documentdb_api_internal.bson_dollar_expr(intermediate_result_1.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, intermediate_result.let) + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_dollar_merge_documents("left", (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS "funcName" FROM documentdb_api_catalog.bson_lookup_unwind(lookup_non_inlined_stage_1."right", 'myMatch'::text) lookup_subpipeline_substage_0(lookup_unwind))) AS document FROM (SELECT intermediate_result."left", intermediate_result."right", intermediate_result.let FROM read_intermediate_result('409_1'::text, 'binary'::citus_copy_format) intermediate_result("left" documentdb_core.bson, "right" documentdb_core.bson, let documentdb_core.bson)) lookup_non_inlined_stage_1 + Node: host=localhost port=58070 dbname=regression + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result + Output: documentdb_api_internal.bson_dollar_merge_documents(intermediate_result."left", (SubPlan 1)) + Function Call: read_intermediate_result('409_1'::text, 'binary'::citus_copy_format) + SubPlan 1 + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, intermediate_result.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on documentdb_api_catalog.bson_lookup_unwind lookup_subpipeline_substage_0 + Output: lookup_subpipeline_substage_0.lookup_unwind + Function Call: documentdb_api_catalog.bson_lookup_unwind(intermediate_result."right", 'myMatch'::text) +(63 rows) + +SELECT documentdb_api.shard_collection('{ "shardCollection": "db.lookup_right", "key": { "_id": "hashed" }, "numInitialChunks": 2 }'); + shard_collection +--------------------------------------------------------------------- + +(1 row) + +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "2" }, "item" : { "$numberInt" : "2" }, "myMatch" : [ { "_id" : { "$numberInt" : "2" }, "name" : { "$numberInt" : "2" } } ] } + { "_id" : { "$numberInt" : "1" }, "item" : { "$numberInt" : "1" }, "myMatch" : [ { "_id" : { "$numberInt" : "1" }, "name" : { "$numberInt" : "1" } } ] } +(2 rows) + +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + -> Distributed Subplan 437_1 + -> Custom Scan (Citus Adaptive) + Output: remote_scan."left", remote_scan."right", remote_scan.let + -> Distributed Subplan 440_1 + -> Custom Scan (Citus Adaptive) + Output: remote_scan.document, remote_scan.let + Task Count: 2 + Tasks Shown: All + -> Task + Query: SELECT document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413232 collection WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4140_413232 collection + Output: document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Task + Query: SELECT document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM documentdb_data.documents_4140_413233 collection WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4140_413233 collection + Output: document, documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Distributed Subplan 440_2 + -> Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 2 + Tasks Shown: All + -> Task + Query: SELECT document FROM documentdb_data.documents_4141_413234 collection_0_1 WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4141_413234 collection_0_1 + Output: document + -> Task + Query: SELECT document FROM documentdb_data.documents_4141_413235 collection_0_1 WHERE true + Node: host=localhost port=58070 dbname=regression + -> Seq Scan on documentdb_data.documents_4141_413235 collection_0_1 + Output: document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT lookup_stage_1.document AS "left", "lookupRight_stage_1".document AS "right", documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(lookup_stage_1.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS let FROM ((SELECT intermediate_result.document, intermediate_result.let FROM read_intermediate_result('440_1'::text, 'binary'::citus_copy_format) intermediate_result(document documentdb_core.bson, let documentdb_core.bson)) lookup_stage_1 LEFT JOIN LATERAL (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(lookup_right_query_stage_0.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS document FROM (SELECT intermediate_result.document FROM read_intermediate_result('440_2'::text, 'binary'::citus_copy_format) intermediate_result(document documentdb_core.bson)) lookup_right_query_stage_0 WHERE documentdb_api_internal.bson_dollar_expr(lookup_right_query_stage_0.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, lookup_stage_1.let)) "lookupRight_stage_1" ON (true)) + Node: host=localhost port=58070 dbname=regression + -> Nested Loop Left Join + Output: intermediate_result.document, (COALESCE(documentdb_api_catalog.bson_array_agg(intermediate_result_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson)), documentdb_api_internal.bson_dollar_lookup_expression_eval_merge(intermediate_result.document, '{ "item_name" : "$item" }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result + Output: intermediate_result.document, intermediate_result.let + Function Call: read_intermediate_result('440_1'::text, 'binary'::citus_copy_format) + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(intermediate_result_1.document, 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result_1 + Output: intermediate_result_1.document + Function Call: read_intermediate_result('440_2'::text, 'binary'::citus_copy_format) + Filter: documentdb_api_internal.bson_dollar_expr(intermediate_result_1.document, '{ "" : { "$eq" : [ "$name", "$$item_name" ] } }'::documentdb_core.bson, intermediate_result.let) + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_dollar_merge_documents("left", (SELECT COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, lookup_non_inlined_stage_1.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) AS "funcName" FROM documentdb_api_catalog.bson_lookup_unwind(lookup_non_inlined_stage_1."right", 'myMatch'::text) lookup_subpipeline_substage_0(lookup_unwind))) AS document FROM (SELECT intermediate_result."left", intermediate_result."right", intermediate_result.let FROM read_intermediate_result('437_1'::text, 'binary'::citus_copy_format) intermediate_result("left" documentdb_core.bson, "right" documentdb_core.bson, let documentdb_core.bson)) lookup_non_inlined_stage_1 + Node: host=localhost port=58070 dbname=regression + -> Function Scan on pg_catalog.read_intermediate_result intermediate_result + Output: documentdb_api_internal.bson_dollar_merge_documents(intermediate_result."left", (SubPlan 1)) + Function Call: read_intermediate_result('437_1'::text, 'binary'::citus_copy_format) + SubPlan 1 + -> Aggregate + Output: COALESCE(documentdb_api_catalog.bson_array_agg(documentdb_api_internal.bson_dollar_project(lookup_subpipeline_substage_0.lookup_unwind, '{ "c" : { "$numberInt" : "0" } }'::documentdb_core.bson, intermediate_result.let), 'myMatch'::text), '{ "myMatch" : [ ] }'::documentdb_core.bson) + -> Function Scan on documentdb_api_catalog.bson_lookup_unwind lookup_subpipeline_substage_0 + Output: lookup_subpipeline_substage_0.lookup_unwind + Function Call: documentdb_api_catalog.bson_lookup_unwind(intermediate_result."right", 'myMatch'::text) +(65 rows) + +RESET citus.explain_all_tasks; +RESET citus.max_adaptive_executor_pool_size; diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out index 248c902e..d410c125 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out @@ -485,12 +485,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224375162" } } }'::documentdb_core.bson) AS document FROM documentdb_data.documents_8201_8200016 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '8201'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM documentdb_data.documents_8201_8200016 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '8201'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224375162" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224375162" } } }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) CosmosSearch Custom Params: { "efSearch" : 16 } -> Index Scan using foo_1 on documentdb_data.documents_8201_8200016 collection Output: document @@ -1983,12 +1983,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386092" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386092" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386092" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2029,12 +2029,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386250" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386250" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386250" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2074,12 +2074,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386404" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386404" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386404" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2119,12 +2119,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386559" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386559" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386559" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2165,12 +2165,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386711" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386711" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386711" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2211,12 +2211,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386869" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386869" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224386869" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out index 02abe2ec..b6dce322 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out @@ -430,12 +430,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224398600" } } }'::documentdb_core.bson) AS document FROM documentdb_data.documents_8100_8100003 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '8100'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM documentdb_data.documents_8100_8100003 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '8100'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224398600" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224398600" } } }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) CosmosSearch Custom Params: { "nProbes" : 150 } -> Index Scan using foo_1 on documentdb_data.documents_8100_8100003 collection Output: document @@ -2151,12 +2151,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224409985" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224409985" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224409985" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2197,12 +2197,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410138" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410138" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410138" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2242,12 +2242,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410283" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410283" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410283" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2287,12 +2287,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410427" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410427" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410427" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2333,12 +2333,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410568" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410568" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410568" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2379,12 +2379,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410710" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410710" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410710" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid @@ -2427,12 +2427,12 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410863" } } }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(c1.document, c1."orderVal"), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8101_8100018 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410863" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Nested Loop - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224410863" } } }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/create_indexes_cosmos_vector.out b/internal/pg_documentdb_distributed/src/test/regress/expected/create_indexes_cosmos_vector.out index 8bd9ac59..c4d95bd5 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/create_indexes_cosmos_vector.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/create_indexes_cosmos_vector.out @@ -533,12 +533,12 @@ EXPLAIN (costs off, verbose on) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255439" } } }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91002_91000037 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91002'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91002_91000037 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91002'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255439" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255439" } } }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <=> '[-1,2,-3]'::vector) CosmosSearch Custom Params: { "nProbes" : 1 } -> Index Scan using foo_1 on documentdb_data.documents_91002_91000037 collection Output: document @@ -633,12 +633,12 @@ EXPLAIN (costs off, verbose on) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255708" } } }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91003_91000050 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91003'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91003_91000050 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91003'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255708" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224255708" } } }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <-> '[-1,2,-3]'::vector) CosmosSearch Custom Params: { "nProbes" : 1 } -> Index Scan using sim_l2 on documentdb_data.documents_91003_91000050 collection Output: document @@ -733,12 +733,12 @@ EXPLAIN (costs off, verbose on) SELECT document FROM bson_aggregation_pipeline(' Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224256006" } } }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91004_91000066 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91004'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM documentdb_data.documents_91004_91000066 collection WHERE ((shard_key_value OPERATOR(pg_catalog.=) '91004'::bigint) AND (documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "-1.0" }, { "$numberInt" : "2" }, { "$numberDouble" : "-3.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '1'::bigint Node: host=localhost port=58070 dbname=regression -> Limit - Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224256006" } } }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector)) + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1739224256006" } } }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector) + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) <#> '[-1,2,-3]'::vector) CosmosSearch Custom Params: { "nProbes" : 1 } -> Index Scan using sim_ip on documentdb_data.documents_91004_91000066 collection Output: document diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_let.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_let.sql index 06594648..073a5a1d 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_let.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_let.sql @@ -286,3 +286,47 @@ SELECT document from bson_aggregation_pipeline('db', SELECT document from bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_let_pipelineto", "pipeline": [ { "$lookup": { "from": "aggregation_pipeline_let_pipelinefrom", "pipeline": [{ "$addFields": { "addedVal": "$$qval" }}, { "$lookup": { "from": "aggregation_pipeline_let_pipelinefrom_second", "localField": "qq", "foreignField": "qval", "as": "myExtra", "let": { "secondVar": "$quantity" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$qq", "$$secondVar" ] } }} ] }} ], "as": "myMatch", "localField": "restaurant_name", "foreignField": "name", "let": { "qval": "$qval" } }} ], "cursor": {} }'); + + +-- Test for lookup with let and $match with $expr + +SELECT documentdb_api.insert_one('db','lookup_left',' { "_id" : 1, "item" : 1 }', NULL); +SELECT documentdb_api.insert_one('db','lookup_left',' { "_id" : 2, "item" : 2 }', NULL); + +-- Fill right collection with more than 100 MB of data +DO $$ +DECLARE i int; +BEGIN +FOR i IN 1..50 LOOP +PERFORM documentdb_api.insert_one('db', 'lookup_right', FORMAT('{ "_id": %s, "name": %s, "c": { "%s": [ %s "d" ] } }', i, i, i, REPEAT('"' || i || REPEAT('a', 1000) || '", ', 5000))::documentdb_core.bson, NULL); +END LOOP; +END; +$$; + +-- This will throw size error because $match in not inlined with right query as it is not the first stage +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$project": { "c": 0 } }, { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$project": { "c": 0 } }, { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + +-- This should not throw error as $match is inlined with right query +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); + +-- Shard left collection and right collections +SET citus.explain_all_tasks to on; +SET citus.max_adaptive_executor_pool_size to 1; +SELECT documentdb_api.shard_collection('{ "shardCollection": "db.lookup_left", "key": { "_id": "hashed" }, "numInitialChunks": 2 }'); +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +SELECT documentdb_api.shard_collection('{ "shardCollection": "db.lookup_right", "key": { "_id": "hashed" }, "numInitialChunks": 2 }'); +SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +EXPLAIN (VERBOSE ON, COSTS OFF) SELECT document from bson_aggregation_pipeline('db', + '{ "aggregate": "lookup_left", "pipeline": [ { "$lookup": { "from": "lookup_right", "pipeline": [ { "$match": { "$expr": { "$eq": [ "$name", "$$item_name" ] }}}, { "$project": { "c": 0 } }], "as": "myMatch", "let": { "item_name": "$item" } }} ], "cursor": {} }'); +RESET citus.explain_all_tasks; +RESET citus.max_adaptive_executor_pool_size; \ No newline at end of file diff --git a/pg_documentdb/include/aggregation/bson_aggregation_pipeline_private.h b/pg_documentdb/include/aggregation/bson_aggregation_pipeline_private.h index a1bdf39b..e4ba0ece 100644 --- a/pg_documentdb/include/aggregation/bson_aggregation_pipeline_private.h +++ b/pg_documentdb/include/aggregation/bson_aggregation_pipeline_private.h @@ -19,6 +19,8 @@ #ifndef BSON_AGGREGATION_PIPELINE_PRIVATE_H #define BSON_AGGREGATION_PIPELINE_PRIVATE_H +typedef struct AggregationStageDefinition AggregationStageDefinition; + /* * for nested stage, use this to record its parent stage name */ @@ -32,6 +34,63 @@ typedef enum ParentStageName } ParentStageName; +/* + * Enums to represent all kind of aggregations stages. + * Please keep the list sorted within their groups for easier readability + */ +typedef enum +{ + Stage_Invalid = 0, + + /* Start internal stages Mongo */ + Stage_Internal_InhibitOptimization = 1, + + /* Start Mongo Public stages */ + Stage_AddFields = 10, + Stage_Bucket, + Stage_BucketAuto, + Stage_ChangeStream, + Stage_CollStats, + Stage_Count, + Stage_CurrentOp, + Stage_Densify, + Stage_Documents, + Stage_Facet, + Stage_Fill, + Stage_GeoNear, + Stage_GraphLookup, + Stage_Group, + Stage_IndexStats, + Stage_Limit, + Stage_ListLocalSessions, + Stage_ListSessions, + Stage_Lookup, + Stage_Match, + Stage_Merge, + Stage_Out, + Stage_Project, + Stage_Redact, + Stage_ReplaceRoot, + Stage_ReplaceWith, + Stage_Sample, + Stage_Search, + Stage_SearchMeta, + Stage_Set, + Stage_SetWindowFields, + Stage_Skip, + Stage_Sort, + Stage_SortByCount, + Stage_UnionWith, + Stage_Unset, + Stage_Unwind, + Stage_VectorSearch, + + /* Start of pg_documentdb Custom or internal stages */ + Stage_InverseMatch = 100, + Stage_LookupUnwind +} Stage; + + /* * Shared context during aggregation pipeline build phase. */ @@ -120,6 +179,16 @@ typedef struct } AggregationPipelineBuildContext; +typedef struct +{ + /* The bson value of the pipeline spec */ + bson_value_t stageValue; + + /* Definition of internal handlers */ + AggregationStageDefinition *stageDefinition; +} AggregationStage; + + /* Core Infra exports */ Query * MutateQueryWithPipeline(Query *query, List *aggregationStages, AggregationPipelineBuildContext *context); @@ -219,6 +288,7 @@ bool IsPartitionByFieldsOnShardKey(const pgbson *partitionByFields, const MongoCollection *collection); Expr * GenerateMultiExpressionRepathExpression(List *repathArgs); +Stage GetAggregationStageAtPosition(const List *aggregationStages, int position); /* Helper methods */ diff --git a/pg_documentdb/src/aggregation/bson_aggregation_nested_pipeline.c b/pg_documentdb/src/aggregation/bson_aggregation_nested_pipeline.c index fb27e068..656f19bd 100644 --- a/pg_documentdb/src/aggregation/bson_aggregation_nested_pipeline.c +++ b/pg_documentdb/src/aggregation/bson_aggregation_nested_pipeline.c @@ -50,6 +50,7 @@ const int MaximumLookupPipelineDepth = 20; extern bool EnableLookupIdJoinOptimizationOnCollation; extern bool EnableNowSystemVariable; +extern bool EnableMatchWithLetInLookup; /* * Struct having parsed view of the @@ -112,15 +113,15 @@ typedef struct LookupOptimizationArgs bool isLookupUncorrelated; /* - * The segment of the pipeline that can be inlined. + * The segment of the pipeline stages that can be inlined. */ - bson_value_t inlinedLookupPipeline; + List *inlinedPipelineStages; /* - * The segment of the pipeline that cannot be inlined and needs + * The segment of the pipeline stages that cannot be inlined and needs * to be applied post-join. */ - bson_value_t nonInlinedLookupPipeline; + List *nonInlinedPipelineStages; /* * Can the join be done on the right query's _id? @@ -148,6 +149,19 @@ typedef struct LookupOptimizationArgs * has a let that is non const. */ bool hasLet; + + /* + * Referrence to the first non-inlined $match stage with $expr qualifiers which + * was not inlined due to the presence of a `let`. + * We do a best effort inlining of this stage close to the right query to avoid + * the stage post-JOIN + */ + AggregationStage *nonInlinedMatchStage; + + /* + * The attrNum for the lookup let in left query + */ + AttrNumber lookupLetAttrNum; } LookupOptimizationArgs; @@ -1949,10 +1963,19 @@ OptimizeLookup(LookupArgs *lookupArgs, strncpy((char *) optimizationArgs->rightQueryContext.collationString, leftQueryContext->collationString, MAX_ICU_COLLATION_LENGTH); optimizationArgs->rightQueryContext.parentStageName = ParentStageName_LOOKUP; + optimizationArgs->rightQueryContext.optimizePipelineStages = + leftQueryContext->optimizePipelineStages; optimizationArgs->isLookupAgnostic = lookupArgs->from.length == 0; optimizationArgs->isLookupUncorrelated = !lookupArgs->hasLookupMatch; + bson_value_t inlinedLookupPipeline = (bson_value_t) { + 0 + }; + bson_value_t nonInlinedLookupPipeline = (bson_value_t) { + 0 + }; + if (leftQueryContext->variableSpec != NULL && !IsA(leftQueryContext->variableSpec, Const)) { @@ -2001,6 +2024,44 @@ OptimizeLookup(LookupArgs *lookupArgs, } } + /* If there is lookup let, we will build the let in the left query and then the let expression + * should be used by the right query / post join stages wherever needed. + */ + Const *letConstValue = MakeBsonConst(lookupArgs->let); + + List *args; + Oid funcOid; + Expr *sourceVariableSpec = leftQueryContext->variableSpec; + if (sourceVariableSpec == NULL) + { + sourceVariableSpec = (Expr *) MakeBsonConst(PgbsonInitEmpty()); + } + + Expr *documentExpr = linitial_node(TargetEntry, leftQuery->targetList)->expr; + args = list_make3(documentExpr, letConstValue, sourceVariableSpec); + funcOid = BsonDollarLookupExpressionEvalMergeOid(); + + Expr *letExpr = (Expr *) makeFuncExpr(funcOid, BsonTypeId(), args, InvalidOid, + InvalidOid, COERCE_EXPLICIT_CALL); + + optimizationArgs->lookupLetAttrNum = list_length(leftQuery->targetList) + 1; + TargetEntry *lookupLetEntry = makeTargetEntry(letExpr, + optimizationArgs->lookupLetAttrNum, + "let", false); + leftQuery->targetList = lappend(leftQuery->targetList, lookupLetEntry); + + /* + * Make a VAR to access the above let in right query. + * Today the right query is pushed to a CTE for and this Var is + * used to support special case non-inline stages i.e. $match + * so varlevelsup is 1 + */ + Index varLevelsUp = 1; + int leftQueryVarNo = 1; + Expr *letVarExpr = (Expr *) makeVar(leftQueryVarNo, + optimizationArgs->lookupLetAttrNum, + BsonTypeId(), -1, InvalidOid, varLevelsUp); + optimizationArgs->rightQueryContext.variableSpec = letVarExpr; optimizationArgs->hasLet = true; } @@ -2071,23 +2132,23 @@ OptimizeLookup(LookupArgs *lookupArgs, /* No point in inlining lookup pipeline on agnostic - it Has to be applied */ if (lookupArgs->pipeline.value_type == BSON_TYPE_EOD) { - optimizationArgs->inlinedLookupPipeline = lookupArgs->pipeline; - optimizationArgs->nonInlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = (bson_value_t) { 0 }; } else if (IsBsonValueEmptyArray(&lookupArgs->pipeline)) { - optimizationArgs->inlinedLookupPipeline = lookupArgs->pipeline; - optimizationArgs->nonInlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = (bson_value_t) { 0 }; } else if (optimizationArgs->isLookupUncorrelated || optimizationArgs->isLookupAgnostic) { - optimizationArgs->inlinedLookupPipeline = lookupArgs->pipeline; - optimizationArgs->nonInlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = (bson_value_t) { 0 }; } @@ -2097,10 +2158,10 @@ OptimizeLookup(LookupArgs *lookupArgs, * This is because it's better to join on _id (index pushdown) then apply the pipeline on * the result. */ - optimizationArgs->inlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = (bson_value_t) { 0 }; - optimizationArgs->nonInlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = lookupArgs->pipeline; } else { @@ -2123,16 +2184,16 @@ OptimizeLookup(LookupArgs *lookupArgs, if (!isPipelineValid) { /* Invalid pipeline - just make it inlined to trigger the error */ - optimizationArgs->inlinedLookupPipeline = lookupArgs->pipeline; - optimizationArgs->nonInlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = (bson_value_t) { 0 }; } else if (canInlinePipelineCore) { /* The full pipeline can be inlined */ - optimizationArgs->inlinedLookupPipeline = lookupArgs->pipeline; - optimizationArgs->nonInlinedLookupPipeline = (bson_value_t) { + inlinedLookupPipeline = lookupArgs->pipeline; + nonInlinedLookupPipeline = (bson_value_t) { 0 }; } @@ -2144,7 +2205,7 @@ OptimizeLookup(LookupArgs *lookupArgs, PgbsonToSinglePgbsonElement(inlinedPipeline, &pipelineElement); if (!IsBsonValueEmptyArray(&pipelineElement.bsonValue)) { - optimizationArgs->inlinedLookupPipeline = pipelineElement.bsonValue; + inlinedLookupPipeline = pipelineElement.bsonValue; } } @@ -2153,8 +2214,7 @@ OptimizeLookup(LookupArgs *lookupArgs, PgbsonToSinglePgbsonElement(nonInlinedPipeline, &pipelineElement); if (!IsBsonValueEmptyArray(&pipelineElement.bsonValue)) { - optimizationArgs->nonInlinedLookupPipeline = - pipelineElement.bsonValue; + nonInlinedLookupPipeline = pipelineElement.bsonValue; } } } @@ -2193,6 +2253,31 @@ OptimizeLookup(LookupArgs *lookupArgs, { ValidatePipelineForShardedLookupWithLet(&lookupArgs->pipeline); } + + /* + * Extract the inline and non inline pipeline stages after everythin is done. + */ + optimizationArgs->inlinedPipelineStages = ExtractAggregationStages( + &inlinedLookupPipeline, &optimizationArgs->rightQueryContext); + optimizationArgs->nonInlinedPipelineStages = ExtractAggregationStages( + &nonInlinedLookupPipeline, &optimizationArgs->rightQueryContext); + + /* + * If the first non-inline stages is a $match (which contains let and $expr), + * and we don't have a better match plan e.g. join on righ collection _id then + * we need to handle it similar to hasLookupMatch + */ + Stage firstNonInlineStage = GetAggregationStageAtPosition( + optimizationArgs->nonInlinedPipelineStages, 0); + if (EnableMatchWithLetInLookup && + !optimizationArgs->isLookupJoinOnRightId && + firstNonInlineStage == Stage_Match) + { + optimizationArgs->nonInlinedMatchStage = (AggregationStage *) linitial( + optimizationArgs->nonInlinedPipelineStages); + optimizationArgs->nonInlinedPipelineStages = + list_delete_first(optimizationArgs->nonInlinedPipelineStages); + } } @@ -2240,14 +2325,10 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, parseState->p_next_resno = 1; /* Process the right query where possible */ - if (optimizationArgs.inlinedLookupPipeline.value_type != BSON_TYPE_EOD) + if (list_length(optimizationArgs.inlinedPipelineStages) > 0) { - /* If lookup is purely a pipeline (uncorrelated subquery) then - * modify the pipeline. */ - List *stages = ExtractAggregationStages(&optimizationArgs.inlinedLookupPipeline, - &optimizationArgs.rightQueryContext); rightQuery = MutateQueryWithPipeline(rightQuery, - stages, + optimizationArgs.inlinedPipelineStages, &optimizationArgs.rightQueryContext); } @@ -2262,7 +2343,7 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, /* Check if the pipeline can be pushed to the inner query (right collection) * If it can, then it's inlined. If not, we apply the pipeline post-join. */ - if (lookupArgs->hasLookupMatch) + if (lookupArgs->hasLookupMatch || optimizationArgs.nonInlinedMatchStage != NULL) { /* We can apply the optimization on this based on object_id if and only if * The right table is pointing directly to an actual table (not a view) @@ -2297,128 +2378,142 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, rightQuery = CreateCteSelectQuery(rightTableExpr, "lookup_right_query", context->nestedPipelineLevel, 0); - /* It's a join on a field, first add a TargetEntry for left for bson_dollar_lookup_extract_filter_expression */ - TargetEntry *currentEntry = linitial(leftQuery->targetList); - - /* The extract query right arg is a simple bson of the form { "remoteField": "localField" } */ - pgbson_writer filterWriter; - PgbsonWriterInit(&filterWriter); - PgbsonWriterAppendUtf8(&filterWriter, lookupArgs->foreignField.string, - lookupArgs->foreignField.length, - lookupArgs->localField.string); - - if (IsCollationApplicable(context->collationString)) + if (lookupArgs->hasLookupMatch) { - PgbsonWriterAppendUtf8(&filterWriter, "collation", 9, - context->collationString); + /* It's a join on a field, first add a TargetEntry for left for bson_dollar_lookup_extract_filter_expression */ + TargetEntry *currentEntry = linitial(leftQuery->targetList); + + /* The extract query right arg is a simple bson of the form { "remoteField": "localField" } */ + pgbson_writer filterWriter; + PgbsonWriterInit(&filterWriter); + PgbsonWriterAppendUtf8(&filterWriter, lookupArgs->foreignField.string, + lookupArgs->foreignField.length, + lookupArgs->localField.string); + + if (IsCollationApplicable(context->collationString)) + { + PgbsonWriterAppendUtf8(&filterWriter, "collation", 9, + context->collationString); + } + + pgbson *filterBson = PgbsonWriterGetPgbson(&filterWriter); + + /* Create the bson_dollar_lookup_extract_filter_expression(document, 'filter') */ + List *extractFilterArgs = list_make2(currentEntry->expr, MakeBsonConst( + filterBson)); + + Expr *projectorFunc; + if (optimizationArgs.isLookupJoinOnLeftId) + { + /* If we can join on the left _id, then just use object_id */ + projectorFunc = (Expr *) makeVar(1, + DOCUMENT_DATA_TABLE_OBJECT_ID_VAR_ATTR_NUMBER, + BsonTypeId(), -1, InvalidOid, 0); + } + else if (optimizationArgs.isLookupJoinOnRightId) + { + projectorFunc = (Expr *) makeFuncExpr( + BsonLookupExtractFilterArrayFunctionOid(), GetBsonArrayTypeOid(), + extractFilterArgs, + InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); + } + else + { + Oid extractFunctionOid = + DocumentDBApiInternalBsonLookupExtractFilterExpressionFunctionOid(); + + projectorFunc = (Expr *) makeFuncExpr( + extractFunctionOid, BsonTypeId(), + extractFilterArgs, + InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); + } + + AttrNumber newProjectorAttrNum = list_length(leftQuery->targetList) + 1; + TargetEntry *extractFilterProjector = makeTargetEntry((Expr *) projectorFunc, + newProjectorAttrNum, + "lookup_filter", false); + leftQuery->targetList = lappend(leftQuery->targetList, + extractFilterProjector); + + /* now on the right query, add a filter referencing this projector */ + List *rightQuals = NIL; + if (rightQuery->jointree->quals != NULL) + { + rightQuals = make_ands_implicit((Expr *) rightQuery->jointree->quals); + } + + /* add the WHERE bson_dollar_in(t2.document, t1.match) */ + TargetEntry *currentRightEntry = linitial(rightQuery->targetList); + Var *rightVar = (Var *) currentRightEntry->expr; + int matchLevelsUp = 1; + Node *inClause; + if (optimizationArgs.isLookupJoinOnLeftId) + { + /* Do a direct equality against object_id */ + Assert(list_length(rightQuery->targetList) == 2); + TargetEntry *rightObjectIdEntry = (TargetEntry *) lsecond( + rightQuery->targetList); + rightQuery->targetList = list_make1(currentRightEntry); + + Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, + BsonTypeId(), -1, + InvalidOid, matchLevelsUp); + inClause = (Node *) make_opclause(BsonEqualOperatorId(), BOOLOID, false, + copyObject( + rightObjectIdEntry->expr), + (Expr *) matchVar, InvalidOid, + InvalidOid); + } + else if (optimizationArgs.isLookupJoinOnRightId) + { + Assert(list_length(rightQuery->targetList) == 2); + TargetEntry *rightObjectIdEntry = (TargetEntry *) lsecond( + rightQuery->targetList); + rightQuery->targetList = list_make1(currentRightEntry); + + ScalarArrayOpExpr *inOperator = makeNode(ScalarArrayOpExpr); + inOperator->useOr = true; + inOperator->opno = BsonEqualOperatorId(); + Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, + GetBsonArrayTypeOid(), -1, + InvalidOid, matchLevelsUp); + List *inArgs = list_make2(copyObject(rightObjectIdEntry->expr), matchVar); + inOperator->args = inArgs; + inClause = (Node *) inOperator; + } + else + { + Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, + BsonTypeId(), + -1, + InvalidOid, matchLevelsUp); + Const *textConst = MakeTextConst(lookupArgs->foreignField.string, + lookupArgs->foreignField.length); + List *inArgs = list_make3(copyObject(rightVar), matchVar, textConst); + inClause = (Node *) makeFuncExpr(BsonDollarLookupJoinFilterFunctionOid(), + BOOLOID, inArgs, + InvalidOid, InvalidOid, + COERCE_EXPLICIT_CALL); + } + + if (rightQuals == NIL) + { + rightQuery->jointree->quals = inClause; + } + else + { + rightQuals = lappend(rightQuals, inClause); + rightQuery->jointree->quals = (Node *) make_ands_explicit(rightQuals); + } } - pgbson *filterBson = PgbsonWriterGetPgbson(&filterWriter); - - /* Create the bson_dollar_lookup_extract_filter_expression(document, 'filter') */ - List *extractFilterArgs = list_make2(currentEntry->expr, MakeBsonConst( - filterBson)); - - Expr *projectorFunc; - if (optimizationArgs.isLookupJoinOnLeftId) + /* + * Add the $match with $expr to the right query + */ + if (optimizationArgs.nonInlinedMatchStage != NULL) { - /* If we can join on the left _id, then just use object_id */ - projectorFunc = (Expr *) makeVar(1, - DOCUMENT_DATA_TABLE_OBJECT_ID_VAR_ATTR_NUMBER, - BsonTypeId(), -1, InvalidOid, 0); - } - else if (optimizationArgs.isLookupJoinOnRightId) - { - projectorFunc = (Expr *) makeFuncExpr( - BsonLookupExtractFilterArrayFunctionOid(), GetBsonArrayTypeOid(), - extractFilterArgs, - InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); - } - else - { - Oid extractFunctionOid = - DocumentDBApiInternalBsonLookupExtractFilterExpressionFunctionOid(); - - projectorFunc = (Expr *) makeFuncExpr( - extractFunctionOid, BsonTypeId(), - extractFilterArgs, - InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); - } - - AttrNumber newProjectorAttrNum = list_length(leftQuery->targetList) + 1; - TargetEntry *extractFilterProjector = makeTargetEntry((Expr *) projectorFunc, - newProjectorAttrNum, - "lookup_filter", false); - leftQuery->targetList = lappend(leftQuery->targetList, extractFilterProjector); - - /* now on the right query, add a filter referencing this projector */ - List *rightQuals = NIL; - if (rightQuery->jointree->quals != NULL) - { - rightQuals = make_ands_implicit((Expr *) rightQuery->jointree->quals); - } - - /* add the WHERE bson_dollar_in(t2.document, t1.match) */ - TargetEntry *currentRightEntry = linitial(rightQuery->targetList); - Var *rightVar = (Var *) currentRightEntry->expr; - int matchLevelsUp = 1; - Node *inClause; - if (optimizationArgs.isLookupJoinOnLeftId) - { - /* Do a direct equality against object_id */ - Assert(list_length(rightQuery->targetList) == 2); - TargetEntry *rightObjectIdEntry = (TargetEntry *) lsecond( - rightQuery->targetList); - rightQuery->targetList = list_make1(currentRightEntry); - - Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, - BsonTypeId(), -1, - InvalidOid, matchLevelsUp); - inClause = (Node *) make_opclause(BsonEqualOperatorId(), BOOLOID, false, - copyObject( - rightObjectIdEntry->expr), - (Expr *) matchVar, InvalidOid, - InvalidOid); - } - else if (optimizationArgs.isLookupJoinOnRightId) - { - Assert(list_length(rightQuery->targetList) == 2); - TargetEntry *rightObjectIdEntry = (TargetEntry *) lsecond( - rightQuery->targetList); - rightQuery->targetList = list_make1(currentRightEntry); - - ScalarArrayOpExpr *inOperator = makeNode(ScalarArrayOpExpr); - inOperator->useOr = true; - inOperator->opno = BsonEqualOperatorId(); - Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, - GetBsonArrayTypeOid(), -1, - InvalidOid, matchLevelsUp); - List *inArgs = list_make2(copyObject(rightObjectIdEntry->expr), matchVar); - inOperator->args = inArgs; - inClause = (Node *) inOperator; - } - else - { - Var *matchVar = makeVar(leftQueryRteIndex, newProjectorAttrNum, BsonTypeId(), - -1, - InvalidOid, matchLevelsUp); - Const *textConst = MakeTextConst(lookupArgs->foreignField.string, - lookupArgs->foreignField.length); - List *inArgs = list_make3(copyObject(rightVar), matchVar, textConst); - inClause = (Node *) makeFuncExpr(BsonDollarLookupJoinFilterFunctionOid(), - BOOLOID, inArgs, - InvalidOid, InvalidOid, - COERCE_EXPLICIT_CALL); - } - - if (rightQuals == NIL) - { - rightQuery->jointree->quals = inClause; - } - else - { - rightQuals = lappend(rightQuals, inClause); - rightQuery->jointree->quals = (Node *) make_ands_explicit(rightQuals); + HandleMatch(&optimizationArgs.nonInlinedMatchStage->stageValue, rightQuery, + &optimizationArgs.rightQueryContext); } /* Add the bson_array_agg function */ @@ -2528,7 +2623,7 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, * We do it here since it's easier to deal with the pipeline * post join (fewer queries to think about and manage). */ - if (optimizationArgs.nonInlinedLookupPipeline.value_type != BSON_TYPE_EOD) + if (list_length(optimizationArgs.nonInlinedPipelineStages) > 0) { resetStringInfo(cteStr); appendStringInfo(cteStr, "lookup_join_cte_%d", context->nestedPipelineLevel); @@ -2536,7 +2631,11 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, lookupCte->ctename = cteStr->data; lookupCte->ctequery = (Node *) lookupQuery; - /* Before creating the CTE - add the $let */ + /* + * Before creating the CTE - add the $let from left query to be used by post join pipeline + * It's a deliberate choice to rebuild the let expression here instead of reusing the one that is available + * because the expressions are not CONST and can pose problems if not handled properly. + */ if (lookupArgs->let) { /* Evaluate the let against the leftExpr */ @@ -2615,12 +2714,8 @@ ProcessLookupCoreWithLet(Query *query, AggregationPipelineBuildContext *context, strncpy((char *) projectorQueryContext.collationString, context->collationString, MAX_ICU_COLLATION_LENGTH); - - List *stages = ExtractAggregationStages( - &optimizationArgs.nonInlinedLookupPipeline, - &projectorQueryContext); subSelectQuery = MutateQueryWithPipeline(subSelectQuery, - stages, + optimizationArgs.nonInlinedPipelineStages, &projectorQueryContext); if (list_length(subSelectQuery->targetList) > 1) diff --git a/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c b/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c index ccabd450..e1058120 100644 --- a/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c +++ b/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c @@ -106,67 +106,10 @@ typedef bool (*CanInlineLookupStage)(const bson_value_t *stageValue, const StringView *lookupPath, bool hasLet); -/* - * Enums to represent all kind of aggregations stages. - * Please keep the list sorted within their groups for easier readability - */ -typedef enum -{ - Stage_Invalid = 0, - - /* Start internal stages Mongo */ - Stage_Internal_InhibitOptimization = 1, - - /* Start Mongo Public stages */ - Stage_AddFields = 10, - Stage_Bucket, - Stage_BucketAuto, - Stage_ChangeStream, - Stage_CollStats, - Stage_Count, - Stage_CurrentOp, - Stage_Densify, - Stage_Documents, - Stage_Facet, - Stage_Fill, - Stage_GeoNear, - Stage_GraphLookup, - Stage_Group, - Stage_IndexStats, - Stage_Limit, - Stage_ListLocalSessions, - Stage_ListSessions, - Stage_Lookup, - Stage_Match, - Stage_Merge, - Stage_Out, - Stage_Project, - Stage_Redact, - Stage_ReplaceRoot, - Stage_ReplaceWith, - Stage_Sample, - Stage_Search, - Stage_SearchMeta, - Stage_Set, - Stage_SetWindowFields, - Stage_Skip, - Stage_Sort, - Stage_SortByCount, - Stage_UnionWith, - Stage_Unset, - Stage_Unwind, - Stage_VectorSearch, - - /* Start of pg_documentdb Custom or internal stages */ - Stage_InverseMatch = 100, - Stage_LookupUnwind -} Stage; - - /* * Declaration of a given aggregation pipeline stage. */ -typedef struct +typedef struct AggregationStageDefinition { /* The stage name in Mongo format (e.g. $addFields, $project) */ const char *stage; @@ -204,15 +147,6 @@ typedef struct Stage stageEnum; } AggregationStageDefinition; -typedef struct -{ - /* The bson value of the pipeline spec */ - bson_value_t stageValue; - - /* Definition of internal handlers */ - AggregationStageDefinition *stageDefinition; -} AggregationStage; - static void AddCursorFunctionsToQuery(Query *query, Query *baseQuery, QueryData *queryData, @@ -2550,6 +2484,22 @@ ValidateElementForNGroupAccumulators(bson_value_t *elementsToFetch, const } +/* + * Given valid aggregation pipeline stages, this function returns the stage enum at the given position. + */ +Stage +GetAggregationStageAtPosition(const List *aggregationStages, int position) +{ + if (list_length(aggregationStages) <= position) + { + return Stage_Invalid; + } + + AggregationStage *stage = list_nth(aggregationStages, position); + return stage->stageDefinition->stageEnum; +} + + /* * Mutates the query for the $addFields stage */ @@ -6024,6 +5974,12 @@ List * ExtractAggregationStages(const bson_value_t *pipelineValue, AggregationPipelineBuildContext *context) { + if (pipelineValue->value_type != BSON_TYPE_ARRAY || + IsBsonValueEmptyArray(pipelineValue)) + { + return NIL; + } + bson_iter_t pipelineIterator; BsonValueInitIterator(pipelineValue, &pipelineIterator); diff --git a/pg_documentdb/src/configs/feature_flag_configs.c b/pg_documentdb/src/configs/feature_flag_configs.c index 3f79833a..a8fc99d9 100644 --- a/pg_documentdb/src/configs/feature_flag_configs.c +++ b/pg_documentdb/src/configs/feature_flag_configs.c @@ -107,6 +107,10 @@ bool EnableSimplifyGroupAccumulators = DEFAULT_ENABLE_SIMPLIFY_GROUP_ACCUMULATOR bool EnableSortbyIdPushDownToPrimaryKey = DEFAULT_ENABLE_SORT_BY_ID_PUSHDOWN_TO_PRIMARYKEY; +#define DEFAULT_ENABLE_MATCH_WITH_LET_IN_LOOKUP true +bool EnableMatchWithLetInLookup = + DEFAULT_ENABLE_MATCH_WITH_LET_IN_LOOKUP; + void InitializeFeatureFlagConfigurations(const char *prefix, const char *newGucPrefix) { @@ -307,4 +311,12 @@ InitializeFeatureFlagConfigurations(const char *prefix, const char *newGucPrefix NULL, &EnableSortbyIdPushDownToPrimaryKey, DEFAULT_ENABLE_SORT_BY_ID_PUSHDOWN_TO_PRIMARYKEY, PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomBoolVariable( + psprintf("%s.enableMatchWithLetInLookup", newGucPrefix), + gettext_noop( + "Whether or not to inline $match with lookup let variables."), + NULL, &EnableMatchWithLetInLookup, + DEFAULT_ENABLE_MATCH_WITH_LET_IN_LOOKUP, + PGC_USERSET, 0, NULL, NULL, NULL); }