MCPcopy Create free account
hub / github.com/documentdb/documentdb / TryOptimizeDollarOrExpr

Function TryOptimizeDollarOrExpr

pg_documentdb/src/query/query_operator.c:4852–5002  ·  view source on GitHub ↗

* Optimize for common expression scenarios for $or * Currently handled optimizations: * { PATH: { $exists: false } } OR { PATH: { $eq: null } } -> { $eq: null } */

Source from the content-addressed store, hash-verified

4850 * { PATH: { $exists: false } } OR { PATH: { $eq: null } } -> { $eq: null }
4851 */
4852static Expr *
4853TryOptimizeDollarOrExpr(BsonQueryOperatorContext *context,
4854 List *orQuals)
4855{
4856 if (list_length(orQuals) == 1)
4857 {
4858 /* $or of a single qual is the qual */
4859 return (Expr *) linitial(orQuals);
4860 }
4861
4862 /* For now, only optimize when $or has 2 entries */
4863 if (list_length(orQuals) != 2)
4864 {
4865 return NULL;
4866 }
4867
4868 const MongoQueryOperator *eqop = GetMongoQueryOperatorByQueryOperatorType(
4869 QUERY_OPERATOR_EQ, context->inputType);
4870 const MongoQueryOperator *existsop = GetMongoQueryOperatorByQueryOperatorType(
4871 QUERY_OPERATOR_EXISTS, context->inputType);
4872
4873 Expr *firstArg = NULL;
4874 Expr *equalsNullExpr = NULL;
4875 StringView singlePathEqualsNull = { 0 };
4876 StringView singlePathExistsFalse = { 0 };
4877 ListCell *cell;
4878 foreach(cell, orQuals)
4879 {
4880 Expr *currentExpr = lfirst(cell);
4881
4882 Oid funcId;
4883 List *args;
4884 if (IsA(currentExpr, FuncExpr))
4885 {
4886 FuncExpr *expr = (FuncExpr *) currentExpr;
4887 funcId = expr->funcid;
4888 args = expr->args;
4889 }
4890 else if (IsA(currentExpr, OpExpr))
4891 {
4892 OpExpr *opExpr = (OpExpr *) currentExpr;
4893 funcId = opExpr->opfuncid;
4894 args = opExpr->args;
4895 }
4896 else
4897 {
4898 return NULL;
4899 }
4900
4901 if (funcId != eqop->postgresRuntimeFunctionOidLookup() &&
4902 funcId != existsop->postgresRuntimeFunctionOidLookup())
4903 {
4904 continue;
4905 }
4906
4907 Expr *currentFirstArg = linitial(args);
4908 Expr *secondArg = lsecond(args);
4909

Callers 1

Calls 5

StringViewEqualsFunction · 0.85
BsonValueAsBoolFunction · 0.85
foreachFunction · 0.50

Tested by

no test coverage detected