* implements $expr functionality * in the runtime. Evaluates the expression pointed to by the filter * against the document. Returns true in the following cases: * 1) The expression evaluated to a boolean result and is true. * 2) The expression evaluated to a numeric and is not 0. * 3) The expression evaluated to a result that is defined. */
| 1734 | * 3) The expression evaluated to a result that is defined. |
| 1735 | */ |
| 1736 | Datum |
| 1737 | bson_dollar_expr(PG_FUNCTION_ARGS) |
| 1738 | { |
| 1739 | pgbson *document = PG_GETARG_PGBSON(0); |
| 1740 | pgbson *filter = PG_GETARG_PGBSON(1); |
| 1741 | pgbson *variablesContext = NULL; |
| 1742 | |
| 1743 | int argPositions[2] = { 1, 2 }; |
| 1744 | int numArgs = 1; |
| 1745 | |
| 1746 | if (PG_NARGS() > 2) |
| 1747 | { |
| 1748 | variablesContext = PG_GETARG_MAYBE_NULL_PGBSON(2); |
| 1749 | numArgs = 2; |
| 1750 | } |
| 1751 | |
| 1752 | const BsonDollarExprQueryState *cachedExprQueryState = NULL; |
| 1753 | BsonDollarExprQueryState localState = { 0 }; |
| 1754 | SetCachedFunctionStateMultiArgs( |
| 1755 | cachedExprQueryState, |
| 1756 | BsonDollarExprQueryState, |
| 1757 | argPositions, |
| 1758 | numArgs, |
| 1759 | PopulateExprStateFromQuery, |
| 1760 | filter, |
| 1761 | variablesContext); |
| 1762 | |
| 1763 | if (cachedExprQueryState == NULL) |
| 1764 | { |
| 1765 | PopulateExprStateFromQuery(&localState, filter, variablesContext); |
| 1766 | cachedExprQueryState = &localState; |
| 1767 | } |
| 1768 | |
| 1769 | pgbson_writer writer; |
| 1770 | PgbsonWriterInit(&writer); |
| 1771 | bool isNullOnEmpty = false; |
| 1772 | StringView emptyPathView = { .length = 0, .string = "" }; |
| 1773 | EvaluateAggregationExpressionDataToWriter( |
| 1774 | cachedExprQueryState->expression, |
| 1775 | document, emptyPathView, &writer, |
| 1776 | cachedExprQueryState->variableContext, |
| 1777 | isNullOnEmpty); |
| 1778 | |
| 1779 | bson_iter_t resultIterator; |
| 1780 | PgbsonWriterGetIterator(&writer, &resultIterator); |
| 1781 | |
| 1782 | if (!bson_iter_next(&resultIterator)) |
| 1783 | { |
| 1784 | PG_RETURN_BOOL(false); |
| 1785 | } |
| 1786 | |
| 1787 | pgbsonelement resultElement; |
| 1788 | BsonIterToPgbsonElement(&resultIterator, &resultElement); |
| 1789 | if (BsonValueIsNumberOrBool(&resultElement.bsonValue)) |
| 1790 | { |
| 1791 | PG_RETURN_BOOL(BsonValueAsInt32(&resultElement.bsonValue) != 0); |
| 1792 | } |
| 1793 |
nothing calls this directly
no test coverage detected