Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[core] Add expression filter support - #11251

Merged
lucaswoj merged 24 commits into
masterfrom
expession-filter
Mar 8, 2018
Merged

lucaswoj merged 24 commits into
masterfrom
expession-filter

Conversation

@lucaswoj

@lucaswoj lucaswoj commented Feb 20, 2018 •

Copy link
Copy Markdown
Contributor

Resolves #10720

GL JS equivalent mapbox/mapbox-gl-js#5193

Todo

Todo in Subsequent PRs

@lucaswoj lucaswoj added Core The cross-platform C++ core, aka mbgl in progress labels Feb 20, 2018

@anandthakker anandthakker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 looking good to me!

Comment thread include/mbgl/style/filter.hpp Outdated
std::shared_ptr<expression::Expression> expression;

friend bool operator==(const ExpressionFilter& lhs, const ExpressionFilter& rhs) {
return lhs.expression == rhs.expression;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is checking the pointers rather than the expression objects themselves. (*(lhs.expression) == *(rhs.expression) to do a deep equality check)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, doh, I was thinking about the ExpressionFilter&s, not the shared_ptr<Expression>s 😄

Comment thread include/mbgl/style/filter.hpp Outdated

class ExpressionFilter {
public:
std::shared_ptr<expression::Expression> expression;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a unique_ptr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Will try that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: this won't work because ExpressionFilter needs to be copyable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be std::shared_ptr<const expression::Expression>?

Comment thread src/mbgl/style/conversion/stringify.hpp Outdated
}

void operator()(const ExpressionFilter&) {
stringifyUnaryFilter("herp", "derp");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11156 should make this fairly straightforward

Comment thread include/mbgl/style/filter.hpp Outdated

template <class PropertyAccessor>
bool operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const;
bool operator()(expression::EvaluationContext context) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the bool operator()(const GeometryTileFeature&) const overload?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely 👍

}

NSPredicate *operator()(mbgl::style::ExpressionFilter filter) {
return nil;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once #11254 lands, the implementation should look something like this:

id jsonObject = MGLJSONObjectFromMBGLExpression(filter.expression);
return [NSPredicate mgl_predicateWithJSONObject:jsonObject];

Comment thread src/mbgl/geometry/feature_index.cpp Outdated
}

if (options.filter && !(*options.filter)(*geometryTileFeature)) {
if (options.filter && !(*options.filter)(style::expression::EvaluationContext { geometryTileFeature.get() })) {

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

Comment thread src/mbgl/layout/symbol_layout.cpp Outdated
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() }))

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

Comment thread src/mbgl/tile/custom_geometry_tile.cpp Outdated

// Apply filter, if any
if (queryOptions.filter && !(*queryOptions.filter)(*feature)) {
if (queryOptions.filter && !(*queryOptions.filter)(style::expression::EvaluationContext { feature.get() })) {

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

Comment thread src/mbgl/tile/geojson_tile.cpp Outdated

// Apply filter, if any
if (options.filter && !(*options.filter)(*feature)) {
if (options.filter && !(*options.filter)(style::expression::EvaluationContext { feature.get() })) {

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

Comment thread src/mbgl/tile/geometry_tile.cpp Outdated

// Apply filter, if any
if (options.filter && !(*options.filter)(*feature)) {
if (options.filter && !(*options.filter)(style::expression::EvaluationContext { feature.get() })) {

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

Comment thread src/mbgl/tile/geometry_tile_worker.cpp Outdated
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() }))

@lucaswoj lucaswoj Feb 26, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • needs zoom

@lucaswoj lucaswoj changed the title Add expression filter support [core] Add expression filter support Feb 28, 2018
@jfirebaugh

Copy link
Copy Markdown
Contributor

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 getFilter accessor.

@jfirebaugh jfirebaugh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, nice work! Just minor points.

Comment thread include/mbgl/style/filter.hpp Outdated

class ExpressionFilter {
public:
std::shared_ptr<expression::Expression> expression;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be std::shared_ptr<const expression::Expression>?

Comment thread include/mbgl/style/filter.hpp Outdated

template <class PropertyAccessor>
bool operator()(FeatureType type, optional<FeatureIdentifier> id, PropertyAccessor accessor) const;
bool operator()(expression::EvaluationContext context) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const expression::EvaluationContext& to avoid a copy.

Comment thread src/mbgl/style/conversion/filter.cpp Outdated

optional<std::string> op = toString(arrayMember(filter, 0));

if (*op == "has") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a guard conditional for if the first member is not a string (the GL JS implementation handles this implicitly in the default case).

Comment thread src/mbgl/style/conversion/filter.cpp Outdated

if (*op == "has") {
auto operand = toString(arrayMember(filter, 1));
return arrayLength(filter) >= 2 && operand && *operand != "$id" && *operand != "$type";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arrayLength check needs to come before arrayMember. Also needs a guard check for string type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right, I missed that condition.

@lucaswoj

lucaswoj commented Mar 6, 2018

Copy link
Copy Markdown
Contributor Author

@jfirebaugh @anandthakker Is this ready to 🚢 🍏 pending a final sanity test?

@anandthakker anandthakker left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 🐑 !

Comment thread include/mbgl/style/filter.hpp Outdated
public:
using FilterBase::FilterBase;
bool operator()(expression::EvaluationContext context) const;
bool operator()(const expression::EvaluationContext &context) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: const expression::EvaluationContext& context (spacing around & is not always consistent due to history, but this is our preferred style).

@lucaswoj
lucaswoj merged commit 5b3ebc4 into master Mar 8, 2018
@lucaswoj
lucaswoj deleted the expession-filter branch March 8, 2018 23:48
@jfirebaugh

Copy link
Copy Markdown
Contributor

I missed that this was targeted at master. Can you please open a PR cherry-picking it to boba-release?

tobrun pushed a commit that referenced this pull request Mar 9, 2018
* 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
tobrun pushed a commit that referenced this pull request Mar 9, 2018
* 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
@tobrun tobrun mentioned this pull request Mar 9, 2018
9 tasks done
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants