| 878 | |
| 879 | template <typename TEval> |
| 880 | void Expr::evaluateSharedSubexpr( |
| 881 | const SelectivityVector& rows, |
| 882 | EvalCtx& context, |
| 883 | VectorPtr& result, |
| 884 | TEval eval) { |
| 885 | // Captures the inputs referenced by distinctFields_. |
| 886 | std::vector<const BaseVector*> expressionInputFields; |
| 887 | for (auto* field : distinctFields_) { |
| 888 | expressionInputFields.push_back( |
| 889 | context.getField(field->index(context)).get()); |
| 890 | } |
| 891 | |
| 892 | // Find the cached results for the same inputs, or create an entry if one |
| 893 | // doesn't exist. |
| 894 | auto sharedSubexprResultsIter = |
| 895 | sharedSubexprResults_.find(expressionInputFields); |
| 896 | if (sharedSubexprResultsIter == sharedSubexprResults_.end()) { |
| 897 | auto maxSharedSubexprResultsCached = context.execCtx() |
| 898 | ->queryCtx() |
| 899 | ->queryConfig() |
| 900 | .maxSharedSubexprResultsCached(); |
| 901 | if (sharedSubexprResults_.size() < maxSharedSubexprResultsCached) { |
| 902 | // If we have room left in the cache, add it. |
| 903 | sharedSubexprResultsIter = |
| 904 | sharedSubexprResults_ |
| 905 | .insert( |
| 906 | std::pair(std::move(expressionInputFields), SharedResults())) |
| 907 | .first; |
| 908 | } else { |
| 909 | // Otherwise, simply evaluate it and return without caching the results. |
| 910 | eval(rows, context, result); |
| 911 | |
| 912 | return; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | auto& [sharedSubexprRows, sharedSubexprValues] = |
| 917 | sharedSubexprResultsIter->second; |
| 918 | |
| 919 | if (sharedSubexprValues == nullptr) { |
| 920 | eval(rows, context, result); |
| 921 | |
| 922 | if (!sharedSubexprRows) { |
| 923 | sharedSubexprRows = context.execCtx()->getSelectivityVector(rows.end()); |
| 924 | } |
| 925 | |
| 926 | *sharedSubexprRows = rows; |
| 927 | if (context.errors()) { |
| 928 | // Clear the rows which failed to compute. |
| 929 | context.deselectErrors(*sharedSubexprRows); |
| 930 | if (!sharedSubexprRows->hasSelections()) { |
| 931 | // Do not store a reference to 'result' if we cannot use any rows from |
| 932 | // it. |
| 933 | return; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | sharedSubexprValues = result; |
nothing calls this directly
no test coverage detected