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

fixes #6248: handle all NSNumber types - #6290

Merged
incanus merged 1 commit into
masterfrom
6248-darwin-filter-value-types
Sep 16, 2016
Merged

incanus merged 1 commit into
masterfrom
6248-darwin-filter-value-types

Conversation

@incanus

@incanus incanus commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

No description provided.

@incanus incanus added iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold runtime styling labels Sep 8, 2016
@incanus incanus added this to the ios-v3.4.0 milestone Sep 8, 2016
@mention-bot

Copy link
Copy Markdown

@incanus, thanks for your PR! By analyzing this pull request, we identified @boundsj to be potential reviewers.

} else if (strcmp([number objCType], @encode(double)) == 0) {
return { number.doubleValue };
} else if ((strcmp([number objCType], @encode(char))) == 0) {
} else if (strcmp([number objCType], @encode(bool)) == 0) {

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.

BOOL is another type that is represented differently between 32- and 64-bit systems. 7e208c4 broke Boolean conversions on 32-bit systems while fixing it for 64-bit systems; this change does the opposite, breaking 64-bit but fixing 32-bit. We need both cases, ideally predicated (hah!) on the architecture via conditional compilation.

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.

As best I can tell, 7e208c4 is actually ok and we want ... if ((strcmp([number objCType], @encode(char))) == 0).

We used to compare [number objCType] (returns c for boolean on both 32 and 64 bit system to @encode(bool) which returns c on 32 bit and b on 64 bit. 7e208c4 changed to compare [number objCType] to @encode(char) which always returns c.

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.

I tested this branch with https://gist.github.com/boundsj/c39973346a27104a3fad1647a37606e6 (it uses a predicate with a boolean value) and this commit works by accident in that applied example since the boolean is converted into a charValue (first conversion in the new code above) and passed to core and core treats that as true. However, I think something like [unclusteredLayer setPredicate:[NSPredicate predicateWithFormat:@"%K != NO", @"cluster"]]; would not work as expected since the NO value (when converted to a charValue) would still be true.

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.

Yeah there are some weird things going on with booleans still, more to come. Been digging into this.

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.

Re: weird things, namely, regardless of platform and handling, there is currently no way to tell if a char value (e.g., 17) should be handled as a number and not a boolean. When we do actually get a boolean parsed out of an NSExpression, the class cluster type inside NSNumber is NSCFBoolean, so I'm currently working on inspecting that a bit better.

1ec5 referenced this pull request Sep 8, 2016
We convert NSNumbers (and NSStrings) to the appropriate mbgl value
so that we can use NSPredicates to  describe mbgl filters we want
to apply to style layers at runtime.

This change fixes an issue where
the conversion from an NSNumber that represented a bool was not
recognized as such. encode(bool) returns a 'c' or 'b' on 32 bit and
64 bit systems respectively and objCType of an NSNumber representing
a bool always returns 'c'. Now the implementation checks for 'c'
always and NSNumbers representing bool don't fall through and
trigger the exception.
@1ec5

1ec5 commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

Can you add tests exercising all the additional code paths?

@boundsj

boundsj commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

Can you add tests exercising all the additional code paths?

MGLFilterTests is probably a good place to add tests for the new types.

@incanus

incanus commented Sep 8, 2016

Copy link
Copy Markdown
Contributor Author

Ok, 3043409 fixes things, I think. Well, it gets the types right now. Filters still aren't working in my use case (@"uniqid == 10191020" or the formatted argument NSNumber variants like @"uniqid = %@", @10191020).

I think we want the behavior in this commit for float values on 32-bit platforms. Since our mbgl::geometry::value only supports double for decimal values, on 32-bit, float values as passed to an NSExpression by -[NSNumber numberWithFloat:] get cast to double and this can introduce imprecision. For example, I had a value like 23.23430 that got passed down to core as 23.23430000232. If we avoid float use on 32-bit, we avoid this. Most folks will probably use -[NSNumber numberWithDouble:] or boxed literals (@(23.23430)) anyway, which get interpreted as double.

Regardless of 32- or 64-bit, we interpret both char (c, 32) and BOOL (B, 64) as booleans now.

Note also the commentary in 3043409#diff-698c604c23220e922e9df03699e408abR58 about how we never seem to get back actually unsigned uint64_t from NSExpression, but I am leaving the code path in to cover our bases for completeness. I could be missing a subtlety here.

More to come.

@boundsj @1ec5 @frederoni @jfirebaugh

} else if (thirtyTwoBit && strcmp([number objCType], @encode(float)) == 0) {
// Float values are converted to double on 32-bit platforms and
// introduce precision problems.
[NSException raise:@"Float values introduce imprecision on 32-bit systems"

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 might be too harsh for what could end up being a pretty common developer scenario. Can we avoid the overprecision issue by using doubleValue instead of floatValue? As a last resort, how about logging a one-time warning instead of raising an exception?

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.

Taking the doubleValue effectively gets the same result as the double cast, so no-go. Using float is just dangerous territory here, but yeah, a log warning could suffice.

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.

Eh, this happens on 64-bit, too. I just got 23.232300 as a -numberWithFloat: turned into 23.2322998046875. So we need to warn regardless of platform.

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.

@incanus

incanus commented Sep 8, 2016

Copy link
Copy Markdown
Contributor Author

Can you add tests exercising all the additional code paths?

Begun in c3f90c3.

@1ec5

1ec5 commented Sep 8, 2016 •

Copy link
Copy Markdown
Contributor
  • Also test the minimum and maximum possible values for each numeric type

Comment thread platform/darwin/test/MGLFilterTests.mm Outdated
source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"test-source" geoJSONData:geoJSONData];
[self.mapView.style addSource:source];
layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" sourceIdentifier:@"test-source"];
[self.mapView.style addLayer:layer];

@boundsj boundsj Sep 9, 2016 •

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.

@incanus I just realized that the reason the simple test was breaking is that, since the tearDown method calls removeLayer and since that code path throws an exception if you attempt to remove a layer that has not been added, adding any test that does not add the test's layer ivar will cause the error we saw. I think we can just chalk this up to shared setup and cleanup methods sometimes causing issues like this. In any case, breaking the tests apart was the way to go here.

@incanus

incanus commented Sep 9, 2016

Copy link
Copy Markdown
Contributor Author

This is looking basically done, pending some test adjustments in order to pass on 32-bit systems. Getting some failures around the uint64_t type test maximums.

@incanus incanus removed the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Sep 9, 2016
// so warn the user to avoid them. This would require them to
// explicitly use -[NSNumber numberWithFloat:] arguments anyway.
// We still do this conversion in order to provide a valid value.
mbgl::Log::Warning(mbgl::Event::Style, "float values are converted to double and can introduce imprecision; please use double values explicitly in predicate arguments");

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.

Why mbgl::Log? Since this is in platform-specific code, we can issue an NSLog() directly. We should also stick it in a dispatch_once(), like we did in #6163, to avoid potentially spamming the console.

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.

@1ec5

1ec5 commented Sep 9, 2016

Copy link
Copy Markdown
Contributor

By the way, we currently test the conversion in the other direction, from mbgl::Value to NSExpression in ValueEvaluator, in -[MGLFeatureTests testPropertyConversion]. (We even have a (TARGET_OS_IPHONE && __LP64__) || TARGET_OS_WATCH conditional in there.) Should we break these tests out into MGLFilterTests too, or should we leave them as integration tests that involve feature querying?

@incanus

incanus commented Sep 16, 2016 •

Copy link
Copy Markdown
Contributor Author

Ok, things are much cleaner, more correct, and tests are now passing on both 32- and 64-bit test simulators as of f6350f6 and d0762f7.

@jfirebaugh helped me realize that mbgl::Value tells us how to decide whether integers will be encoded as int64_t (negative) or uint64_t (positive or zero), which simplifies things somewhat and makes them more predictable. I also figured out that we always need to deal with the (signed or unsigned) long long value in order to avoid overflow, since mbgl::Value is meant to handle the largest of integers. This helps us avoid all of the introspection and @encode comparison for integers of any type other than char and BOOL (which we treat as 32- and 64-bit booleans) and just look at the sign.

@waphumanoid, would you mind confirming whether this fixes #6248 for you? Assuming this checks out and tests pass for b9c1e1e, this will be ready to 🚢.

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.

We can still assert that certain values like -1 are round-tripped, and we can still do imprecise comparisons to make sure it's in the right ballpark.

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.

a486f3390543684c7fd63478938a00f3e52431b6

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.

In the message, say that the message won't be shown again. This is a common pattern for limited-time only offers one-time log messages.

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.

05874a4154ea9f687e98c783e2a11316a0ac6510

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.

We should at least exercise the code path to ensure we convert to something in the right ballpark.

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.

a486f3390543684c7fd63478938a00f3e52431b6

@waphumanoid

Copy link
Copy Markdown

@incanus, Sorry I can't check it since I'm not able to compile all the project :( . I use it via podspecs

@incanus

incanus commented Sep 16, 2016 •

Copy link
Copy Markdown
Contributor Author

@waphumanoid Not a huge deal, though you can do this if you want:

  1. Clone the repo & install dependencies.
  2. make iframework
  3. Use pod 'Mapbox-iOS-SDK', :path => '<clone folder>/build/ios/pkg/dynamic/Mapbox-iOS-SDK.podspec'

@incanus

incanus commented Sep 16, 2016

Copy link
Copy Markdown
Contributor Author

Ok, gonna rebase on master and squash this, then I think it's good to go! I may review it myself, but anyone else is welcome to, also.

@incanus
incanus force-pushed the 6248-darwin-filter-value-types branch from a486f33 to fc9aa30 Compare September 16, 2016 17:59
@incanus

incanus commented Sep 16, 2016

Copy link
Copy Markdown
Contributor Author

Squashed into fc9aa30. When that passes, I'll merge locally and push up to close this out.

@incanus
incanus force-pushed the 6248-darwin-filter-value-types branch from fc9aa30 to 6304103 Compare September 16, 2016 18:29
@incanus

incanus commented Sep 16, 2016

Copy link
Copy Markdown
Contributor Author

It passed, but based on elapsed time I had to rebase again and then still couldn't merge at the command line as things are blocked by this new review system. Can I get someone to review this as of 6304103 so I can land this?

@incanus
incanus merged commit 0bd66d4 into master Sep 16, 2016
@incanus
incanus deleted the 6248-darwin-filter-value-types branch September 16, 2016 18:52
@waphumanoid

waphumanoid commented Sep 17, 2016 •

Copy link
Copy Markdown

@incanus, Thanks, I did compile the master branch (with a little issue which I will post separately) and give it some tests. It works as expected so far. Still testing and looking forward to seeing an implementation for [NSException raise:@"Unsupported filter type" format:@"Cannot convert mbgl::style::NotHasFilter to NSPredicate"];

@1ec5

1ec5 commented Sep 17, 2016

Copy link
Copy Markdown
Contributor

looking forward to seeing an implementation for [NSException raise:@"Unsupported filter type" format:@"Cannot convert mbgl::style::NotHasFilter to NSPredicate"];

This is being tracked in #6244.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants