Repository navigation
[core] Add expression filter support - #11251
Conversation
anandthakker
left a comment
There was a problem hiding this comment.
👍 looking good to me!
| std::shared_ptr<expression::Expression> expression; | ||
|
|
||
| friend bool operator==(const ExpressionFilter& lhs, const ExpressionFilter& rhs) { | ||
| return lhs.expression == rhs.expression; |
There was a problem hiding this comment.
Note that this is checking the pointers rather than the expression objects themselves. (*(lhs.expression) == *(rhs.expression) to do a deep equality check)
There was a problem hiding this comment.
Are you sure? I can't find a definitive answer but Googling around seems to indicate C++ will use ExpressionFilter::operator== rather than an address check.
There was a problem hiding this comment.
Pretty sure -- according to http://en.cppreference.com/w/cpp/memory/shared_ptr/operator_cmp, shared_ptr::operator== simply compares the wrapped Expression* pointers, as opposed to invoking Expression::operator==(). Confirmed here: http://cpp.sh/7yskf
There was a problem hiding this comment.
Oh, doh, I was thinking about the ExpressionFilter&s, not the shared_ptr<Expression>s 😄
|
|
||
| class ExpressionFilter { | ||
| public: | ||
| std::shared_ptr<expression::Expression> expression; |
There was a problem hiding this comment.
Could this be a unique_ptr?
There was a problem hiding this comment.
I think so. Will try that.
There was a problem hiding this comment.
Update: this won't work because ExpressionFilter needs to be copyable
There was a problem hiding this comment.
Can it be std::shared_ptr<const expression::Expression>?
| } | ||
|
|
||
| void operator()(const ExpressionFilter&) { | ||
| stringifyUnaryFilter("herp", "derp"); |
|
|
||
| template <class PropertyAccessor> | ||
| bool operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const; | ||
| bool operator()(expression::EvaluationContext context) const; |
There was a problem hiding this comment.
Should we remove the bool operator()(const GeometryTileFeature&) const overload?
| } | ||
|
|
||
| NSPredicate *operator()(mbgl::style::ExpressionFilter filter) { | ||
| return nil; |
There was a problem hiding this comment.
Once #11254 lands, the implementation should look something like this:
id jsonObject = MGLJSONObjectFromMBGLExpression(filter.expression);
return [NSPredicate mgl_predicateWithJSONObject:jsonObject];52e3666 to
f1764dd
Compare
| } | ||
|
|
||
| if (options.filter && !(*options.filter)(*geometryTileFeature)) { | ||
| if (options.filter && !(*options.filter)(style::expression::EvaluationContext { geometryTileFeature.get() })) { |
| for (size_t i = 0; i < featureCount; ++i) { | ||
| auto feature = sourceLayer->getFeature(i); | ||
| if (!leader.filter(feature->getType(), feature->getID(), [&] (const auto& key) { return feature->getValue(key); })) | ||
| if (!leader.filter(expression::EvaluationContext { feature.get() })) |
|
|
||
| // Apply filter, if any | ||
| if (queryOptions.filter && !(*queryOptions.filter)(*feature)) { | ||
| if (queryOptions.filter && !(*queryOptions.filter)(style::expression::EvaluationContext { feature.get() })) { |
|
|
||
| // Apply filter, if any | ||
| if (options.filter && !(*options.filter)(*feature)) { | ||
| if (options.filter && !(*options.filter)(style::expression::EvaluationContext { feature.get() })) { |
|
|
||
| // Apply filter, if any | ||
| if (options.filter && !(*options.filter)(*feature)) { | ||
| if (options.filter && !(*options.filter)(style::expression::EvaluationContext { feature.get() })) { |
| std::unique_ptr<GeometryTileFeature> feature = geometryLayer->getFeature(i); | ||
|
|
||
| if (!filter(feature->getType(), feature->getID(), [&] (const auto& key) { return feature->getValue(key); })) | ||
| if (!filter(expression::EvaluationContext { feature.get() })) |
4c59cdb to
bf13252
Compare
Background, raster, and custom layers do not have a filter, so we've avoided adding it to the base class. The appropriate concrete types already do have a |
jfirebaugh
left a comment
There was a problem hiding this comment.
This looks good, nice work! Just minor points.
|
|
||
| class ExpressionFilter { | ||
| public: | ||
| std::shared_ptr<expression::Expression> expression; |
There was a problem hiding this comment.
Can it be std::shared_ptr<const expression::Expression>?
|
|
||
| template <class PropertyAccessor> | ||
| bool operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const; | ||
| bool operator()(expression::EvaluationContext context) const; |
There was a problem hiding this comment.
const expression::EvaluationContext& to avoid a copy.
|
|
||
| optional<std::string> op = toString(arrayMember(filter, 0)); | ||
|
|
||
| if (*op == "has") { |
There was a problem hiding this comment.
Needs a guard conditional for if the first member is not a string (the GL JS implementation handles this implicitly in the default case).
|
|
||
| if (*op == "has") { | ||
| auto operand = toString(arrayMember(filter, 1)); | ||
| return arrayLength(filter) >= 2 && operand && *operand != "$id" && *operand != "$type"; |
There was a problem hiding this comment.
The arrayLength check needs to come before arrayMember. Also needs a guard check for string type.
There was a problem hiding this comment.
👍
optional<std::string> operand = toString(arrayMember(filter, 1));
return operand && *operand != "$id" && *operand != "$type";My understanding is that checking the truthiness of operand serves as a guard check for string type. Is that the case?
There was a problem hiding this comment.
Oh, you're right, I missed that condition.
|
@jfirebaugh @anandthakker Is this ready to 🚢 🍏 pending a final sanity test? |
| public: | ||
| using FilterBase::FilterBase; | ||
| bool operator()(expression::EvaluationContext context) const; | ||
| bool operator()(const expression::EvaluationContext &context) const; |
There was a problem hiding this comment.
Nit: const expression::EvaluationContext& context (spacing around & is not always consistent due to history, but this is our preferred style).
…lter and use it to decide in Converter<Filter>::operator() whether to parse the incoming JSON as an ExpressionFilter or one of the legacy filter types
45b0db2 to
594bfd1
Compare
|
I missed that this was targeted at |
* WIP * WIP * WIP * Remove Filter::operator()(const Feature&) * WIP * WIP * WIP * WIP * Hook up expression filter evaluator * Replace `shared_ptr` with &reference * Fill in implementation of `void operator()(const ExpressionFilter&)` * Fix failing tests * Switch back to a shared_ptr per chat with @anandthakker * Fix benchmark compilation * Shot in the dark to fix CI * Shot in the dark to fix CI (part 2) * Shot in the dark to fix CI (part 3) * In src/mbgl/style/conversion/filter.cpp, add a port of isExpressionFilter and use it to decide in Converter<Filter>::operator() whether to parse the incoming JSON as an ExpressionFilter or one of the legacy filter types * Remove bool Filter::operator()(const GeometryTileFeature&) const * Ensure the map zoom is passed into filtering operations wherever applicable * Add expression filter tests * Addressed PR feedback * Implement `NSPredicate *operator()(mbgl::style::ExpressionFilter filter)` * Fix formatting& nit
* WIP * WIP * WIP * Remove Filter::operator()(const Feature&) * WIP * WIP * WIP * WIP * Hook up expression filter evaluator * Replace `shared_ptr` with &reference * Fill in implementation of `void operator()(const ExpressionFilter&)` * Fix failing tests * Switch back to a shared_ptr per chat with @anandthakker * Fix benchmark compilation * Shot in the dark to fix CI * Shot in the dark to fix CI (part 2) * Shot in the dark to fix CI (part 3) * In src/mbgl/style/conversion/filter.cpp, add a port of isExpressionFilter and use it to decide in Converter<Filter>::operator() whether to parse the incoming JSON as an ExpressionFilter or one of the legacy filter types * Remove bool Filter::operator()(const GeometryTileFeature&) const * Ensure the map zoom is passed into filtering operations wherever applicable * Add expression filter tests * Addressed PR feedback * Implement `NSPredicate *operator()(mbgl::style::ExpressionFilter filter)` * Fix formatting& nit
Resolves #10720
GL JS equivalent mapbox/mapbox-gl-js#5193
Todo
ExpressionFiltertype to include/mbgl/style/filter.hpp that holds anmbgl::style::Expressionobject (of typembgl::style::expression::type::Boolean)mbgl::style::FilterEvaluatorStringifyFilter::operator()(const ExpressionFilter&)implementation using Implement Expression.serialize() (issue #10174) #11156Converter<Filter>::operator()whether to parse the incoming JSON as anExpressionFilteror one of the legacy filter types.bool Filter::operator()(const GeometryTileFeature&) constfilter.test.cppNSPredicate *operator()(mbgl::style::ExpressionFilter filter)cc @1ec5 depends on Streamline mbgl expression to NSExpression conversion #11254 / Cherry-pick Expression.serialize() #11312Todo in Subsequent PRs
filter-*expressions from GL JS, adding them to src/mbgl/style/expression/compound_expression.cpp.ExpressionFilter(using thefilter-*types as needed for legacy syntax)mbgl::style::FilterEvaluator, removing all the different*Filtertypes and theFiltervariant and renamingExpressionFiltertoFilter.